if(rd==1){ p=ready; ready=ready->next; ready->next=p; p->next=NULL; strcpy(ready->state,\ strcpy(ready->next->state,\ } else strcpy(ready->state,\ pc=ready->breakp; }
void processor(){ //模拟处理器指令执行
if(strcmp(ready->name,\当前进程为生产者 switch(pc) {
case 0://produce printf(\生产者生产了字符%c\\n\ rec_p[rp1]=str[sp];//添加到生产记录 sp=(sp+1)%len; pc++;
ready->breakp=pc; //保存断点 break; case 1: //p(s1) pc++; p(1); break; case 2: //put buffer[in]=rec_p[rp1]; //放到缓冲区 printf(\字符成功入驻空缓存!\\n\ rp1++; in=(in+1)%BUF; pc++; ready->breakp=pc; //保存断点 break; case 3: //v(s2) pc++; printf(\释放一个s2信号\\n\ v(2); break;
case 4://goto01 printf(\生产进程goto 0 操作\\n\ pc=0;
count--; //剩余字符个数减1
printf(\剩余字符count=%d个\\n\ready->breakp=pc; //保存断点 if(count<=0){ //生产结束 printf(\生产者结束生产!\\n\ strcpy(p1->state,\ strcpy(p1->reason,\ ready->breakp=-1;
ready=ready->next;//在就绪队列中去掉
} }
else //当前进程为消费者 switch(pc) { case 0: //p(s2) pc++; p(2); break; case 1: //get
printf(\消费者取字符!\\n\temp=buffer[out]; out=(out+1)%BUF; pc++;
ready->breakp=pc; //保存断点 break; case 2: //v(s1) pc++; printf(\释放一个s1\\n\ v(1); break;
case 3: //consume
printf(\消费了字符%c\\n\rec_c[rp2]=temp;//添加到消费记录 rp2++; con_cnt++;
if(con_cnt>=len){ strcpy(c1->state,\完成态 c1->breakp=-1; return; } pc++;
ready->breakp=pc; //保存断点 break;
case 4: //goto0
printf(\消费进程goto 0 操作\\n\ pc=0; ready->breakp=pc; //保存断点 } }
void print(){ int i,j;
printf(\生产者消费者模拟-------\\n\ printf(\模拟过程的字符串为:\\t\ printf(\
printf(\已生产:\ for(j=0;j<=rp1;j++) printf(\ printf(\空缓存:\ for(j=rp2;j<=rp1;j++) printf(\ printf(\已消费:\ for(j=0;j<=rp2;j++) printf(\
printf(\进程控制块的信息--------\\n\ printf(\进程名\\t\\t状态\\t等待原因\\t断点\\n\
printf(\ printf(\ printf(\ printf(\继续 0.退出\\n\ scanf(\ if(i==0){ exit(0); }
} 主程序
#include \#include \#include \void main(){ printf(\生产者消费者模拟\\n\ printf(\ printf(\请输入字符串:\\n\ scanf(\ //string数组存放将要产生的字符 len=strlen(str); count=len; //输入字符的个数
}
init(); //初始化