




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
S10、并发程序-同步与互斥S10、并发程序-同步与互斥S10、并发程序-同步与互斥1、并发线程同步与互斥#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<unistd.h>#include<string.h>intnum=30,count=10;pthread_mutex_tmylock=PTHREAD_MUTEX_INITIALIZER;S10、并发程序-同步与互斥void*sub1(void*arg){
inti=0,tmp;for(;i<count;i++){ pthread_mutex_lock(&mylock);
tmp=num-1; usleep(13);
num=tmp; pthread_mutex_unlock(&mylock); printf("线程1num减1后值为:%d\n",num);} return((void*)0);}S10、并发程序-同步与互斥void*sub2(void*arg){inti=0,tmp;for(;i<count;i++){ pthread_mutex_lock(&mylock);
tmp=num-1; usleep(31);
num=tmp; pthread_mutex_unlock(&mylock);
printf("线程2num减1后值为:%d\n",num);}return((void*)0);}S10、并发程序-同步与互斥intmain(intargc,char**argv){
pthread_ttid1,tid2;interr,i=0,tmp;void*tret;err=pthread_create(&tid1,NULL,sub1,NULL);if(err!=0){ printf("pthread_createerror:%s\n",strerror(err));exit(-1);
}err=pthread_create(&tid2,NULL,sub2,NULL);if(err!=0){ printf("pthread_createerror:%s\n",strerror(err));exit(-1);
}S10、并发程序-同步与互斥for(;i<count;i++){ pthread_mutex_lock(&mylock);
tmp=num-1; usleep(5);
num=tmp; pthread_mutex_unlock(&mylock);
printf("mainnum减1后值为:%d\n",num);}printf("两个线程运行结束\n");
err=pthread_join(tid1,&tret);S10、并发程序-同步与互斥if(err!=0){ printf("cannotjoinwiththread1:%s\n",strerror(err));exit(-1);}printf("thread1exitcode%d\n",(int)tret);err=pthread_join(tid2,&tret);if(err!=0){ printf("cannotjoinwiththread1:%s\n",strerror(err));exit(-1);}printf("thread2exitcode%d\n",(int)tret);return0;}S10、并发程序-同步与互斥编译链接:gccthreadmutex.c-othreadmutex–lpthread运行:./threadmutex
S10、并发程序-同步与互斥2、生产者-消费者同步与互斥问题(1)无同步互斥操作程序(2)运行情况(3)有同步互斥操作程序(4)运行情况S10、并发程序-同步与互斥#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<pthread.h>#include<unistd.h>#include<signal.h>#include<semaphore.h>#defineMaxbuf10 //缓冲单元数目#defineTimesOfOp10 //生产者、消费者循环读写缓冲区的次数#definetrue1#definefalse0#definehistorynum100 //生产者、消费者读写历史记录数目S10、并发程序-同步与互斥structCirclebuf //循环缓冲队列结构{ intread; //读指针 intwrite; //写指针 intbuf[Maxbuf]; //缓冲区}circlebuf;sem_tmutex; //互斥信号量sem_tempty; //空白缓冲区同步信号量sem_tfull; //满缓冲区同步信号量charwritehistory[historynum][30];//写历史charreadhistory[historynum][30];//读历史intwritehistorycount=0; //写历史计数器intreadhistorycount=0; //读历史计数器charhistory[historynum][30]; //缓冲区操作历史inthistorycount=0; //缓冲区操作历史计数器S10、并发程序-同步与互斥voidwriteCirclebuf(structCirclebuf*circlebuf,int*value)//向缓冲区中写一个值{ circlebuf->buf[circlebuf->write]=(*value); sleep(1); circlebuf->write=(circlebuf->write+1)%Maxbuf;}intreadCirclebuf(structCirclebuf*circlebuf){ intvalue=0; value=circlebuf->buf[circlebuf->read]; sleep(1); circlebuf->buf[circlebuf->read]=0; circlebuf->read=(circlebuf->read+1)%Maxbuf; returnvalue;}S10、并发程序-同步与互斥voidsigend(intsig){ exit(0);}void*productThread(void*i){ int*n=(int*)i; intt=TimesOfOp; intwriteptr;S10、并发程序-同步与互斥 while(t--)
{ sem_wait(&empty); sem_wait(&mutex); writeCirclebuf(&circlebuf,n); if(circlebuf.write>0)writeptr=circlebuf.write-1; elsewriteptr=Maxbuf-1; sprintf(writehistory[writehistorycount++],"生产者%d:缓冲区%d=%d",*n,writeptr,*n); sprintf(history[historycount++],"生产者%d:缓冲区%d=%d\n",*n,writeptr,*n); sem_post(&mutex); sem_post(&full); sleep(1); }}S10、并发程序-同步与互斥void*consumerThread(void*i){ int*n=(int*)i; intt=TimesOfOp; intvalue=0; intreadptr; while(t--) { sem_wait(&full); sem_wait(&mutex); value=readCirclebuf(&circlebuf); if(circlebuf.read>0)readptr=circlebuf.read-1; elsereadptr=Maxbuf-1;S10、并发程序-同步与互斥 sprintf(readhistory[readhistorycount++],"消费者%d:缓冲区%d=%d\n",*n,readptr,value); sprintf(history[historycount++],"消费者%d:缓冲区%d=%d\n",*n,readptr,value); sem_post(&mutex); sem_post(&empty); sleep(1); }}intmain(){ inti,max; intConsNum=0,ProdNum=0,ret; sem_init(&mutex,0,1);S10、并发程序-同步与互斥 sem_init(&empty,0,Maxbuf); sem_init(&full,0,0); signal(SIGINT,sigend); signal(SIGTERM,sigend); circlebuf.read=circlebuf.write=0; for(i=0;i<Maxbuf;i++) circlebuf.buf[i]=0; printf("请输入生产者线程的数目:"); scanf("%d",&ProdNum); int*pro=(int*)malloc(ProdNum*sizeof(int)); pthread_t*proid=(pthread_t*)malloc(ProdNum*sizeof(pthread_t)); printf("请输入消费者线程的数目:"); scanf("%d",&ConsNum); int*con=(int*)malloc(ConsNum*sizeof(int));S10、并发程序-同步与互斥 pthread_t*conid=(pthread_t*)malloc(ConsNum*sizeof(pthread_t)); for(i=1;i<=ConsNum;i++) { con[i-1]=i; ret=pthread_create(&conid[i],NULL,consumerThread,(void*)&con[i-1]); if(ret!=0) { printf("Createthreaderror"); exit(1); } }S10、并发程序-同步与互斥 for(i=1;i<=ProdNum;i++) { pro[i-1]=i; ret=pthread_create(&proid[i],NULL,productThread,(void*)&pro[i-1]); if(ret!=0) { printf("Createthreaderror"); exit(1); } }
sleep((ConsNum+ProdNum)*10); if(writehist
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年树脂型胶粘剂合作协议书
- 2025年河北石家庄学院选聘事业单位考试试题【答案】
- 2025年喀什地区招聘中学教师考试试题【答案】
- 2025年吉林省直事业单位招聘考试试题【答案】
- 2025年克拉玛依市消防救援支队招聘政府专职消防员考试试题【答案】
- 2025年院前急救信息系统合作协议书
- 河北非营利组织免税资格认定申请表
- 毕业实习周记范文
- 2025年新型膜材料及其装置合作协议书
- 2025年电子握力计项目发展计划
- 港口装卸作业培训
- 2025年湖北省武汉市中考数学真题(无答案)
- 钳工考试试题及答案
- 2025至2030中国牙科氧化锆块行业发展趋势分析与未来投资战略咨询研究报告
- 拖欠维修费车辆以车抵债协议范本
- 2025至2030中国复印机行业发展趋势分析与未来投资战略咨询研究报告
- 暑假安全家长会4
- 2024年安徽省泗县卫生局公开招聘试题带答案
- 2025年北京市高考化学试卷真题(含答案)
- 2025年重庆市中考化学试卷真题(含标准答案)
- JG/T 202-2007工程管道用聚氨酯、蛭石绝热材料支吊架
评论
0/150
提交评论