您好,欢迎来到尔游网。
搜索
您的当前位置:首页操作系统实验-进程同步(模拟生产者与消费者问题)

操作系统实验-进程同步(模拟生产者与消费者问题)

来源:尔游网


#include

#include

int processnum=0;

struct pcb

{

int flag;

int numlabel;

char product;

char state;

struct pcb* processlink;

}*exe=NULL,*over=NULL;

typedef struct pcb PCB;

PCB* readyhead=NULL,*readytail=NULL;

PCB* consumerhead=NULL,*consumertail=NULL;

PCB* producerhead=NULL,*producertail=NULL;

int productnum=0;

int buffersize=100;

int full=0,empty=buffersize;

char buffer[100];

int bufferpoint=0;

void linkqueue(PCB* process,PCB**tail);

PCB* getq(PCB* head,PCB**tail);

bool hasElement(PCB* pro);

void display(PCB* p);

void linklist(PCB* p,PCB* listhead);

void freelink(PCB *linkhead);

bool processproc();

bool waitempty();

bool waitfull();

void signalempty();

void signalfull();

void producerrun();

void comsuerrun();

bool hasElement(PCB* pro);

void linklist(PCB* p,PCB* listhead)

{

PCB* cursor=listhead;

while(cursor->processlink!=NULL){

cursor=cursor->processlink;

}

cursor->processlink=p;

}

void freelink(PCB* linkhead)

{

PCB* p;

while(linkhead!=NULL){

p=linkhead;

linkhead=linkhead->processlink;

free(p);

}

}

void linkqueue(PCB* process,PCB** tail)

{

if((*tail)!=NULL){

(*tail)->processlink=process;

(*tail)=process;

}

else{

printf(\"队列未初始化!\");

}

}

PCB* getq(PCB* head,PCB** tail)

{

PCB* p;

p=head->processlink;

if(p!=NULL){

head->processlink=p->processlink;

p->processlink=NULL;

if(head->processlink==NULL)

(*tail)=head;

}

else

return NULL;

return p;

}

bool processproc()

{

int i,f,num;

char ch;

PCB*p=NULL;

PCB** p1=NULL;

printf(\"\\n请输入希望产生的进程个数?\");

scanf(\"%d\

getchar();

//if(num>=100){

//printf(\"您怎么要产生这么多进程! Demands Denied!\");

//return false;

//}

for(i=0;i{

printf(\"\\n请输入您要产生的进程:输入1为生产者进程:输入2为消费者进程\\n\");

scanf(\"%d\

getchar();

p=(PCB*)malloc(sizeof(PCB));

if(!p){

printf(\"内存分配失败\");

return false;

}

p->flag=f;

processnum++;

p->numlabel=processnum;

p->state='w';

p->processlink=NULL;

if(p->flag==1){

printf(\"您要产生的进程是生产者,它是第%d个进程。请您输入您要该进程产生的字!\\n\

scanf(\"%c\

getchar();

p->product=ch;

productnum++;

printf(\"您要该进程产生的字符是%c \\n\

}

else

{

printf(\"您要产生的进程是消费者,它是第%d个进程。}

linkqueue(p,&readytail);

}

\\n\

return true;

}

bool waitempty()

{

if(empty<=0)

{

printf(\"进程%d:缓冲区存数,缓冲区慢满,该进程进入生产者等待队列\\n\

linkqueue(exe,&producertail);

return false;

}

else{

empty--;

return true;

}

}

void signalempty()

{

PCB* p;

if(hasElement(producerhead)){

p=getq(producerhead,&producertail);

linkqueue(p,&readytail);

printf(\"等待中的生产者进程进入就绪队列,它的进程号是%d\\n\

}

empty++;

}

bool waitfull()

{

if(full<=0)

{

printf(\"进程%d:缓冲区空,该进程进入消费者等待队列\\n\

linkqueue(exe,&consumertail);

return false;

}

else{

full--;

return true;

}

}

void signalfull()

{

PCB* p;

if(hasElement(consumerhead)){

p=getq(consumerhead,&consumertail);

linkqueue(p,&readytail);

printf(\"等待中的消费者进程进入就绪队列,它的进程号是%d\\n\

}

full++;

}

void producerrun()

{

if(!waitempty())

return;

printf(\"进程%d开始向缓冲区存数%c\\n\

buffer[bufferpoint]=exe->product;

bufferpoint++;

printf(\"进程%d向缓冲区存数操作结束\\n\

signalfull();

linklist(exe,over);

}

void comsuerrun()

{

if(!waitfull())

return;

printf(\"进程%d开始向缓冲区取数%c\\n\

exe->product=buffer[bufferpoint-1];

printf(\"进程%d向缓冲区取数操作结束,取数

是%c\\n\

signalempty();

linklist(exe,over);

}

void display(PCB* p)

{

p=p->processlink;

while(p!=NULL){

printf(\"进程%d,它是一个\

p->flag==1?printf(\"生产者\\n\"):printf(\"消费者\\n\");

p=p->processlink;

}

}

bool hasElement(PCB* pro)

{

if(pro->processlink==NULL)

return false;

else return true;

}

void main()

{

char terminate;

bool element;

printf(\"你想开始程序吗?(y/n)\");

scanf(\"%c\

getchar();

//Queue initialize;

readyhead=(PCB*)malloc(sizeof(PCB));

if(readyhead==NULL)return;

readytail=readyhead;

readyhead->flag=3;

readyhead->numlabel=processnum;

readyhead->state='w';

readyhead->processlink=NULL;

consumerhead=(PCB*)malloc(sizeof(PCB));

if( consumerhead==NULL)return;

consumertail=consumerhead;

consumerhead->processlink=NULL;

consumerhead->flag=4;

consumerhead->numlabel=processnum;

consumerhead->state='w';

consumerhead->processlink=NULL;

producerhead=(PCB*)malloc(sizeof(PCB));

if( producerhead==NULL)return;

producertail=producerhead;

producerhead->processlink=NULL;

producerhead->flag=5;

producerhead->numlabel=processnum;

producerhead->state='w';

producerhead->processlink=NULL;

over=(PCB*)malloc(sizeof(PCB));

if(over==NULL)return;

over->processlink=NULL;

while(terminate=='y')

{

if(!processproc())break;

element=hasElement(readyhead);

while(element){

exe=getq(readyhead,&readytail);

printf(\"进程%d申请运行,它是一个\

exe->flag==1? printf(\"生产者\\n\"):printf(\" 消费者\\n\");

if(exe->flag==1)

producerrun();

else

comsuerrun();

element=hasElement(readyhead);

}

printf(\"就绪队列没有进程\\n\");

if(hasElement(consumerhead))

{

printf(\"消费者等待队列中有进程:\\n\");

display(consumerhead);

}

else

{

printf(\"消费者等待队列中没有进程:\\n\");

}

if(hasElement(producerhead))

{

printf(\"生产者等待队列中有进程:\\n\");

display (producerhead);

}

else

{

printf(\"生产者等待队列中没有进程:\\n\");

}

printf(\"你想继续吗?(press 'y' for on)\");

scanf(\"%c\

getchar();

}

printf(\"\\n\\n 进程模式完成.\\n\");

//Free the room;

freelink(over);

over=NULL;

freelink(readyhead);

readyhead=NULL;

readytail=NULL;

freelink(consumerhead);

consumerhead=NULL;

consumertail=NULL;

freelink(producerhead);

producerhead=NULL;

producertail=NULL;

getchar();

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- axer.cn 版权所有 湘ICP备2023022495号-12

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务