1、程序实验211多线程编程实验报告程序实验二:11-多线程编程实验专业班级 实验日期 5.21 姓名 学号 实验一(p284:11-thread.c)1、软件功能描述创建3个线程,让3个线程重用同一个执行函数,每个线程都有5次循环,可以看成5个小任务,每次循环之间会有随即等待时间(1-10s)意义在于模拟每个任务到达的时间是随机的没有任何的特定规律。2、程序流程设计3部分程序代码注释(关键函数或代码)#include#include #include #define T_NUMBER 3#define P_NUMBER 5#define TIME 10.0void *thrd_func(void
2、 *arg ) int thrd_num=(int)arg; int delay_time =0; int count =0; printf(Thread %d is staraingn,thrd_num); for(count=0;countP_NUMBER;count+) delay_time =(int)(rand()*TIME/(RAND_MAX)+1; sleep(delay_time); printf(tTH%d:job%d delay =%dn,thrd_num,count,delay_time); printf(%d finishedn,thrd_num); pthread_e
3、xit(NULL); int main() pthread_t threadT_NUMBER; int no=0,res; void * thrd_ret; srand(time(NULL); for(no=0;noT_NUMBER;no+) res=pthread_create(&threadno,NULL, thrd_func,(void*)no); if(res!=0) printf(Creay th %d faildn,no); exit(res); printf(successnwaitingn); for(no=0;noT_NUMBER;no+) res=pthread_join(
4、threadno,&thrd_ret); if(!res) printf(t %d joinedn,no); else printf(T %djoined faildn,no); return 0;4编译、运行方法及结果(抓屏)5结果分析由运行结果可以看出,创建线程、释放资源按照顺序,而每个线程的运行和结束是独立与并行的。实验二(p287: 11-thread_mutex.c)1、软件功能描述在试验1的基础上通过互斥锁,使原本独立,无序的多个线程能够按顺序进行2、程序流程设计3部分程序代码注释(关键函数或代码)#include#include#include#define THREAD_NUM
5、BER 3 /*线程数*/#define REPEAT_NUMBER 3 /*每个线程的小任务数*/#define DELAY_TIME_LEVELS 10.0 /*小任务间的最大时间间隔*/pthread_mutex_t mutex; void *thrd_func(void *arg) /线程函数例程 int thrd_num = (int)arg; int delay_time = 0, count = 0; int res; /互斥锁上锁 res = pthread_mutex_lock(&mutex); if(res) printf(Thread %d is startingn, t
6、hrd_num); pthread_exit(NULL); printf(Thread %d is startingn, thrd_num); for(count = 0; count REPEAT_NUMBER; count+) delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX) + 1; sleep(delay_time); printf(tThread %d: job %d delay = %dn,thrd_num, count, delay_time); printf(Thread %d finishedn, thrd_nu
7、m); pthread_exit(NULL); int main(void) pthread_t threadTHREAD_NUMBER; int no = 0, res; void * thrd_ret; srand(time(NULL); pthread_mutex_init(&mutex, NULL); for(no = 0; no THREAD_NUMBER; no+ ) res = pthread_create(&threadno, NULL, thrd_func, (void*)no); if(res != 0) printf(Create thread %d failedn, n
8、o ); exit(res); printf(Create threads successn Waiting for threads to finish.n);/*这里的问题很奇怪,按照书上程序敲打,出现的线程首先是2而非0,没有按照书上所说:与创建顺序相同,但是在这里体现了互斥性,由于后面的释放资源程序从0开始,而一开始为线程2占用资源,所以无法释放资源,导致程序卡死,最后修改为先释放线程2的资源,之后则可以依次显示*/ for(no = 2; no THREAD_NUMBER; no-) res = pthread_join(threadno,&thrd_ret); if(!res) pr
9、intf(Thread %d joinedn, no); else printf(Thread %d join failedn, no); pthread_mutex_unlock(&mutex); pthread_mutex_destroy(&mutex); return 0;4编译、运行方法及结果(抓屏)5结果分析实验结果体现了互斥性,3个线程有序运行。实验三(P291:11-thread_sem.c)1、软件功能描述该程序在实验1的基础上,用信号量同步机制实现3个线程的有序执行,利用pv操作来控制执行顺序,达到执行的顺序和创建的顺序刚好相反。2、程序流程设计3部分程序代码注释(关键函数或
10、代码)#include#include#include#include#define THREAD_NUMBER 3#define REPEAT_NUMBER 3#define DELAY_TIME_LEVELS 10.0sem_t semTHREAD_NUMBER; void *thrd_func(void *arg)int thrd_num = (int)arg; int delay_time = 0; int count = 0; sem_wait(&semthrd_num); printf(Thread %d is startingn,thrd_num); for(count = 0;
11、 count REPEAT_NUMBER; count+)delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)+1; sleep(delay_time); printf(tThread %d: job %d delay = %dn,thrd_num, count, delay_time); printf(Thread %d finishedn, thrd_num); pthread_exit(NULL); int main(void)pthread_t threadTHREAD_NUMBER; int no = 0,res; void
12、 * thrd_ret; srand(time(NULL); for(no = 0; no = 0; no-)res = pthread_join(threadno, &thrd_ret); if(!res)printf(Thread %d joinedn, no); elseprintf(Thread %d join failedn, no); sem_post(&sem(no + THREAD_NUMBER-1)%THREAD_NUMBER); for(no = 0; no THREAD_NUMBER; no+)sem_destroy(&semno); return 0; 4编译、运行方法
13、及结果(抓屏)5结果分析程序运行、结束都是按照顺序进行的,只是顺序和创建的顺序刚好相反。实验四(p295: 11-thread_attr.c)1、软件功能描述创建一个线程,具有绑定和分离属性,主线程通过finishflag 标志变量来获得线程结束的消息,不调用pthread_join函数2、程序流程设计3部分程序代码注释(关键函数或代码)#include#include#include#define REPEAT_NUMBER 3#define DELAY_TIME_LEVELS 10.0int finish_flag = 0; void *thrd_func(void *arg)int de
14、lay_time = 0; int count = 0; printf(Th is startingn); for(count=0; count REPEAT_NUMBER; count+)delay_time = (int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX) + 1); sleep(delay_time); printf(tTh : job %d delay = %dn,count,delay_time); printf(Th finishedn); finish_flag = 1; pthread_exit(NULL); int main(void)p
15、thread_t thread; pthread_attr_t attr; int no = 0,res; void * thrd_ret; srand(time(NULL); res = pthread_attr_init(&attr); if(res != 0)printf(Create attribute failedn); exit(res); res = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);res += pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED
16、); if(res!=0)printf(Setting attribute failedn); exit(res); res = pthread_create(&th, &attr, thrd_func, NULL); if(res!=0)printf(Create th failedn); exit(res); pthread_attr_destroy(&attr); printf(Create th successn); while(!finish_flag)printf(Waiting for th to finish.n); sleep(2); return 0; 4编译、运行方法及结
17、果(抓屏)5结果分析由结果可以看出,这个创建的线程具有绑定好而分离属性,而且主线程通过一个finish_glag标志变量赖获得线程结束的消息,但是线程在运行结束之后似乎没有收回系统资源,因为程序运行前和程序运行后使用“free”命令查看内存使用情况,发现使用的内存并不一致。实验五(p298: 11-producer-customer.c)1、软件功能描述信号量的意义,在程序生产中和消费过程中是随机进行的,而且生产者的速度比消费者的速度平均快两倍。生产者一次生产一个单元产品,消费者一次消费一个单元的产品。2、程序流程设计3部分程序代码注释(关键函数或代码)#include#include#inc
18、lude#include#include#include#include#include#define MYFIFO myfifo#define BUFFER_SIZE 3#define UNIT_SIZE 5#define RUN_TIME 30#define DELAY_TIME_LEVELS 5.0int fd; time_t end_time; sem_t mutex,full,avail; void *producer(void *arg)int real_write; int delay_time=0; while(time(NULL)end_time)delay_time=(in
19、t)(rand()*DELAY_TIME_LEVELS/(RAND_MAX)/2.0)+1; sleep(delay_time); sem_wait(&avail); sem_wait(&mutex); printf(nProducer: delay = %dn, delay_time); if(real_write=write(fd,hello,UNIT_SIZE)=-1)if(errno=EAGAIN)printf(The FIFO has not been read yet.Please try lattern); elseprintf(Write %d to the FIFOn, re
20、al_write); sem_post(&full); sem_post(&mutex); pthread_exit(NULL); void *customer(void *arg)unsigned char read_bufferUNIT_SIZE; int real_read; int delay_time; while(time(NULL)end_time)delay_time=(int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX)+1; sleep(delay_time); sem_wait(&full); sem_wait(&mutex); memset(
21、read_buffer,0,UNIT_SIZE); printf(nCustomer: delay = %dn,delay_time); if(real_read = read(fd,read_buffer,UNIT_SIZE)=-1)if(errno=EAGAIN)printf(No data yetn); printf(Read %s from FIFOn,read_buffer); sem_post(&avail); sem_post(&mutex); pthread_exit(NULL); int main()pthread_t thrd_prd_id,thrd_cst_id; pth
22、read_t mon_th_id; int ret;srand(time(NULL); end_time=time(NULL)+RUN_TIME; if(mkfifo(MYFIFO,O_CREAT|O_EXCL)0)&(errno != EEXIST)printf(Cannot create fifon); return errno; fd = open(MYFIFO,O_RDWR); if(fd=-1)printf(Open fifo errorn); return fd; ret = sem_init(&mutex,0,1); ret += sem_init(&avail,0,BUFFER
23、_SIZE); ret += sem_init(&full,0,0); if(ret !=0)printf(Any semaphore initialization failedn); return ret; ret = pthread_create(&thrd_prd_id,NULL,producer,NULL); if(ret!=0)printf(Create producer thread errorn); return ret;ret = pthread_create(&thrd_cst_id,NULL,customer,NULL); if(ret != 0)printf(Create customer thread errorn); return ret; pthread_join(thrd_prd_id,NULL); pthread_join(thrd_cst_id,NULL); close(fd); unlink(MYFIFO); return 0; 4编译、运行方法及结果(抓屏)5结果分析由实验结果可以看出生产者的速度比消费者的速度平均快2倍左右。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1