1、 为简单起见,每次产生的数据为0-99的整数,从0开始,顺序递增。两个进程的调度是通过运行者使用键盘来实现的。4. 程序使用的数据结构进程控制块:包括进程名,进程状态和执行次数。缓冲区:一个整数数组。缓冲区说明块:包括类型,读指针,写指针,读等待指针和写等待指针。5. 程序使用说明启动程序后,如果使用p键则运行一次生产者进程,使用c键则运行一次消费者进程。通过屏幕可以观察到两个进程的状态和缓冲区变化的情况。6.实验流程图源程序#includestdlib.hstring.h#define PIPESIZE 8enum Status RUN, WAIT, READY ;#define NORMA
2、L 0#define SLEEP 1#define AWAKE 2struct PCB char name3; enum Status status; int time;struct waitqueue struct PCB pcb; struct waitqueue *next;struct PCB *producer; /* write wait point */struct PCB *consumer; /* read wait point */struct waitqueue *headerqueue, *tailqueue;int writeptr;int readptr;int w
3、ritein, readout;int bufferPIPESIZE;int empty, full;void runp(char in3), runc(char in3), print();int main() char in3; writeptr = 0; readptr = 0; writein = 0; empty = PIPESIZE; full = 0; producer = (struct PCB *)malloc(sizeof(struct PCB); consumer = (struct PCB *)malloc(sizeof(struct PCB); headerqueue
4、 = (struct waitqueue *)malloc(sizeof(struct waitqueue); headerqueue-next = NULL; tailqueue = headerqueue; producer-status = READY; consumer-status = WAIT;time = consumer-time = 0; printf(Now starting the program!n);Press p1 to run PRODUCER1,Press to run PRODUCER2 nc1 to run CONSUMER1,Press c2 to run
5、 CONSUMER2 ne to exit from the program.n while (1) strcpy(in, N while (!strcmp(in, ) printf( scanf(%s, in); if (strcmp(in, e) & strcmp(in, p1p2c1c2 printf(error,please input again! strcpy(in, if ( = in0) runp(in); producer-time+; else if ( runc(in); consumer- else /printf(PRODUCER product %d timesn,
6、 producer-time);CONSUMER consumer %d timesn, consumer- exit(0); print(); printf( void runp(char in3) if (full = 8) struct waitqueue *search; search = headerqueue-next; while (search != NULL) if (!strcmp(in, search-pcb.name)error! exit(1); search = search- producer-PRODUCER %s process is waiting, can
7、t be scheduled.n struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue); strcpy(p-pcb.name, in); p-pcb.status = WAIT;pcb.time = producer-time + 1; tailqueue-next = p; tailqueue = p; full+; else writein = (writein + 1) % 100;status = RUN;run PRODUCER %s process . product %d , in, w
8、ritein); bufferwriteptr = writein; if (empty8) struct waitqueue *p; p = headerqueue-run CONSUMER %s process. use %d, p-pcb.name, bufferwriteptr); if (tailqueue = p) tailqueue = headerqueue; headerqueue-next = p- free(p); empty-; if (writeptr readptr) writeptr+; if (writeptr = PIPESIZE) writeptr = 0;
9、 if (readptr = 0) producer- else producer- else if (writeptr = readptr) producer- else producer- full+;void runc(char in3) if (empty consumer-CONSUMER %s is waiting, can,in);pcb.time = consumer- empty+; readout = bufferreadptr;run CONSUMER %s process. use %d , in, readout); if (full writein = (write
10、in + 1) % 100; bufferwriteptr = writein;run PRODUCER %s process. product %d full-; writeptr+; readptr+; if (readptr writeptr) readptr+; if (readptr readptr = 0; if (writeptr = 0) consumer- else else consumer- if (readptr = writeptr) consumer- writeptr = readptr = 0; producer- empty+;void print() int
11、 i = 0; int j = 0; int low = 0; int high = 0; for (i; i PIPESIZE; i+)- if (readptr for (low = 0; low else if (producer-status = WAIT) for (i = 0; if (producer-status = WAIT & NULL !=headerqueue-next) struct waitqueue *p = headerqueue- while (NULL != p) PRODUCER %s process are waittingnpcb.name); p = p-PRODUCER ready n if (consumer-headerqueue- CONSUMER %s process are waittingnCONSUMER readyn 实验结果: