程序实验2:11-多线程编程-实验报告_第1页
程序实验2:11-多线程编程-实验报告_第2页
程序实验2:11-多线程编程-实验报告_第3页
程序实验2:11-多线程编程-实验报告_第4页
程序实验2:11-多线程编程-实验报告_第5页
已阅读5页,还剩15页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、嵌入式操作系统多线程 -实验报告( 11-多线程编程)程序实验二: 11-多线程编程实验专业班级实验日期5.21姓名学号实验一( p284: 11-thread.c)1、软件功能描述创建 3 个线程,让 3 个线程重用同一个执行函数,每个线程都有 5 次循环,可以看成 5 个小任务,每次循环之间会有随即等待时间 ( 1-10s)意义在于模拟每个任务到达的时间是随机的没有任何的特定规律。2、程序流程设计3部分程序代码注释 (关键函数或代码 )#include #include#include嵌入式操作系统多线程-实验报告( 11-多线程编程)#defineT_NUMBER 3#defineP_N

2、UMBER 5#defineTIME 10.0void *thrd_func(void *arg )intthrd_num=( int )arg;intdelay_time =0;intcount =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);print

3、f(%d finishedn,thrd_num);pthread_exit(NULL);intmain()pthread_t threadT_NUMBER;intno=0,res;void * thrd_ret;srand(time(NULL);for (no=0;noT_NUMBER;no+)res=pthread_create(&threadno,NULL, thrd_func,(if (res!=0)void *)no);printf(Creay th %d faildn,no);exit(res);printf(successnwaitingn);for (no=0;noT_NUMBE

4、R;no+)res=pthread_join(threadno,&thrd_ret);if (!res)printf(t %d joinedn,no);嵌入式操作系统多线程 -实验报告( 11-多线程编程)elseprintf(T %djoined faildn,no);return0;4编译、运行方法及结果(抓屏 )5结果分析由运行结果可以看出, 创建线程、 释放资源按照顺序, 而每个线程的运行和结束是独立与并行的。嵌入式操作系统多线程 -实验报告( 11-多线程编程)嵌入式操作系统多线程 -实验报告( 11-多线程编程)实验二( p287: 11-thread_mutex.c)1、软件功能

5、描述在试验 1 的基础上通过互斥锁,使原本独立,无序的多个线程能够按顺序进行2、程序流程设计3部分程序代码注释 (关键函数或代码 )#include#include#include#define THREAD_NUMBER3/* 线程数 */#define REPEAT_NUMBER3/* 每个线程的小任务数 */#define DELAY_TIME_LEVELS10.0/* 小任务间的最大时间间隔 */pthread_mutex_t mutex;void *thrd_func(void *arg) /线程函数例程int thrd_num = (int)arg;int delay_time =

6、 0, count = 0;int res;/互斥锁上锁res = pthread_mutex_lock(&mutex);if(res)嵌入式操作系统多线程 -实验报告( 11-多线程编程)printf(Thread %d is startingn, thrd_num);pthread_exit(NULL);printf(Thread %d is startingn, thrd_num);for(count = 0; count REPEA T_NUMBER; count+)delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX) + 1

7、; 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 * thrd_ret;srand(time(NULL);pthread_mutex_init(&mutex, NULL);for(no = 0; no THREAD_NUMBER;

8、no+ )res = pthread_create(&threadno, NULL, thrd_func, (void*)no); if(res != 0)printf(Create thread %d failedn, no );exit(res);printf(Create threads successn Waiting for threads to finish.n);/* 这里的问题很奇怪,按照书上程序敲打,出现的线程首先是2 而非 0,没有按照书上所说:与创建顺序相同,但是在这里体现了互斥性,由于后面的释放资源程序从为线程 2 占用资源,所以无法释放资源,导致程序卡死,最后修改为先

9、释放线程之后则可以依次显示*/for(no = 2; no THREAD_NUMBER; no-)0 开始,而一开始2 的资源,res = pthread_join(threadno,&thrd_ret);if(!res)printf(Thread %d joinedn, no);嵌入式操作系统多线程 -实验报告( 11-多线程编程)elseprintf(Thread %d join failedn, no);pthread_mutex_unlock(&mutex);pthread_mutex_destroy(&mutex);return 0;4编译、运行方法及结果(抓屏 )5结果分析实验结果

10、体现了互斥性,3 个线程有序运行。嵌入式操作系统多线程 -实验报告( 11-多线程编程)实验三( P291: 11-thread_sem.c)1、软件功能描述该程序在实验1 的基础上,用信号量同步机制实现3 个线程的有序执行,利用pv 操作来控制执行顺序,达到执行的顺序和创建的顺序刚好相反。2、程序流程设计3部分程序代码注释 (关键函数或代码 )#include#include#include#include#define THREAD_NUMBER3#define REPEAT_NUMBER 3#define DELAY_TIME_LEVELS 10.0sem_t semTHREAD_NUM

11、BER;嵌入式操作系统多线程 -实验报告( 11-多线程编程)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; count REPEAT_NUMBER; count+)delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)+1; sleep(delay_time);pr

