#include \"stdafx.h\"
#include extern int s = 3;// 这个是信号量的设置无所谓了 typedef struct PCB { int num; struct PCB *next; }PCB,*ready,*stuck; void Init(PCB *p) { p = (PCB*)malloc(sizeof(PCB)); p->next = NULL; } ready e; //这里我是没办法才这么写的 stuck k; // 主要是这两个队列的头指针该如何解决!!!!!!!!!!!!!!!!! void AddR(int ip) { if(e->next == NULL) //就在这开始崩溃,后面没运行呢 { PCB *t; t = (PCB*)malloc(sizeof(PCB)); //如果就绪为空直接插入 t->num = ip; t->next = NULL; e->next = t; } else { PCB *temp = (PCB*)malloc(sizeof(PCB)); temp->num = ip; //不为空...头插法 后面的加入到阻塞队列的操作和这个类似就不鳌述了 temp->next = e->next; e->next = temp; } } void weakup(int ip)//这个是唤醒操作将阻塞队列的一个置到就绪队列 { PCB *r = k; PCB *t; t = r; while(r->num != ip)//在阻塞队列里找匹配的进程 { t = r; r = r->next; } t->next = r->next; //从这个队列中出去要找的进程 if(e->next == NULL) //如果队列为空直接插进去(就绪队列) { e->next = r; e->next = NULL; } else //否则...头插法 { r->next = e->next; e->next = r; } } void AddS(int ip) //这个是把进程加入到阻塞队列 { PCB *r = k; if(r->next == NULL) { PCB *t; t = (PCB*)malloc(sizeof(PCB)); t->num = ip; t->next = NULL; r->next = t; } else { PCB *temp = (PCB*)malloc(sizeof(PCB)); temp->num = ip; temp->next = r->next; r->next = temp; } } void P() //p操作 { int ip; cout <<\"请输入要申请的进程号:\"; cin>>ip; s--; if(s >= 0) { cout <<\"分配成功\" < void V() //V操作 { int ip; cout <<\"请输入要释放的进程号:\"; cin>>ip; s++; if(s <= 0) { weakup(ip); } else { if(e->next ==NULL) cout <<\"无法释放,就绪队列为空!\" < t = t->next; while(t->num != ip); { pre = t; t = t->next; } pre->next = t->next; t->next = NULL; free(t); cout <<\"进程号为\" < int main() { Init(e); Init(k); int con; cout < while(con) { switch(con){ case 1: P(); break; case 2: V(); break; } } return 0; } #include do{*mutex -= 1;} while(*mutex>=0); } v(int *mutex) { do{*mutex += 1;} while(*mutex<=10); } producer(int *empty,int *full) { p(empty); printf(\"生产一个产品\\n\"); v(full); printf(\"%d\\n\} consumer(int *empty,int *full) { p(full); printf(\"消耗一个产品\\n\"); v(empty); printf(\"%d\\n\} int main() {int empty=10,full=0; printf(\"实现pv操作\\n\"); return 0; } 因篇幅问题不能全部显示,请点此查看更多更全内容