欢迎来到天天文库
浏览记录
ID:59461463
大小:496.50 KB
页数:20页
时间:2020-11-02
《程序实验2:11-多线程编程---实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、程序实验二:11-多线程编程实验专业班级实验日期5.21姓名学号实验一(p284:11-thread.c)1、软件功能描述创建3个线程,让3个线程重用同一个执行函数,每个线程都有5次循环,可以看成5个小任务,每次循环之间会有随即等待时间(1-10s)意义在于模拟每个任务到达的时间是随机的没有任何的特定规律。2、程序流程设计3.部分程序代码注释(关键函数或代码)#include#include#include#defineT_NUMBER3#defineP_NUMBER5#define
2、TIME10.0void*thrd_func(void*arg){intthrd_num=(int)arg;intdelay_time=0;intcount=0;printf("Thread%disstaraing",thrd_num);for(count=0;count3、e);}printf("%dfinished",thrd_num);pthread_exit(NULL);}intmain(){pthread_tthread[T_NUMBER];intno=0,res;void*thrd_ret;srand(time(NULL));for(no=0;no4、rintf("successwaiting");for(no=0;no5、述在试验1的基础上通过互斥锁,使原本独立,无序的多个线程能够按顺序进行2、程序流程设计3.部分程序代码注释(关键函数或代码)#include#include#include#defineTHREAD_NUMBER3/*线程数*/#defineREPEAT_NUMBER3/*每个线程的小任务数*/#defineDELAY_TIME_LEVELS10.0/*小任务间的最大时间间隔*/pthread_mutex_tmutex;void*thrd_func(void*arg)//线程函数例程6、{intthrd_num=(int)arg;intdelay_time=0,count=0;intres;//互斥锁上锁res=pthread_mutex_lock(&mutex);if(res){printf("Thread%disstarting",thrd_num);pthread_exit(NULL);}printf("Thread%disstarting",thrd_num);for(count=0;count7、_LEVELS/(RAND_MAX))+1;sleep(delay_time);printf("tThread%d:job%ddelay=%d",thrd_num,count,delay_time);}printf("Thread%dfinished",thrd_num);pthread_exit(NULL);}intmain(void){pthread_tthread[THREAD_NUMBER];intno=0,res;void*thrd_ret;srand(time(NULL));pthread_mutex_init(&mut8、ex,NULL);for(no=0;no
3、e);}printf("%dfinished",thrd_num);pthread_exit(NULL);}intmain(){pthread_tthread[T_NUMBER];intno=0,res;void*thrd_ret;srand(time(NULL));for(no=0;no4、rintf("successwaiting");for(no=0;no5、述在试验1的基础上通过互斥锁,使原本独立,无序的多个线程能够按顺序进行2、程序流程设计3.部分程序代码注释(关键函数或代码)#include#include#include#defineTHREAD_NUMBER3/*线程数*/#defineREPEAT_NUMBER3/*每个线程的小任务数*/#defineDELAY_TIME_LEVELS10.0/*小任务间的最大时间间隔*/pthread_mutex_tmutex;void*thrd_func(void*arg)//线程函数例程6、{intthrd_num=(int)arg;intdelay_time=0,count=0;intres;//互斥锁上锁res=pthread_mutex_lock(&mutex);if(res){printf("Thread%disstarting",thrd_num);pthread_exit(NULL);}printf("Thread%disstarting",thrd_num);for(count=0;count7、_LEVELS/(RAND_MAX))+1;sleep(delay_time);printf("tThread%d:job%ddelay=%d",thrd_num,count,delay_time);}printf("Thread%dfinished",thrd_num);pthread_exit(NULL);}intmain(void){pthread_tthread[THREAD_NUMBER];intno=0,res;void*thrd_ret;srand(time(NULL));pthread_mutex_init(&mut8、ex,NULL);for(no=0;no
4、rintf("successwaiting");for(no=0;no5、述在试验1的基础上通过互斥锁,使原本独立,无序的多个线程能够按顺序进行2、程序流程设计3.部分程序代码注释(关键函数或代码)#include#include#include#defineTHREAD_NUMBER3/*线程数*/#defineREPEAT_NUMBER3/*每个线程的小任务数*/#defineDELAY_TIME_LEVELS10.0/*小任务间的最大时间间隔*/pthread_mutex_tmutex;void*thrd_func(void*arg)//线程函数例程6、{intthrd_num=(int)arg;intdelay_time=0,count=0;intres;//互斥锁上锁res=pthread_mutex_lock(&mutex);if(res){printf("Thread%disstarting",thrd_num);pthread_exit(NULL);}printf("Thread%disstarting",thrd_num);for(count=0;count7、_LEVELS/(RAND_MAX))+1;sleep(delay_time);printf("tThread%d:job%ddelay=%d",thrd_num,count,delay_time);}printf("Thread%dfinished",thrd_num);pthread_exit(NULL);}intmain(void){pthread_tthread[THREAD_NUMBER];intno=0,res;void*thrd_ret;srand(time(NULL));pthread_mutex_init(&mut8、ex,NULL);for(no=0;no
5、述在试验1的基础上通过互斥锁,使原本独立,无序的多个线程能够按顺序进行2、程序流程设计3.部分程序代码注释(关键函数或代码)#include#include#include#defineTHREAD_NUMBER3/*线程数*/#defineREPEAT_NUMBER3/*每个线程的小任务数*/#defineDELAY_TIME_LEVELS10.0/*小任务间的最大时间间隔*/pthread_mutex_tmutex;void*thrd_func(void*arg)//线程函数例程
6、{intthrd_num=(int)arg;intdelay_time=0,count=0;intres;//互斥锁上锁res=pthread_mutex_lock(&mutex);if(res){printf("Thread%disstarting",thrd_num);pthread_exit(NULL);}printf("Thread%disstarting",thrd_num);for(count=0;count7、_LEVELS/(RAND_MAX))+1;sleep(delay_time);printf("tThread%d:job%ddelay=%d",thrd_num,count,delay_time);}printf("Thread%dfinished",thrd_num);pthread_exit(NULL);}intmain(void){pthread_tthread[THREAD_NUMBER];intno=0,res;void*thrd_ret;srand(time(NULL));pthread_mutex_init(&mut8、ex,NULL);for(no=0;no
7、_LEVELS/(RAND_MAX))+1;sleep(delay_time);printf("tThread%d:job%ddelay=%d",thrd_num,count,delay_time);}printf("Thread%dfinished",thrd_num);pthread_exit(NULL);}intmain(void){pthread_tthread[THREAD_NUMBER];intno=0,res;void*thrd_ret;srand(time(NULL));pthread_mutex_init(&mut
8、ex,NULL);for(no=0;no
此文档下载收益归作者所有