操作系统实验
---进程状态转换及其PCB的变化
1. 目的:
自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容、组织的变化,理解进程与其PCB间的一一对应关系。
2. 内容及要求:
1)设计并实现一个模拟进程状态转换及其相应PCB内容、组织结构变化的程序。
2)独立编写、调试程序。进程的数目、进程的状态模型(三状态、五状态、七状
态或其它)以及PCB的组织形式可自行选择。
3)合理设计与进程PCB相对应的数据结构。PCB的内容要涵盖进程的基本信息、控
制信息、资源需求及现场信息。
4)设计出可视性较好的界面,应能反映出进程状态的变化引起的对应PCB内容、
组织结构的变化。
5)代码书写要规范,要适当地加入注释。
6)鼓励在实验中加入新的观点或想法,并加以实现。
7)认真进行预习,完成预习报告。
8)实验完成后,要认真总结,完成实验报告。
3. 程序流程图
4.代码部分
(1)数据结构部分
进程结构体
struct PCB
{
char P_name[10];
int P_id;
int P_state;
int needtime;
int lefttime;
int P_level;
};
进程结构体有:进程号,进程状态,进程剩下的时间,进程需要的时间,进程的优先级
队列结构体
struct Queue
{
PCB qNode[5];
int queueLength;
};
定义的队列最多有5个进程。
(2)程序解释
这里做的是对操作系统课程里面将的“3 状态”的模型的模拟,给它提供的初始状态是有5个进程,而进程所需要的时间和进程的状态都是随机产生的,并把就绪队列里面的进程按照进程的优先级(P_level)进行排序,设置初始状态时没有在运行的程序。
(3)程序的代码
//#include
#include
#include
#include
struct PCB
{
char P_name[10];
int P_id;
int P_state;
int needtime;
int leavetime;
int P_level;
};
struct Queue
{
PCB qNode[5];
int queueLength;
};
int insertQueue(Queue *q,PCB *p)
{
int i=0;
if (q->queueLength==0)
{
q->qNode[i]=*p;
q->queueLength++;
return 1;
}
else
{
while(p->P_level<=q->qNode[i].P_level)
{
i++;
}
for (int j=q->queueLength;j>=i;j--)
{
q->qNode[j]=q->qNode[j-1];
}
q->qNode[i]=*p;
q->queueLength=q->queueLength+1;
return 1;
}
}
void setQueue(PCB pcb[5],Queue *ready,Queue *blocked,Queue *finish) {
ready->queueLength=0;
blocked->queueLength=0;
finish->queueLength=0;
for (int i=0;i<5;i++)
{
if (pcb[i].P_state==0)
{
insertQueue(ready,&pcb[i]);
}
else
{
blocked->qNode[blocked->queueLength]=pcb[i];
blocked->queueLength++;
}
}
}
void show(Queue ready,Queue blocked,Queue fininsh,PCB run)
{
printf("\n完成队列\n");
printf("进程名------优先级------所需时间--------剩余时间\n");
for (int i=0;i { printf("%s%d %d %d %d\n",fininsh.qNode[i].P_name,fininsh.qNode[i].P_id,fininsh.qNode[i].P_level,fininsh.qNod e[i].needtime,fininsh.qNode[i].leavetime); } printf("\n"); if (run.needtime!=0) { printf("当前正在运行的进程:\n"); printf("进程名----------优先级------所需时间--------剩余时间\n"); printf("%s%d %d %d %d\n",run.P_name,run.P_id,run.P_level,run.needtime,run.leavetime); } printf("\n"); printf("就绪队列\n"); printf("进程名----------优先级------所需时间--------剩余时间\n"); for ( i=0;i { printf("%s%d %d %d %d\n",ready.qNode[i].P_name,ready.qNode[i].P_id,ready.qNode[i].P_level,ready.qNode[i].n eedtime,ready.qNode[i].leavetime); } printf("\n"); printf("阻塞队列\n"); printf("进程名----------优先级------所需时间--------剩余时间\n"); for (i=0;i { printf("%s%d %d %d %d\n",blocked.qNode[i].P_name,blocked.qNode[i].P_id,blocked.qNode[i].P_level,blocked.q Node[i].needtime,blocked.qNode[i].leavetime); } } void P_levelRun(Queue ready,Queue blocked,Queue finish,PCB run) { int i_blocked=0; int i_finish=0; while(finish.queueLength!=5) { printf("*******************************************************************\n "); int i_ready=0; if (run.needtime!=0) { run.leavetime--; if (run.needtime!=0&&run.leavetime==0) { finish.qNode[i_finish]=run; finish.queueLength++; i_finish++; run.needtime=0; if (ready.queueLength!=0) { run=ready.qNode[i_ready]; for (;i_ready { ready.qNode[i_ready]=ready.qNode[i_ready+1]; } ready.queueLength=ready.queueLength-1; } } else { int temp=run.P_id; if (run.P_level<=ready.qNode[0].P_level) { insertQueue(&ready,&run); run.needtime=0; printf("时间片用尽,%s%d进入就绪队列,优先级更高的%s%d开始运行!\n",run.P_name,run.P_id,ready.qNode[0].P_name,ready.qNode[0].P_id); } else { printf("时间片用尽,但是没有更高优先级的进程,%s%d继续运行……\n",run.P_name,run.P_id); } } } else if (run.needtime==0) { if (ready.queueLength!=0) { run=ready.qNode[i_ready]; for (;i_ready { ready.qNode[i_ready]=ready.qNode[i_ready+1]; } ready.queueLength=ready.queueLength-1; } } if (blocked.queueLength!=0) { srand((int) time(0)); int temp=rand()%2; if (temp==1) { printf("\n%s%d所需资源产生,进程从阻塞队列转到就绪队列……\n",&blocked.qNode[i_blocked].P_name,blocked.qNode[i_blocked].P_id); insertQueue(&ready,&blocked.qNode[i_blocked]); blocked.queueLength=blocked.queueLength-1; if (blocked.queueLength!=0) { for (int i=0;i { blocked.qNode[i]=blocked.qNode[i+1]; } } } else if(temp==0) { printf("\n%s%d所需资源没有产生,等待。。\n",&blocked.qNode[i_blocked].P_name,blocked.qNode[i_blocked].P_id); } } show(ready,blocked,finish,run); } } void main() { Queue ready,blocked,finish; PCB pcb[5]; PCB run; run.needtime=0; printf("设置了5个进程!\n[说明:进程的状态,所需时间和优先级等是随机产生的。]\n\n\n"); srand((int) time(0)); for (int i=0;i<5;i++) { pcb[i].P_name[0]='P'; pcb[i].P_name[1]='r'; pcb[i].P_name[2]='o'; pcb[i].P_name[3]='c'; pcb[i].P_name[4]='e'; pcb[i].P_name[5]='s'; pcb[i].P_name[6]='s'; pcb[i].P_name[7]='\0'; pcb[i].P_id=i; pcb[i].P_state=rand()%2; pcb[i].P_level=rand()%10+1; pcb[i].needtime=rand()%2+1; pcb[i].leavetime=pcb[i].needtime; } setQueue(pcb,&ready,&blocked,&finish); printf("各队列的初始信息如下:\n\n"); show(ready,blocked,finish,run); P_levelRun(ready,blocked,finish,run); printf("\n5个进程都运行完,停止。\n"); } (4)截图部分: 最新文件仅供参考已改成word文本。方便更改如有侵权请联系网站删除