}
return0;
}
拓展点:
五状态模型
四、实验结果(截图)
创建进程:
状态running?
ready:
状态running?
blocked:
状态blocked?
ready:
创建新进程:
情况一有进程正在运行
情况二无进程正在运行
终止进程:
实验三进程同步和通信
一、实验目的
调试、修改、运行模拟程序,通过形象化的状态显示,使学生理解进程的概念,了解同步和通信的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。
通过补充新功能,使学生能灵活运用相关知识,培养创新能力。
二、实验原理
假定.缓冲区可以容纳8个数据;
因为缓冲区是有限的,因此当其满了时生产者进程应该等待;当消费者取走一个数据后,应唤醒正在等待的生产者进程;
当缓冲区空时,消费者进程应该等待;当生产者向缓冲区放入了一个数据时,应唤醒正在等待的消费者进程。
这就是生产者和消费者之间的同步
三、实验内容(源码、注释、基础内容、扩展点等)
基础内容:
编写程序使其模拟两个进程,即生产者(producer)进程和消费者(Consumer)进程工作;生产者每次产生一个数据,送入缓冲区中;消费者每次从缓冲区中取走一个数据。
每次写入和读出数据时,都将读和写指针加一。
当指针到达缓冲区尾,重新将指针退回起点;
/***************************************************************/
/*PROGRAMNAME:
PRODUCER_CONSUMER*/
/*Thisprogramsimulatestwoprocesses,producerwhich*/
/*continuestoproducemessageandputitintoabuffer*/
/*[implementedbyPIPE],andconsumerwhichcontinuestoget*/
/*messagefromthebufferanduseit.*/
/*Theprogramalsodemonstratesthesynchronismbetween*/
/*processesandusesofPIPE.*/
/***************************************************************/
#include
#include
//#include
#definePIPESIZE8
#definePRODUCER0
#defineCONSUMER1
#defineRUN0/*statuofprocess*/
#defineWAIT1/*statuofprocess*/
#defineREADY2/*statuofprocess*/
#defineNORMAL0
#defineSLEEP1
#defineAWAKE2
#include
//ProcessControlblock
structpcb{char*name;
intstatu;
inttime;
};/*timesofexecution*/
//Buffer
structpipetype{chartype;//type
intwriteptr;//Writepointer
intreadptr;//Readpointer
structpcb*pointp;/*writewaitpoint*/
structpcb*pointc;};/*readwaitpoint*/
intpipe[PIPESIZE];//Bufferarray
structpipetypepipetb;
structpcbprocess[2];
//numberofproducer-numberofconsumer,buffer.count>=8:
toomanyprodecers;<=0:
toomanyconsumers
intcount=0;
intcountp=0,countr=0;
main()
{
intoutput,ret,i;//output->numberoftimes,ret->presentstatus
charin[2];
intrunp(),runc(),prn();
pipetb.type='c';pipetb.writeptr=0;pipetb.readptr=0;
pipetb.pointp=pipetb.pointc=NULL;
process[PRODUCER].name="Producer\0";
process[CONSUMER].name="Consumer\0";
process[PRODUCER].statu=process[CONSUMER].statu=READY;
process[PRODUCER].time=process[CONSUMER].time=0;
output=0;
printf("Nowstartingtheprogram!
\n");
printf("Press'p1'torunPRODUCER1,press'p2'torunPRODUCER2.\npress'c'torunCONSUMER.\n");
//PRODUCER1->product1newdata,PRODUCER2->product2newdata
printf("Press'e'toexitfromtheprogram.\n");
for(i=0;i<1000;i++)
{
in[0]='N';
while(in[0]=='N')
{
scanf("%s",in);
if(in[0]!
='e'&&in[0]!
='p'&&in[0]!
='c')//whennotp,c,econtinue
in[0]='N';
}
if(in[0]=='p'&&process[PRODUCER].statu==READY)//producerandready
{
if(in[1]=='2')//producer2
{
intm;
for(m=0;m<2;m++)
{
if(countp>3)//thenumberofwaittingproducerover4,waitqueue=4
{
printf("wrong!
!
!
\n");
continue;
}
if(count<8)
{
//output=rand()%99+1;//0~99
output=(output+1)%100;
}
if((ret=runp(output,process,pipe,&pipetb,PRODUCER))==SLEEP)//sleep,setwritewaitpointer
{
pipetb.pointp=&process[PRODUCER];
}
if(ret==AWAKE)//awake,executeconsumer
{
(pipetb.pointc)->statu=READY;
runc(process,pipe,&pipetb,CONSUMER);
countr--;
printf("countr=%d\n",countr);
if(countr==0)pipetb.pointc=NULL;
}
}
}
elseif(in[1]=='1')//producer1
{
if(countp>3)
{
printf("wrong!
!
!
\n");
continue;
}
if(count<8)
{
//output=rand()%99+1;//0~99
output=(output+1)%100;
}
if((ret=runp(output,process,pipe,&pipetb,PRODUCER))==SLEEP)//sleep
{
pipetb.pointp=&process[PRODUCER];
}
if(ret==AWAKE)//awake
{
(pipetb.pointc)->statu=READY;
runc(process,pipe,&pipetb,CONSUMER);
countr--;
printf("countr=%d\n",countr);
if(countr==0)pipetb.pointc=NULL;
}
}
}
if(in[0]=='c'&&process[CONSUMER].statu==READY)//consumerandready
{
if((ret=runc(process,pipe,&pipetb,CONSUMER))==SLEEP)//sleep
{
pipetb.pointc=&process[CONSUMER];
}
if(ret==AWAKE)//awake
{
(pipetb.pointp)->statu=READY;
output=(output+1)%100;
runp(output,process,pipe,&pipetb,PRODUCER);
countp--;
printf("countp=%d\n",countp);
if(countp==0)pipetb.pointp=NULL;
}
}
if(in[0]=='p'&&process[PRODUCER].statu==WAIT)//producerandwait
{
if(in[1]=='2')//producer2
{
intm;
for(m=0;m<2;m++)
{
if(countp>3)
{
printf("wrong!
!
!
\n");
printf("PRODUCERiswaiting,can'tbescheduled.\n");
continue;
}
countp++;
printf("countp=%d\n",countp);
}
}
elseif(in[1]=='1')//producer1
{
if(countp>3)
{
printf("wrong!
!
!
\n");
printf("PRODUCERiswaiting,can'tbescheduled.\n");
continue;
}
countp++;
printf("countp=%d\