#include #include using namespace std; #define" />
当前位置:文档之家› 南邮操作系统实验代码

南邮操作系统实验代码

cout<<endl;
}
voidRPcb::print()
{
cout<<"********************************************"<<endl;
cout<<"进程名"<<"\t"<<"还需运行时间\t"<<"已用CPU时间"<<
"\t"<<"状态"<<endl;
for(inti = 0; i<P_NUM; i++)
rpcb[i].process =finish;
rpcb[i].cpuTime = rpcb[i].allTime;//防止所用时间超过总的时间
system("cls");
print();
Sleep(1000);
}
else
{
system("cls");
print();
Sleep(1000);
rpcb[i].process =run;
hpcb[f].process =finish;
hpcb[f].cpuTime = hpcb[f].allTime;//防止所用时间超过总的时间
system("cls");
print();
Sleep(1000);
}
else
{
hpcb[f].cpuTime -=P_TIME;//为了输出改变前的相关信息
hpcb[f].firstNum++;//为了输出改变前的相关信息
system("cls");
print();
Sleep(1000);
hpcb[f].cpuTime +=P_TIME;
hpcb[f].firstNum--;
hpcb[f].process =ready;
}
for(ii = 0; ii<P_NUM; ii++)//用于判断是否还有进程未完成
{
for(i = 0; i<P_NUM; i++)
if(rpcb[i].process ==run)
break;
rpcb[i].cpuTime +=P_TIME;
rpcb[i].process =run;
if(rpcb[i].cpuTime >= rpcb[i].allTime)//该进程执行完成
{
{
fpcb[i].cpuTime -=P_TIME;
system("cls");
print();
Sleep(1000);
fpcb[i].cpuTime +=P_TIME;
fpcb[i].cpuTime = fpcb[i].allTime;
fpcb[i].process =finish;
fpcb[i + 1].process =run;
hpcb[f].firstNum--;
hpcb[f].process =run;
if(hpcb[f].cpuTime >= hpcb[f].allTime)//该进程执行完成
{
hpcb[f].cpuTime -=P_TIME;
system("cls");
print();
Sleep(1000);
hpcb[f].firstNum =MIN;
system("cls");
print();
Sleep(1000);
}
else
{
fpcb[i].cpuTime -=P_TIME;
system("cls");
print();
Sleep(1000);
fpcb[i].process =finish;
fpcb[i].cpuTime = fpcb[i].allTime;
print();
Sleep(1000);
rpcb[i].process =finish;
rpcb[i].cpuTime = rpcb[i].allTime;//防止所用时间超过总的时间
system("cls");
print();
Sleep(1000);
}
else
{
rpcb[i].cpuTime -=P_TIME;
{
intii, f, i = 0;
for(; i<P_NUM; i++)
{
char* ch;
ch =newchar[1];
cout<<"请输入第"<<i + 1<<"个进程的进程名优先、需要运行的时间:"<<endl;
cin>>ch;
hpcb[i].name =newchar[strlen(ch) + 1];
}
intHPcb::getFirst()//得到优先级最高的进程
{
intk = 0;
for(inti = 1; i<P_NUM; i++)
if(hpcb[k].firstNum<hpcb[i].firstNum)
k = i;
returnk;
}
Pcb::~Pcb()
{
delete[]name;
}
voidHPcb::print()
intfirstNum;
};
HPcbhpcb[P_NUM];
classFPcb:publicPcb
{
public:
staticvoidprint();
staticvoidfcfs();
private:
intcomeTime;
};
FPcbfpcb[P_NUM];
classRPcb:publicPcb
{
#defineP_TIME2//时间片长度
#defineMIN-9999
enumstate//进程状态
{Hale Waihona Puke ready,//就绪run,//执行
wait,//阻塞
finish//完成
};
classPcb
{
public:
staticvoidprint() {};
~Pcb();
protected:
char* name;//进程名
system("cls");
print();
Sleep(1000);
}
}
else
{
fpcb[i].cpuTime -=P_TIME;
system("cls");
print();
Sleep(1000);
fpcb[i].cpuTime +=P_TIME;
}
}
}
cout<<"所有进程已运行完成!"<<endl;
intallTime;//需要运行时间
intcpuTime;//已用cpu时间
stateprocess;//进程状态
};
classHPcb:publicPcb
{
public:
staticvoidprint();
staticvoidhighS();
staticintgetFirst();
private:
system("cls");
print();
Sleep(1000);
rpcb[i].cpuTime +=P_TIME;
rpcb[i].process =ready;
}
}
}
for(i = 0; i<P_NUM; i++)
if(rpcb[i].process !=ready)
k++;
if(k == 2)
public:
staticvoidprint();
staticvoidrr();
};
RPcbrpcb[P_NUM];
voidRPcb::rr()//时间片轮转算法
{
intii, i = 0;
intk = 0;
for(; i<P_NUM; i++)
{
char* ch;
ch =newchar[1];
cout<<"请输入第"<<i + 1<<"个进程的进程名、需要运行的时间:"<<endl;
caseready:cout<<"就绪态"<<endl;break;
caserun:cout<<"运行态"<<endl;break;
casefinish:cout<<"完成态"<<endl;break;
cin>>ch;
rpcb[i].name =newchar[strlen(ch) + 1];
strcpy_s(rpcb[i].name,5,ch);
cin>>rpcb[i].allTime;
rpcb[i].cpuTime = 0;
rpcb[i].process =ready;
}
do
{
for(i = 0; i<P_NUM; i++) {
相关主题