东北大学操作系统实验报告.docx

上传人:b****6 文档编号:6051273 上传时间:2023-01-03 格式:DOCX 页数:37 大小:23.57KB
下载 相关 举报
东北大学操作系统实验报告.docx_第1页
第1页 / 共37页
东北大学操作系统实验报告.docx_第2页
第2页 / 共37页
东北大学操作系统实验报告.docx_第3页
第3页 / 共37页
东北大学操作系统实验报告.docx_第4页
第4页 / 共37页
东北大学操作系统实验报告.docx_第5页
第5页 / 共37页
点击查看更多>>
下载资源
资源描述

东北大学操作系统实验报告.docx

《东北大学操作系统实验报告.docx》由会员分享,可在线阅读,更多相关《东北大学操作系统实验报告.docx(37页珍藏版)》请在冰豆网上搜索。

东北大学操作系统实验报告.docx

东北大学操作系统实验报告

计算机科学与工程学院实验报告

实验课程名称

操作系统实验

实验成绩

专业

计算机科学与技术

班级

1507班

指导教师签字

学号

20154377

姓名

罗艺博

实验报告批改时间

实验项目目录

1.实验一熟悉Linux系统

2.实验二进程状态

3.实验三进程同步和通信

4.实验四进程的管道通信

5.实验五页面置换算法

实验报告正文

实验一熟悉Linux系统

一、实验目的

熟悉和掌握Linux系统基本命令,熟悉Linux编程环境,为以后的实验打下基础。

二、实验原理

基于linux系统的基础操作

三、实验内容(源码、注释、基础内容、扩展点等)

启动、退出、ls(显示目录内容)、cp(文件或目录的复制)、mv(文件、目录更名或移动)、rm(删除文件或目录)、mkdir(创建目录)、rmdir(删除空目录)、cd(改变工作目录)…

C语言编辑、编译

四、实验结果(截图)

lsmkdir:

cd:

rmdir:

实验二进程状态

一、实验目的

自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容、组织的变化,理解进程与其PCB间的一一对应关系。

二、实验原理

1.进程在内存中存在三种基本状态:

就绪态、执行态、阻塞态

2.三种状态在满足某种条件时会发生转换:

①就绪——》运行:

调度程序选择一个新的进程运行

②运行——》就绪:

运行进程用完了时间片

运行进程被中断,因为一高优先级进程处于就绪状态

③运行——》阻塞:

当一进程等待某一事件的发生时,如

请求系统服务;初始化I/O且必须等待结果;

无新工作可做;等待某一进程提供输入(IPC)

④阻塞——》就绪:

当所等待的事件发生时

三、实验内容(源码、注释、基础内容、扩展点等)

#include

#include

#include

#include//for"sort"invector

usingnamespacestd;

classPro//processclass

{

public:

charname;

stringstatus;

};

vectorru,re,bl;//ru->running,re->ready,bl->blocked

//functiondeclaration

inthelloUI();

intiniQ();

intshowPCB();

intruTOre();

intruTObl();

intblTOre();

intneTOre();

intruTOex();

inthelloUI()//startUI

{

cout<<"Hello!

Welcometocomeback."<

cout<<"#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#"<

cout<

cout<

return0;

}

intiniQ()//initializetheprocess

{

inti;

cout<<"Pleaseenterprocessesnamesandtheirstatus."<

cout<

for(i=0;i<5;i++)//15processatoo

{

Proprocess;

charnam;

stringsta;

cout<<"Pleaseenter"<

cin>>nam;

process.name=nam;

cout<<"Pleaseenterprocessesstatus."<

cout<<"Statuscontainsr1(running),r2(ready)andb(blocked)."<

cin>>sta;

process.status=sta;

if(sta=="r1")//judgewhichstatus

{

if(ru.size()<1)

{

ru.push_back(process);

cout<<"yes"<

}

else

cout<<"Error!

"<

}

elseif(sta=="r2")

re.push_back(process);

elseif(sta=="b")

bl.push_back(process);

else

cout<<"Error!

!

"<

cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<

cout<

}

showPCB();

return0;

}

intshowPCB()

{

inti;

cout<<"running:

";

for(i=0;i

{

cout<

}

cout<

cout<<"ready:

";

for(i=0;i

{

cout<

}

cout<

cout<<"blocked:

";

for(i=0;i

{

cout<

}

cout<

return0;

}

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());

}

else

cout<<"ErrorinruTOre"<

showPCB();

return0;

}

intruTObl()

{

if(!

ru.empty())//runningQueueisbeingused.

{

bl.push_back(ru.front());

ru.erase(ru.begin());

if(!

re.empty())

{

ru.push_back(re.front());

re.erase(re.begin());

}

else

cout<<"ErrorinruTObl1."<

}

else

cout<<"ErrorinruTObl2."<

showPCB();

return0;

}

intblTOre()

{

if(!

bl.empty())//blockedQueueisnotempty.

{

re.push_back(bl.front());

bl.erase(bl.begin());

if(ru.empty())//runningQueueisempty,thenready->running

{

ru.push_back(re.front());

re.erase(re.begin());

}

}

else

cout<<"ErrorinblTOre"<

showPCB();

return0;

}

intneTOre()

{

inti;

cout<<"Pleaseenterprocessesnamesandtheirstatus."<

cout<

Proprocess;

charnam;

stringsta;

cout<<"Pleaseenterprocessesnames."<

cin>>nam;

process.name=nam;

process.status="r2";

re.push_back(process);

cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<

cout<

if(ru.empty())

{

ru.push_back(re.front());

re.erase(re.begin());

}

showPCB();

return0;

}

intruTOex()

{

if(!

ru.empty())//runningQueueisbeingused.

{

ru.erase(ru.begin());

if(!

re.empty())

{

ru.push_back(re.front());

re.erase(re.begin());

}

else

cout<<"ErrorinruTOex1."<

}

else

cout<<"ErrorinruTOex2."<

showPCB();

return0;

}

intmain()

{

intact;//chooseaction

helloUI();

cout<<"Pleaseinitializetheprocess."<

iniQ();

while

(1)

{

cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<

cout<<"Pleaseselecttheactiontotake."<

cout<<"2:

running-->ready"<

cout<<"3:

running-->blocked"<

cout<<"4:

blocked-->ready"<

cout<<"5:

new-->ready"<

cout<<"6:

running-->exit"<

cin>>act;

if(act==2)

ruTOre();

elseif(act==3)

ruTObl();

elseif(act==4)

blTOre();

elseif(act==5)

neTOre();

elseif(act==6)

ruTOex();

else

cout<<"Errorinselect."<

cout<<"####################################"<

cout<

}

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\

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1