《Linux应用实例教程》课件-讲义29并发程序-同步与互斥_第1页
《Linux应用实例教程》课件-讲义29并发程序-同步与互斥_第2页
《Linux应用实例教程》课件-讲义29并发程序-同步与互斥_第3页
《Linux应用实例教程》课件-讲义29并发程序-同步与互斥_第4页
《Linux应用实例教程》课件-讲义29并发程序-同步与互斥_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

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

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论