-写者问题的实现源程序_GAOQS.COM.doc

上传人:心*** 文档编号:1039581 上传时间:2018-11-22 格式:DOC 页数:8 大小:8.97KB
下载 相关 举报
-写者问题的实现源程序_GAOQS.COM.doc_第1页
第1页 / 共8页
-写者问题的实现源程序_GAOQS.COM.doc_第2页
第2页 / 共8页
-写者问题的实现源程序_GAOQS.COM.doc_第3页
第3页 / 共8页
-写者问题的实现源程序_GAOQS.COM.doc_第4页
第4页 / 共8页
-写者问题的实现源程序_GAOQS.COM.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、1. /*/#include #include #include #include #include #define RWSEMKEY 75#define WWSEMKEY 90#define RSEM 78#define WSEM 79#define ZZ 80#define SEMMNI 128#define SHMKEY 166int shmid;int semid_rw;int semid_ww;int rsem;int wsem;int zz;int flags=0666;struct sembuf pbuf;struct sembuf vbuf;float w_time;float

2、 r_time;/*/struct MSGint proid;char decr;int w_time;int r_time;int *num_r;int *num_w;struct MSG msg20;void Read(struct MSG msg1)sleep(msg1.w_time);FILE *fpw=fopen(“out“,“ab“);/* printf(“the %ds (reading) progress is created!n“,msg1.proid); */* fprintf(fpw,“the %ds(reading) progress is created!n“,msg

3、1.proid);*/fclose(fpw);pbuf.sem_num=0;pbuf.sem_op=-1;pbuf.sem_flg=0;vbuf.sem_num=0;vbuf.sem_op=1;vbuf.sem_flg=0;shmid=shmget(SHMKEY,20,flags|IPC_EXCL);num_r=(int *)shmat(shmid,0,0);num_w=(int*)(num_r+8);semid_rw=semget(RWSEMKEY,1,flags|IPC_EXCL);semid_ww=semget(WWSEMKEY,1,flags|IPC_EXCL);rsem=semget

