东北大学操作系统实验之Linux及进程状态.docx
《东北大学操作系统实验之Linux及进程状态.docx》由会员分享,可在线阅读,更多相关《东北大学操作系统实验之Linux及进程状态.docx(15页珍藏版)》请在冰豆网上搜索。
东北大学操作系统实验之Linux及进程状态
操作系统实验报告一
实验一进程状态转换及其PCB的变化
一、实验目的:
自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容、组织的变化,理解进程与其PCB间的一一对应关系。
二、实验内容及要求:
(1)、设计并实现一个模拟进程状态转换及其相应PCB内容、组织结构变化的程序。
(2)、独立编写、调试程序。
进程的数目、进程的状态模型(三状态、五状态、七状态或其它)以及PCB的组织形式可自行选择。
(3)、合理设计与进程PCB相对应的数据结构。
PCB的内容要涵盖进程的基本信息、控制信息、资源需求及现场信息。
(4)、设计出可视性较好的界面,应能反映出进程状态的变化引起的对应PCB内容、组织结构的变化。
(5)、代码书写要规范,要适当地加入注释。
(6)、鼓励在实验中加入新的观点或想法,并加以实现。
(7)、认真进行预习,完成预习报告。
(8)、实验完成后,要认真总结,完成实验报告。
五状态模型:
3、主要数据结构:
structRun_type//运行态进程
{
charname;
intstate;
intneedtime;
intpriority;
};
structReady_type//就绪态进程
{
charname;
intneedtime;
intpriority;
};
structBlocked_type//阻塞态进程
{
charname;
intneedtime;
intpriority;
};
4、程序源代码:
#include
#include
#include
inti=0;intj=0;
structRun_type{
charname;
intstate;
intneedtime;
intpriority;
};
structReady_type{
charname;
intneedtime;
intpriority;
};
structBlocked_type{
charname;
intneedtime;
intpriority;
};
structRun_typeCpu;
structReady_typeReady[10];
structBlocked_typeBlocked[10];
voidCreat(){//创建一个新的进程
intk=0;
label1:
printf("inputnewprocessname(inputaletter):
\n");
scanf("%s",&(Ready[i].name));getchar();
for(k=0;k
if(Ready[i].name==Ready[k].name||Ready[i].name==Ready[k].name+32||Ready[i].name==Ready[k].name-32)
{
printf("theprocessisalreadyexist!
");
gotolabel1;
}
printf("inputneedtime(inputaintnumber):
\n");
label3:
scanf("%d",&(Ready[i].needtime));getchar();
if(Ready[i].needtime<1||Ready[i].needtime>100)
{
printf("pleaseinputthetrueneedtime(1--100)\n");
gotolabel3;
}
printf("inputthepriority(1--10):
\n");
label2:
scanf("%d",&(Ready[i].priority));getchar();
if(Ready[i].priority<1||Ready[i].priority>10)
{
printf("please1--10!
\n");
gotolabel2;
}
i++;
}
voidDispath()//P135
{
intt;charv;intk;intj;
if(Cpu.state==0)
if(Ready[i-1].needtime==0)
{
printf("*thereisnoprocessready!
\n");
exit(0);
}
else
{
for(k=0;kfor(j=0;jif(Ready[j].priority>Ready[j+1].priority)
{t=Ready[j].priority;
Ready[j].priority=Ready[j+1].priority;Ready[j+1].priority=t;
t=Ready[j].needtime;
Ready[j].needtime=Ready[j+1].needtime;Ready[j+1].needtime=t;
v=Ready[j].name;
Ready[j].name=Ready[j+1].name;Ready[j+1].name=v;
}
--i;
Cpu.name=Ready[i].name;Cpu.needtime=Ready[i].needtime;Cpu.priority=Ready[i].priority;Cpu.state=1;
printf("*%5cissendtocpu!
\n",Cpu.name);
Cpu.needtime--;
if(Cpu.needtime==0)
{printf("*%5cisfinished\n",Cpu.name);
Cpu.state=0;}
}
else
{
Ready[i].name=Cpu.name;
Ready[i].needtime=Cpu.needtime;
Ready[i].priority=Cpu.priority;
for(k=0;k
for(j=0;jif(Ready[j].priority>Ready[j+1].priority)
{t=Ready[j].priority;
Ready[j].priority=Ready[j+1].priority;Ready[j+1].priority=t;
t=Ready[j].needtime;
Ready[j].needtime=Ready[j+1].needtime;Ready[j+1].needtime=t;
v=Ready[j].name;
Ready[j].name=Ready[j+1].name;Ready[j+1].name=v;
}
Cpu.name=Ready[i].name;Cpu.needtime=Ready[i].needtime;Cpu.priority=Ready[i].priority;Cpu.state=1;
printf("*%5cissendtocpu!
\n",Cpu.name);
Cpu.needtime--;
if(Cpu.needtime==0)
{printf("*%5cisfinished\n",Cpu.name);
Cpu.state=0;}
}
}
voidTimeout()
{
if(Cpu.state==0)
{
printf("*thereisnoprocessincpu(pleaseselectDispath!
\n");
exit(0);
}
else
{
Ready[i].name=Cpu.name;
Ready[i].needtime=Cpu.needtime;
Ready[i].priority=Cpu.priority;
printf("%cistimeout\n",Cpu.name);
Cpu.name=0;
Cpu.needtime=0;
Cpu.priority=0;
Cpu.state=0;
i++;
}
}
voidEventwait(){
if(Cpu.state!
=0)
{
Blocked[j].name=Cpu.name;
Blocked[j].needtime=Cpu.needtime;
Blocked[j].priority=Cpu.priority;
printf("*%cisBlocked!
\n",Cpu.name);
j++;
Cpu.name=Blocked[j].name;
Cpu.needtime=Blocked[j].needtime;
Cpu.priority=Blocked[j].priority;
Cpu.state=0;
}
else
printf("*Thereisnoprocessincpu!
");
}
voidEventoccurs(chara){
intk=0;intm=0;intn=0;intp=0;
if(Blocked[0].needtime==0)
{
printf("*thereisnoprocessblocked!
\n");
exit(0);
}
else
{
for(k;kif(Blocked[k].name==a)
{
Ready[i].name=Blocked[k].name;
Ready[i].needtime=Blocked[k].needtime;
Ready[i].priority=Blocked[k].priority;
p=1;
m=j-k;
for(n;n{
Blocked[k].name=Blocked[k+1].name;
Blocked[k].needtime=Blocked[k+1].needtime;
Blocked[k].priority=Blocked[k+1].priority;
k++;
}
j--;i++;
}
if(p==1)
printf("*%cisready!
\n",a);
else
printf("*%cisnotfound!
\n",a);
}
}
voidmain(){
Cpu.state=0;
intCputime=0;
intx=6;
while(x!
=0)
{
printf("\n1:
inputnewprocess2:
Dispath\n");
printf("3:
Timeout4:
Eventwait\n");
printf("5:
Eventoccurs(selectwhoseeventoccur)0:
exit\n");
label4:
scanf("%d",&x);getchar();
printf("\n==============================================================================\n");
if(x==0||x==1||x==2||x==3||x==4||x==5)
{
switch(x)
{
case1:
Creat();break;
case2:
Dispath();break;
case3:
Timeout();break;
case4:
Eventwait();break;
case5:
chara;scanf("%c",&a);getchar();Eventoccurs(a);break;
default:
printf("pleaseselectfrom0to5\n");
}
printf("-----------------------------Cputime:
%3d----------------------------------\n",Cputime);
printf("|ProceNsnameNeedTimePriority|\n");
if(Cpu.state!
=0)//显示Cpu中的程序
{
printf("|Cpu:
%16c",Cpu.name);printf("%20d",Cpu.needtime);printf("%18d|\n",Cpu.priority);
}
else
printf("|*Cpuisfree|\n");
inty=0;
if(i==0)//显示Ready队列中的数据
{
printf("|*Thereisnoprocessready.|\n");
}
else
{
for(y;y
{
printf("|Ready%d:
",y);
printf("%15c",Ready[y].name);printf("%20d",Ready[y].needtime);printf("%18d|\n",Ready[y].priority);
}
}
intz=0;
if(j==0)//显示Blocked队列中的程序
{
printf("|*Thereisnoprocessblocked.|\n");
}
else
{
for(z;z{
printf("|Blocked%d:
",z);
printf("%13c",Blocked[z].name);printf("%20d",Blocked[z].needtime);printf("%18d|\n",Blocked[z].priority);
}
}
Cputime++;
if(Cpu.state!
=0)
Cpu.needtime=Cpu.needtime-1;
printf("-----------------------------------------------------------------------------\n");
}
else
{
printf("pleaseinput0--5\n");
gotolabel4;
}
}
}
5、运行结果:
1.创建a,b两个进程
2、调度进程(由于b优先级高,所以b先进入运行态)
3、当b时间片到了,b变为就绪态
4、再次调度(由于b优先级高,故还是b变成运行态)
5、正在运行的b阻塞
6、当b等待的事件发生,b重新进入就绪态
6、程序使用说明:
编译程序后若无错就执行程序,初始如下:
其中:
1表示创建新进程,随后输入进程名,所需时间,优先级
2表示系统调度进程,首先调度最高优先级
3表示时间片已到,运行中的进程将编程就绪态
4表示进程所需的事件尚未发生,进程将被阻塞
5表示进程等待的事件已发生,进程从阻塞态变为就绪态
0退出程序