东北大学操作系统实验报告Word文档下载推荐.docx
《东北大学操作系统实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《东北大学操作系统实验报告Word文档下载推荐.docx(37页珍藏版)》请在冰豆网上搜索。
调度程序选择一个新的进程运行
②运行——》就绪:
运行进程用完了时间片
运行进程被中断,因为一高优先级进程处于就绪状态
③运行——》阻塞:
当一进程等待某一事件的发生时,如
请求系统服务;
初始化I/O且必须等待结果;
无新工作可做;
等待某一进程提供输入(IPC)
④阻塞——》就绪:
当所等待的事件发生时
#include<
iostream>
vector>
stdlib.h>
algorithm>
//for"
sort"
invector
usingnamespacestd;
classPro//processclass
{
public:
charname;
stringstatus;
};
vector<
Pro>
ru,re,bl;
//ru->
running,re->
ready,bl->
blocked
//functiondeclaration
inthelloUI();
intiniQ();
intshowPCB();
intruTOre();
intruTObl();
intblTOre();
intneTOre();
intruTOex();
inthelloUI()//startUI
cout<
<
"
Hello!
Welcometocomeback."
<
endl;
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#"
return0;
}
intiniQ()//initializetheprocess
inti;
Pleaseenterprocessesnamesandtheirstatus."
for(i=0;
i<
5;
i++)//15processatoo
{
Proprocess;
charnam;
stringsta;
Pleaseenter"
i<
"
processesnames."
cin>
>
nam;
process.name=nam;
Pleaseenterprocessesstatus."
Statuscontainsr1(running),r2(ready)andb(blocked)."
sta;
process.status=sta;
if(sta=="
r1"
)//judgewhichstatus
if(ru.size()<
1)
ru.push_back(process);
yes"
}
else
Error!
elseif(sta=="
r2"
)
re.push_back(process);
b"
bl.push_back(process);
!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
showPCB();
intshowPCB()
running:
;
ru.size();
i++)
ru[i].name<
"
ready:
re.size();
re[i].name<
blocked:
bl.size();
bl[i].name<
intruTOre()
if(!
ru.empty())//runningQueueisbeingused.
re.push_back(ru.front());
//running'
sfirstprocessgotoready'
last
ru.erase(ru.begin());
//deleterunning'
sfirstprocess
ru.push_back(re.front());
re.erase(re.begin());
ErrorinruTOre"
intruTObl()
bl.push_back(ru.front());
re.empty())
ErrorinruTObl1."
ErrorinruTObl2."
intblTOre()
bl.empty())//blockedQueueisnotempty.
re.push_back(bl.front());
bl.erase(bl.begin());
if(ru.empty())//runningQueueisempty,thenready->
running
ErrorinblTOre"
intneTOre()
Pleaseenterprocessesnames."
process.status="
if(ru.empty())
intruTOex()
ErrorinruTOex1."
ErrorinruTOex2."
intmain()
intact;
//chooseaction
helloUI();
Pleaseinitializetheprocess."
iniQ();
while
(1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
Pleaseselecttheactiontotake."
2:
running-->
ready"
3:
blocked"
4:
blocked-->
5:
new-->
6:
exit"
act;
if(act==2)
ruTOre();
elseif(act==3)
ruTObl();
elseif(act==4)
blTOre();
elseif(act==5)
neTOre();
elseif(act==6)
ruTOex();
Errorinselect."
endl;
####################################"
拓展点:
五状态模型
创建进程:
状态running✍ready:
状态running✍blocked:
状态blocked✍ready:
创建新进程:
情况一有进程正在运行
情况二无进程正在运行
终止进程:
实验三进程同步和通信
一、实验目的
调试、修改、运行模拟程序,通过形象化的状态显示,使学生理解进程的概念,了解同步和通信的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。
通过补充新功能,使学生能灵活运用相关知识,培养创新能力。
假定.缓冲区可以容纳8个数据;
因为缓冲区是有限的,因此当其满了时生产者进程应该等待;
当消费者取走一个数据后,应唤醒正在等待的生产者进程;
当缓冲区空时,消费者进程应该等待;
当生产者向缓冲区放入了一个数据时,应唤醒正在等待的消费者进程。
这就是生产者和消费者之间的同步
基础内容:
编写程序使其模拟两个进程,即生产者(producer)进程和消费者(Consumer)进程工作;
生产者每次产生一个数据,送入缓冲区中;
消费者每次从缓冲区中取走一个数据。
每次写入和读出数据时,都将读和写指针加一。
当指针到达缓冲区尾,重新将指针退回起点;
/***************************************************************/
/*PROGRAMNAME:
PRODUCER_CONSUMER*/
/*Thisprogramsimulatestwoprocesses,producerwhich*/
/*continuestoproducemessageandputitintoabuffer*/
/*[implementedbyPIPE],andconsumerwhichcontinuestoget*/
/*messagefromthebufferanduseit.*/
/*Theprogramalsodemonstratesthesynchronismbetween*/
/*processesandusesofPIPE.*/
stdio.h>
//#include<
time.h>
#definePIPESIZE8
#definePRODUCER0
#defineCONSUMER1
#defineRUN0/*statuofprocess*/
#defineWAIT1/*statuofprocess*/
#defineREADY2/*statuofprocess*/
#defineNORMAL0
#defineSLEEP1
#defineAWAKE2
//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"
);
Press'
p1'
torunPRODUCER1,press'
p2'
torunPRODUCER2.\npress'
torunCONSUMER.\n"
//PRODUCER1->
product1newdata,PRODUCER2->
product2newdata
e'
toexitfromtheprogram.\n"
i<
1000;
i++)
in[0]='
N'
while(in[0]=='
{
scanf("
%s"
in);
if(in[0]!
='
&
in[0]!
p'
)//whennotp,c,econtinue
in[0]='
}
if(in[0]=='
process[PRODUCER].statu==READY)//producerandready
if(in[1]=='
2'
)//producer2
intm;
for(m=0;
m<
2;
m++)
if(countp>
3)//thenumberofwaittingproducerover4,waitqueue=4
wrong!
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--;
countr=%d\n"
countr);
if(countr==0)pipetb.pointc=NULL;
elseif(in[1]=='
1'
)//producer1
3)
pipetb,PRODUCER))==SLEEP)//sleep
if(ret==AWAKE)//awake
if(countr==0)pipetb.pointc=NULL;
if(in[0]=='
process[CONSUMER].statu==READY)//consumerandready
if((ret=runc(process,pipe,&
pipetb,CONSUMER))==SLEEP)//sleep
{
pipetb.pointc=&
process[CONSUMER];
}
(pipetb.pointp)->
output=(output+1)%100;
runp(output,process,pipe,&
pipetb,PRODUCER);
countp--;
printf("
countp=%d\n"
countp);
if(countp==0)pipetb.pointp=NULL;
process[PRODUCER].statu==WAIT)//producerandwait
PRODUCERiswaiting,can'
tbescheduled.\n"
countp++;
countp=%d\