资源描述
实验一:熟悉Linux系统
实验目的:
自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容 、组织的变化,理解进程与其PCB间的一一对应关系。
实验要求:
设计并实现一个模拟进程状态转换及其相应PCB组织结构变化的程序;
独立设计、编写、调试程序;
程序界面应能反映出在模拟条件下,进程之间状态转换及其对应的PCB组织的变化。
进程的状态模型(三状态、五状态、七状态或其它)可自行选择
运行
Running
就绪
Ready
等待
Blocked
j
Dispatch
k
Timeout
l
Event
Wait
m
Event
Occurs
基本状态间的转换
代码书写要规范,要适当地加入注释;
鼓励在实验中加入新的观点或想法,并加以实现;
认真进行预习,完成预习报告;
实验完成后,要认真总结,完成实验报告。
程序流程图:
说明(1)上为运行结果,x<y>:x为进程号;y为进程结束剩余时间片。
(2)程序中使用队列数据结构,并以结构体process(进程)作为队列元素。
(3) 程序自动进行时间片分配,但每次运行一个进程前需要进行阻塞判断,由操作员手动输入。
(4)程序在等待队列中进程小于4时,自动将阻塞队列中的进程释放出来。
程序源代码:
#include<iostream>
#include<queue>
#include<windows.h>
using namespace std;
struct process
{
int id;
int hp;
process(){}
process(int a,int b)
{
id=a;
hp=b;
}
};
queue<process> re,bl,ru;
int main()
{
void show(queue<process> re,queue<process> ru,queue<process> bl);
process p1(1,2),p2(2,1),p3(3,1),p4(4,3),p5(5,2),p6(6,1);
re.push(p1);
re.push(p2);
re.push(p3);
bl.push(p4);
bl.push(p5);
bl.push(p6);
process x;char ch;int n;
cout<<"初始化中.";
Sleep(2000);
cout<<".";
Sleep(2000);
cout<<"."<<endl<<endl;
show(re,ru,bl);
cout<<endl<<"开始执行!"<<endl<<endl;
while(!re.empty())
{
x=re.front();
re.pop();
ru.push(x);
x=ru.front();
show(re,ru,bl);
cout<<endl<<"是否阻塞?(y/n)"<<endl;
ch=getchar();
getchar();
if(ch=='y')
{
x=ru.front();
ru.pop();
bl.push(x);
}
else if(ch=='n')
{
x.hp--;
ru.front().hp=x.hp;
if(x.hp!=0)
{
x=ru.front();
ru.pop();
re.push(x);
}
else ru.pop();
}
else cout<<"输入有误"<<endl;
n=re.size();
while(n<4)
{
if(bl.empty())break;
x=bl.front();
bl.pop();
re.push(x);
n++;
}
}
return 0;
}
void show(queue<process> re,queue<process> ru,queue<process> bl)
{
cout<<"运行任务:";
process y;
if(ru.empty())cout<<"空"<<endl;
else
{
y=ru.front();
ru.pop();
cout<<y.id<<"("<<y.hp<<")"<<endl;
}
cout<<"队列中的任务:";
if(!re.empty())
{
while(!re.empty())
{
y=re.front();
re.pop();
cout<<y.id<<"("<<y.hp<<") ";
}
}
else cout<<"空";
cout<<endl<<"阻塞中的任务:";
if(!bl.empty())
{
while(!bl.empty())
{
y=bl.front();
bl.pop();
cout<<y.id<<"("<<y.hp<<") ";
}
}
else cout<<"空";
cout<<endl;
}
展开阅读全文