资源描述:
《操作系统读者写者问题 源代码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、在linux环境下运行!#include#include#include#include#include#include#includeunionsemun{intval;structsemid_ds*buf;unsignedshort*array;structseminfo*_buf;};int*readcount;staticintshmid,semid,s;intmain(){ke
2、y_tsemkey,shmkey,skey;unionsemunarg;if((semkey=ftok(".",0x16))==-1)printf("error1");if((skey=ftok(".",0x36))==-1)printf("error3");if((shmid=shmget(IPC_PRIVATE,1024,0600
3、IPC_CREAT))==-1)printf("error4");if((shmat(shmid,NULL,0))==-1)printf("error!");readcount=(i
4、nt*)shmat(shmid,NULL,0);semid=semget(semkey,1,0600
5、IPC_CREAT);s=semget(skey,1,0600
6、IPC_CREAT);arg.val=1;semctl(semid,0,SETVAL,arg);semctl(shmid,0,SETVAL,arg);semctl(s,0,SETVAL,arg);intinitsem(intkey){return(semget(key,1,0600
7、IPC_CREAT
8、IPC_EXCL));}intP(intsemidd,intsem
9、nun){structsembufsops={semnun,-1,SEM_UNDO};return(semop(semidd,&sops,1));}intV(intsemidd,intsemnun){structsembufsops={semnun,+1,SEM_UNDO};return(semop(semidd,&sops,1));}voidwriter(intk){while(k>0){P(s,0);P(semid,0);if(*readcount==0)P(shmid,0);(*readcount)++;V(semid,0)
10、;V(s,0);printf("XIEjingc");P(semid,0);(*readcount)--;;if((*readcount)==0)V(shmid,0);V(semid,0);k--;}}voidreader(intj){while(j>0){P(s,0);P(shmid,0);printf("DUjingc");V(shmid,0);V(s,0);j--;}}writer(5);reader(6);return0;}