# 操作系统实验二
# 前情提要 Again:
实验介绍很多,很多,非常多,让人看的很头大,越看越感觉这实验是要你把系统进程管理手搓出来,然后一种感觉油然而生。
老师太看得起我们啦!!!
后来仔细看了一下 命令行的内容,发现,好像,似乎,大概,不需要我们真的创建进程然后去管理,我们只需要去模拟这个进程管理的流程就行了,然后就有了这样的简易版的进程模拟程序:
# 一、进程结构体和资源结构体
进程结构体:
struct ProcessNode
{
int pid;
string name;
vector <int> reslist;
int state; // state 0->Ready 1->Block 2->Running 3->release
int priority; //优先级
int tim; // 建立时间
ProcessNode* fa;
ProcessNode* child;
bool operator < (const ProcessNode & ot) const
{
if(priority!=ot.priority)
return priority>ot.priority;
return tim<ot.tim;
}
};
资源结构体:
struct ProcessNode
{
int pid;
string name;
vector <int> reslist;
int state; // state 0->Ready 1->Block 2->Running 3->release
int priority; //优先级
int tim; // 建立时间
ProcessNode* fa;
ProcessNode* child;
bool operator < (const ProcessNode & ot) const //排序方法,先根据优先级排序,再根据时间顺序排序
{
if(priority!=ot.priority)
return priority>ot.priority;
return tim<ot.tim;
}
};
# 二、进程队列、阻塞队列、资源管理模块
vector <ProcessNode > ready;
vector <ProcessNode > block;
vector <ResNode> res;
# 三、进程创建
void create(string name,int prio)
{
ProcessNode newnode;
newnode.child=NULL,newnode.fa=NULL;
newnode.pid=++pidcnt;
newnode.priority=prio;
newnode.tim=++timcnt;
newnode.name=name;
ready.push_back(newnode);
sort(ready.begin(),ready.end());
return;
}
# 四、资源请求
void request(char name)
{
int len=res.size();
for(int i=0;i<len;i++)
{
if(res[i].name==name && res[i].available==true)
{
res[i].available=false;
ready[0].reslist.push_back(res[i].rid);
cout<<"Process "<<ready[0].name<<" is running"<<endl;
}
else if(res[i].name==name && res[i].available==false)
{
ProcessNode t=ready[0];
ready[0].reslist.push_back(res[i].rid);
ready.erase(ready.begin());
block.push_back(t);
cout<<"Process "<<t.name<<" is blocked;Process "<<ready[0].name<<" is running"<<endl;
}
}
return;
}
# 五、进程释放
bool check()
{
if(block.size()==0)
return false;
int len=block[0].reslist.size();
for(int i=0;i<len;i++)
{
int l1=res.size();
for(int j=0;j<l1;j++)
{
if(res[j].rid==block[0].reslist[i])
{
if(res[j].available==false)
return false;
break;
}
}
}
return true;
}
void release()
{
int l1=ready[0].reslist.size();
for(int i=0;i<l1;i++)
{
int l2=res.size();
for(int j=0;j<l2;j++)
{
if(res[j].rid==ready[0].reslist[i])
res[j].available=true;
}
}
ready.erase(ready.begin());
while(check())
{
ProcessNode t=block[0];
cout<<"Process "<<t.name<<" is activate"<<endl;
block.erase(block.begin());
ready.push_back(t);
}
sort(ready.begin(),ready.end());
if(ready.size()!=0)
cout<<"Process "<<ready[0].name<<" is running"<<endl;
else
cout<<"All the Processes have finished"<<endl;
return;
}
# 六、完整程序:
#include <bits/stdc++.h>
using namespace std;
int pidcnt,ridcnt,timcnt;
struct ResNode
{
char name;
int rid;
bool available;
ResNode(char nn,int rr,bool aa)
{
name=nn,rid=rr,available=aa;
}
ResNode()
{
}
};
struct ProcessNode
{
int pid;
string name;
vector <int> reslist;
int state; // state 0->Ready 1->Block 2->Running 3->release
// state其实可以不加,但是为了完整性可以加一下
int priority; //优先级
int tim; // 建立时间
ProcessNode* fa;
ProcessNode* child;
bool operator < (const ProcessNode & ot) const
{
if(priority!=ot.priority)
return priority>ot.priority;
return tim<ot.tim;
}
};
vector <ProcessNode > ready;
vector <ProcessNode > block;
vector <ResNode> res;
void create(string name,int prio)
{
ProcessNode newnode;
newnode.child=NULL,newnode.fa=NULL;
newnode.pid=++pidcnt;
newnode.priority=prio;
newnode.tim=++timcnt;
newnode.name=name;
ready.push_back(newnode);
sort(ready.begin(),ready.end());
return;
}
void init_res()
{
//一共十种资源,每种资源初始都可以使用,每种资源的命名方式是由A,B,C,...以此类推
for(int i=0;i<10;i++)
res.push_back(ResNode(i+'A',++ridcnt,true));
return;
}
void request(char name)
{
int len=res.size();
for(int i=0;i<len;i++)
{
if(res[i].name==name && res[i].available==true)
{
res[i].available=false;
ready[0].reslist.push_back(res[i].rid);
cout<<"Process "<<ready[0].name<<" is running"<<endl;
}
else if(res[i].name==name && res[i].available==false)
{
ProcessNode t=ready[0];
ready[0].reslist.push_back(res[i].rid);
ready.erase(ready.begin());
block.push_back(t);
cout<<"Process "<<t.name<<" is blocked;Process "<<ready[0].name<<" is running"<<endl;
}
}
return;
}
bool check()
{
if(block.size()==0)
return false;
int len=block[0].reslist.size();
for(int i=0;i<len;i++)
{
int l1=res.size();
for(int j=0;j<l1;j++)
{
if(res[j].rid==block[0].reslist[i])
{
if(res[j].available==false)
return false;
break;
}
}
}
return true;
}
void release()
{
int l1=ready[0].reslist.size();
for(int i=0;i<l1;i++)
{
int l2=res.size();
for(int j=0;j<l2;j++)
{
if(res[j].rid==ready[0].reslist[i])
res[j].available=true;
}
}
ready.erase(ready.begin());
while(check())
{
ProcessNode t=block[0];
cout<<"Process "<<t.name<<" is activate"<<endl;
block.erase(block.begin());
ready.push_back(t);
}
sort(ready.begin(),ready.end());
if(ready.size()!=0)
cout<<"Process "<<ready[0].name<<" is running"<<endl;
else
cout<<"All the Processes have finished"<<endl;
return;
}
int main()
{
init_res();
cout<<"请输入命令"<<endl;
string op;
int prio;
while(cin>>op)
{
if(op=="cr")
{
string name;
cin>>name>>prio;
create(name,prio);
cout<<"Process "<<ready[0].name<<" is running"<<endl;
}
else if(op=="rq")
{
char name;
cin>>name;
request(name);
}
else if(op=="rel")
release();
else if(op=="esc")
break;
}
return 0;
}
可能有些不完善的地方,如有问题,请评论区指出。