4、(RSEM,1,flags|IPC_EXCL);wsem=semget(WSEM,1,flags|IPC_EXCL);zz=semget(ZZ,1,flags|IPC_EXCL);semop(zz,semop(rsem,semop(semid_rw,(*num_r)+;if(*num_r=1)semop(wsem,semop(semid_rw,semop(rsem,semop(zz,fpw=fopen(“out“,“ab“);printf(“the %ds progress is reading!n“,msg1.proid);fprintf(fpw,“the %ds progress is r

5、eading!n“,msg1.proid);fclose(fpw);sleep(msg1.r_time);fpw=fopen(“out“,“ab“);printf(“the %ds progresss reading is end!n“,msg1.proid);fprintf(fpw,“the %ds progresss reading is end!n“,msg1.proid);fclose(fpw);semop(semid_rw,(*num_r)-;if(*num_r=0)semop(wsem,semop(semid_rw,void Write(struct MSG msg1)sleep(

6、msg1.w_time);shmid=shmget(SHMKEY,20,flags|IPC_EXCL);num_r=(int *)shmat(shmid,0,0);num_w=(int*)(num_r+8);FILE *fpw=fopen(“out“,“ab“);/* printf(“the %ds progress (writing) is created!n“,msg1.proid); */* fprintf(fpw,“the %ds progress (writing) is created!n“,msg1.proid);*/fclose(fpw);pbuf.sem_num=0;pbuf

7、.sem_op=-1;pbuf.sem_flg=0;vbuf.sem_num=0;vbuf.sem_op=1;vbuf.sem_flg=0;semid_rw=semget(RWSEMKEY,1,flags|IPC_EXCL);semid_ww=semget(WWSEMKEY,1,flags|IPC_EXCL);rsem=semget(RSEM,1,flags|IPC_EXCL);wsem=semget(WSEM,1,flags|IPC_EXCL);semop(semid_ww,(*num_w)+;if(*num_w=1)semop(rsem,semop(semid_ww,semop(wsem,

8、fpw=fopen(“out“,“ab“);printf(“the %ds progress is wrinting!n“,msg1.proid);fprintf(fpw,“the %ds progress is wrinting!n“,msg1.proid);fclose(fpw);sleep(msg1.r_time);fpw=fopen(“out“,“ab“);printf(“the %ds progresss wrinting is end!n“,msg1.proid);fprintf(fpw,“the %ds progresss wrinting is end!n“,msg1.proi

9、d);fclose(fpw);semop(wsem,semop(semid_ww,(*num_w)-;if(*num_w=0)semop(rsem,semop(semid_ww,int main()int i=0;int num;int j;float w_time;float r_time;FILE *fp=fopen(“input.txt“,“rb“);FILE *fpw=fopen(“out“,“wb+“);if(fp=NULL)printf(“Canot open the inputfile!n“);fprintf(fpw,“Canot open the inputfile!n“);f

10、close(fpw);exit(0);while(!feof(fp)fscanf(fp,“%d %c %f %f“,msgi.w_time=(int)w_time;msgi.r_time=(int)r_time;i+;fclose(fp);shmid=shmget(SHMKEY,20,flags|IPC_CREAT);num_r=(int *)shmat(shmid,0,0);num_w=(int *)(num_r+8);* num_r=0;* num_w=0;semid_rw=semget(RWSEMKEY,1,flags|IPC_CREAT);semid_ww=semget(WWSEMKE

11、Y,1,flags|IPC_CREAT);rsem=semget(RSEM,1,flags|IPC_CREAT);wsem=semget(WSEM,1,flags|IPC_CREAT);zz=semget(ZZ,1,flags|IPC_CREAT);semctl(semid_rw,0,SETVAL,1);semctl(semid_ww,0,SETVAL,1);semctl(rsem,0,SETVAL,1);semctl(wsem,0,SETVAL,1);semctl(zz,0,SETVAL,1);num=i;for(i=0;i#include #include #include #includ

12、e #define RWSEMKEY 75#define WWSEMKEY 90#define SEMMNI 128#define SHMKEY 166int shmid;int semid_rw;int semid_ww;int flags=0666;int *fpw;struct sembuf pbuf;struct sembuf vbuf;/*/*/struct MSGint proid;/*/*/char decr;/*)*/int w_time;/* */int r_time; /*/;int *num_r;int *num_w;struct MSG msg20;void Read(

13、struct MSG msg1)sleep(msg1.w_time);FILE *fpw=fopen(“out“,“ab“);/* printf(“the %ds (reading) progress is created!n“,msg1.proid);*/* fprintf(fpw,“the %ds(reading) progress is created!n“,msg1.proid);*/fclose(fpw);pbuf.sem_num=0;pbuf.sem_op=-1;pbuf.sem_flg=0;vbuf.sem_num=0;vbuf.sem_op=1;vbuf.sem_flg=0;s

14、hmid=shmget(SHMKEY,20,flags|IPC_EXCL);num_r=(int *)shmat(shmid,0,0);num_w=(int*)(num_r+8);semid_rw=semget(RWSEMKEY,1,flags|IPC_EXCL);semid_ww=semget(WWSEMKEY,1,flags|IPC_EXCL);semop(semid_rw,if(*num_r=0)semop(semid_ww,(*num_r)+;semop(semid_rw,fpw=fopen(“out“,“ab“);printf(“the %ds progress is reading

15、!n“,msg1.proid);fprintf(fpw,“the %ds progress is reading!n“,msg1.proid);fclose(fpw);sleep(msg1.r_time);fpw=fopen(“out“,“ab“);printf(“the %ds progresss reading is end!n“,msg1.proid);fprintf(fpw,“the %ds progresss reading is end!n“,msg1.proid);fclose(fpw);semop(semid_rw,(*num_r)-;if(*num_r=0)semop(sem

16、id_ww,semop(semid_rw,void Write(struct MSG msg1)sleep(msg1.w_time);FILE *fpw=fopen(“out“,“ab“);/* printf(“the %ds progress (writing) is created!n“,msg1.proid);*/* fprintf(fpw,“the %ds progress (writing) is created!n“,msg1.proid);*/fclose(fpw);pbuf.sem_num=0;pbuf.sem_op=-1;pbuf.sem_flg=0;vbuf.sem_num

17、=0;vbuf.sem_op=1;vbuf.sem_flg=0;semid_ww=semget(WWSEMKEY,1,flags|IPC_EXCL);semop(semid_ww,fpw=fopen(“out“,“ab“);printf(“the %ds progress is wrinting!n“,msg1.proid);fprintf(fpw,“the %ds progress is wrinting!n“,msg1.proid);fclose(fpw);sleep(msg1.r_time);fpw=fopen(“out“,“ab“);printf(“the %ds progresss

18、wrinting is end!n“,msg1.proid);fprintf(fpw,“the %ds progresss wrinting is end!n“,msg1.proid);fclose(fpw);semop(semid_ww,int main()int i=0;int num;int j;float w_time;float r_time;FILE *fp=fopen(“input.txt“,“rb“);FILE *fpw=fopen(“out“,“wb+“);if(fp=NULL)printf(“Canot open the inputfile!n“);fprintf(fpw,

19、“Canot open the inputfile!n“);fclose(fpw);exit(0);while(!feof(fp)fscanf(fp,“%d %c %f %f“,msgi.w_time=(int)w_time;msgi.r_time=(int)r_time;i+;fclose(fp);shmid=shmget(SHMKEY,20,flags|IPC_CREAT);num_r=(int *)shmat(shmid,0,0);num_w=(int *)(num_r+8);* num_r=0;* num_w=0;semid_rw=semget(RWSEMKEY,1,flags|IPC

20、_CREAT);semid_ww=semget(WWSEMKEY,1,flags|IPC_CREAT);semctl(semid_rw,0,SETVAL,1);semctl(semid_ww,0,SETVAL,1);num=i;for(i=0;inum;i+)while(j=fork()=-1);if(!j)if(msgi.decr=W)Write(msgi);else if(msgi.decr=R)Read(msgi);elseprintf(“n“);return 1;exit(0);for(i=0;inum;i+)wait();semctl(semid_rw,IPC_RMID,0);semctl(semid_ww,IPC_RMID,0);shmctl(shmid,IPC_RMID,0);return 1;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。