void *downloadfile(void *filename) { printf("I am downloading the file %s!\n", (char *)filename); sleep(10); long downloadtime = rand()%100; printf("I finish downloading the file within %d minutes!\n", downloadtime); pthread_exit((void *)downloadtime); }
int main(int argc, char *argv[]) { char files[NUM_OF_TASKS][20]={"file1.avi","file2.rmvb","file3.mp4","file4.wmv","file5.flv"}; pthread_t threads[NUM_OF_TASKS]; int rc; int t; int downloadtime;
# ./a.out creating thread 0, please help me to download file1.avi creating thread 1, please help me to download file2.rmvb I am downloading the file file1.avi! creating thread 2, please help me to download file3.mp4 I am downloading the file file2.rmvb! creating thread 3, please help me to download file4.wmv I am downloading the file file3.mp4! creating thread 4, please help me to download file5.flv I am downloading the file file4.wmv! I am downloading the file file5.flv! I finish downloading the file within 83 minutes! I finish downloading the file within 77 minutes! I finish downloading the file within 86 minutes! I finish downloading the file within 15 minutes! I finish downloading the file within 93 minutes! Thread 0 downloads the file file1.avi in 83 minutes. Thread 1 downloads the file file2.rmvb in 86 minutes. Thread 2 downloads the file file3.mp4 in 77 minutes. Thread 3 downloads the file file4.wmv in 93 minutes. Thread 4 downloads the file file5.flv in 15 minutes.
for(t=0;t<NUM_OF_TASKS;t++){ rc = pthread_create(&threads[t], NULL, coder, NULL); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } }
sleep(5);
for(t=1;t<=4;t++){ pthread_mutex_lock(&g_task_lock); tail+=t; printf("I am Boss, I assigned %d tasks, I notify all coders!\n", t); pthread_cond_broadcast(&g_task_cv); pthread_mutex_unlock(&g_task_lock); sleep(20); }
[root@deployer createthread]# ./a.out //招聘三个员工,一开始没有任务,大家睡大觉 No task now! Thread 3491833600 is waiting! No task now! Thread 3483440896 is waiting! No task now! Thread 3475048192 is waiting! //老板开始分配任务了,第一批任务就一个,告诉三个员工醒来抢任务 I am Boss, I assigned 1 tasks, I notify all coders! //员工一先发现有任务了,开始抢任务 Have task now! Thread 3491833600 is grabing the task ! //员工一抢到了任务A,开始干活 Thread 3491833600 has a task A now! //员工二也发现有任务了,开始抢任务,不好意思,就一个任务,让人家抢走了,接着等吧 Have task now! Thread 3483440896 is grabing the task ! No task now! Thread 3483440896 is waiting! //员工三也发现有任务了,开始抢任务,你比员工二还慢,接着等吧 Have task now! Thread 3475048192 is grabing the task ! No task now! Thread 3475048192 is waiting! //员工一把任务做完了,又没有任务了,接着等待 Thread 3491833600 finish the task A ! No task now! Thread 3491833600 is waiting! //老板又有新任务了,这次是两个任务,叫醒他们 I am Boss, I assigned 2 tasks, I notify all coders! //这次员工二比较积极,先开始抢,并且抢到了任务B Have task now! Thread 3483440896 is grabing the task ! Thread 3483440896 has a task B now! //这次员工三也聪明了,赶紧抢,要不然没有年终奖了,终于抢到了任务C Have task now! Thread 3475048192 is grabing the task ! Thread 3475048192 has a task C now! //员工一上次抢到了,这次抢的慢了,没有抢到,是不是飘了 Have task now! Thread 3491833600 is grabing the task ! No task now! Thread 3491833600 is waiting! //员工二做完了任务B,没有任务了,接着等待 Thread 3483440896 finish the task B ! No task now! Thread 3483440896 is waiting! //员工三做完了任务C,没有任务了,接着等待 Thread 3475048192 finish the task C ! No task now! Thread 3475048192 is waiting! //又来任务了,这次是三个任务,人人有份 I am Boss, I assigned 3 tasks, I notify all coders! //员工一抢到了任务D,员工二抢到了任务E,员工三抢到了任务F Have task now! Thread 3491833600 is grabing the task ! Thread 3491833600 has a task D now! Have task now! Thread 3483440896 is grabing the task ! Thread 3483440896 has a task E now! Have task now! Thread 3475048192 is grabing the task ! Thread 3475048192 has a task F now! //三个员工都完成了,然后都又开始等待 Thread 3491833600 finish the task D ! Thread 3483440896 finish the task E ! Thread 3475048192 finish the task F ! No task now! Thread 3491833600 is waiting! No task now! Thread 3483440896 is waiting! No task now! Thread 3475048192 is waiting! //公司活越来越多了,来了四个任务,赶紧干呀 I am Boss, I assigned 4 tasks, I notify all coders! //员工一抢到了任务G,员工二抢到了任务H,员工三抢到了任务I Have task now! Thread 3491833600 is grabing the task ! Thread 3491833600 has a task G now! Have task now! Thread 3483440896 is grabing the task ! Thread 3483440896 has a task H now! Have task now! Thread 3475048192 is grabing the task ! Thread 3475048192 has a task I now! //员工一和员工三先做完了,发现还有一个任务开始抢 Thread 3491833600 finish the task G ! Thread 3475048192 finish the task I ! //员工三没抢到,接着等 No task now! Thread 3475048192 is waiting! //员工一抢到了任务J,多做了一个任务 Thread 3491833600 has a task J now! //员工二这才把任务H做完,黄花菜都凉了,接着等待吧 Thread 3483440896 finish the task H ! No task now! Thread 3483440896 is waiting! //员工一做完了任务J,接着等待 Thread 3491833600 finish the task J ! No task now! Thread 3491833600 is waiting!