12、intf(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 * thrd_ret;srand(time(NULL);for(no = 0; no = 0; no-)res = pthread_join(threadno, &thrd_ret);if(!res)printf(Thread

13、 %d joinedn, no);嵌入式操作系统多线程 -实验报告( 11-多线程编程)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编译、运行方法及结果(抓屏 )5结果分析嵌入式操作系统多线程 -实验报告( 11-多线程编程)程序运行、结束都是按照顺序进行的,只是顺序和创建的顺序刚好相反。嵌入式操作系统多线程 -实验报告( 11-多线

14、程编程)实验四( p295: 11-thread_attr.c)1、软件功能描述创建一个线程,具有绑定和分离属性,主线程通过 finish flag 标志变量来获得线程结束的消息,不调用 pthread_join 函数2、程序流程设计3部分程序代码注释 (关键函数或代码 )#include#include#include#define REPEAT_NUMBER 3#define DELAY_TIME_LEVELS10.0int finish_flag = 0;void *thrd_func(void *arg)嵌入式操作系统多线程 -实验报告( 11-多线程编程)int delay_time

15、 = 0;int count = 0;printf(Th is startingn);for(count=0; count REPEA T_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)pthread_t thread

16、;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); if(res!=0)printf(Set

17、ting 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)嵌入式操作系统多线程 -实验报告( 11-多线程编程)printf(Waiting for th to finish.n);sleep(2);return 0;4编译、运行方法及结果(抓屏 )

18、5结果分析由结果可以看出,这个创建的线程具有绑定好而分离属性,而且主线程通过一个 finish_glag 标志变量赖获得线程结束的消息,但是线程在运行结束之后似乎没有收回系统资源,因为程序运行前和程序运行后使用“ free”命令查看内存使用情况,发现使用的内存并不一致。嵌入式操作系统多线程 -实验报告( 11-多线程编程)实验五( p298: 11-producer-customer.c)1、软件功能描述信号量的意义, 在程序生产中和消费过程中是随机进行的, 而且生产者的速度比消费者的速度平均快两倍。 生产者一次生产一个单元产品, 消费者一次消费一个单元的产品 。2、程序流程设计3部分程序代码

19、注释 (关键函数或代码 )#include#include#include嵌入式操作系统多线程 -实验报告( 11-多线程编程)#include#include#include#include#include#define MYFIFO myfifo#defineBUFFER_SIZE3#defineUNIT_SIZE5#defineRUN_TIME30#defineDELAY_TIME_LEVELS5.0int fd;time_t end_time;sem_t mutex,full,avail;void *producer(void *arg)int real_write;int delay

20、_time=0;while(time(NULL)end_time)delay_time=(int)(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 latt

21、ern);elseprintf(Write %d to the FIFOn, real_write);sem_post(&full);sem_post(&mutex);pthread_exit(NULL);void *customer(void *arg)嵌入式操作系统多线程 -实验报告( 11-多线程编程)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;

22、sleep(delay_time);sem_wait(&full);sem_wait(&mutex);memset(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;pthread_t mon_th_id;int ret;srand(time(NULL);end_time=time(NULL)+RUN_TIME;if(mkfifo(MYFIFO,O_CREAT|O_EX

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论