要求: 理解进程的三状态调度过程,及各状态间的转换关系;
模拟若干个进程的运行过程,将其存入进程文件中。如:进程1:运行5秒后有3秒的I/O操作,之后有10秒的运行,结束。可以写成:”p1:r5,io3,r10 e;”
编程实现调度算法函数,定义时间片大小和并发进程个数,不断从进程文件中读出进程信 息,模拟进程的运行及调度过程;
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
#include "string.h"
#include "windows.h"
#define time 5
#define maxNum 20
typedef struct ProFile{ //进程文件
int run1;
int io;
int run2;
}proFile;
struct pcb{
char name[10];
proFile pf;
int waitTime; // 还需运行时间
int runTime; // 已运行时间
int allTime; // 从开始运行到完成的总时间
int property; // 优先级
char state; // 状态:w(就绪),r(运行),f(完成),b(阻塞)
struct pcb *next;
}pro[maxNum],*ready,*block,*run,*finish,temp,*p,*q;
int num; //需运行的进程数
void initiate(){
for (int i=0;i<num;i++)
{
cout<<"请输入第"<<i+1<<"个进程名:";
cin>>pro[i].name;
cout<<"请输入进程运行信息:运行时间->i/o时间->运行时间,(例:1 2 3)";
cin>>pro[i].pf.run1;
cin>>pro[i].pf.io;
cin>>pro[i].pf.run2;
cout<<"优先数:";
cin>>pro[i].property;
pro[i].waitTime=pro[i].pf.run1+pro[i].pf.io+pro[i].pf.run2;
pro[i].runTime=0;
pro[i].allTime=0;
pro[i].state='w';
pro[i].next=NULL;
}
}
//按优先级高高-->低排序,并插入就绪队列
int newQue(){
for (int i=0;i<num;i++)
{
for (int j=i;j<num;j++)
{
if (pro[i].property<pro[j].property)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
}
}
ready=(struct pcb*)malloc(sizeof(struct pcb));
block=(struct pcb*)malloc(sizeof(struct pcb));
finish=(struct pcb*)malloc(sizeof(struct pcb));
block->next=NULL;
finish->next=NULL;
q=ready;
for (i=0;i<num;i++)
{
p=(struct pcb*)malloc(sizeof(struct pcb));
strcpy(p->name,pro[i].name);
p->pf=pro[i].pf;
p->property=pro[i].property;
p->waitTime=pro[i].waitTime;
p->runTime=pro[i].runTime;
p->allTime=pro[i].allTime;
p->state=pro[i].state;
p->next=NULL;
q->next=p;
q=p;
}
return 1;
}
//输出就绪队列信息
void printR(){
q=ready->next;
if (q!=NULL)
{
cout<<"******************就绪队列信息***************"<<endl;
cout<<"进程名 优先级 已运行时间 还需时间 总运行时间 进程状态"<<endl;
while (q!=NULL)
{
cout<<" "<<q->name<<" ";
cout<<q->property<<" ";
cout<<q->runTime<<" ";
cout<<q->waitTime<<" ";
cout<<q->allTime<<" ";
cout<<q->state<<" ";
q=q->next;
cout<<endl;
}
cout<<"备注-->状态:w(就绪),r(运行),f(完成),b(阻塞)"<<endl;
cout<<endl;
}
}
//输出完成队列信息
void printF(){
q=finish->next;
if (q!=NULL)
{
cout<<"******************完成队列信息***************"<<endl;
cout<<"进程名 优先级 已运行时间 还需时间 总运行时间 进程状态"<<endl;
while (q!=NULL)
{
cout<<" "<<q->name<<" ";
cout<<q->property<<" ";
cout<<q->runTime<<" ";
cout<<q->waitTime<<" ";
cout<<q->allTime<<" ";
cout<<q->state<<" ";
q=q->next;
cout<<endl;
}
cout<<"备注-->状态:w(就绪),r(运行),f(完成),b(阻塞)"<<endl;
cout<<endl;
}
}
//输出阻塞队列信息
void printB(){
q=block->next;
if (q!=NULL)
{
cout<<"******************阻塞队列信息***************"<<endl;
cout<<"进程名 优先级 已运行时间 还需时间 总运行时间 进程状态"<<endl;
while (q!=NULL)
{
cout<<" "<<q->name<<" ";
cout<<q->property<<" ";
cout<<q->runTime<<" ";
cout<<q->waitTime<<" ";
cout<<q->allTime<<" ";
cout<<q->state<<" ";
q=q->next;
cout<<endl;
}
cout<<"备注-->状态:w(就绪),r(运行),f(完成),b(阻塞)"<<endl;
cout<<endl;
}
}
//插入就绪队列
void InsertR()
{
int ok=0;
q=ready;
p=ready->next ;
while(p!=NULL&&ok==0)
{
if(run->property>p->property)
{
q->next =run;
run->next =p;
q=q->next ;
ok=1;
}
else
{
q=p;
p=p->next ;
}
}
if(p==NULL&&ok==0)
q->next =run;
if(p!=NULL&&ok==0)
p->next =run;
}
//插入阻塞队列
void InsertB()
{
p=block;
while(p->next!=NULL)
{
p=p->next;
}
p->next=run;
}
//从阻塞队列删除
void deletB(){
p=block->next;
if(p->next==NULL)
{
block->next=NULL;
}
else{
while (p->next->next!=NULL)
{
p=p->next;
}
q=p->next;
p->next=NULL;
}
}
//插入完成队列
void InsertF()
{
p=finish;
while(p->next !=NULL)
{
p=p->next;
}
p->next=run;
}
//改变其他进程的
void Changeother()
{
p=ready->next;
q=block->next;
while(p!=NULL)
{
p->allTime ++;
p=p->next;
}
while(q!=NULL)
{
q->allTime++;
q=q->next;
}
}
//运行一个进程
int runone(){
run=ready->next;
if(run!=NULL)
{
int ok1=0;
int ok2=0;
q=ready->next->next;
run=ready->next;
run->next=NULL;
ready->next=q;
run->runTime++;
run->allTime++;
int sleepT=run->pf.io;
if (run->pf.run1>0)
{
run->pf.run1--;
}
if (run->pf.run1==0&&run->pf.io==0)
{
ok1=1;
}
Changeother();
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"本次调度进程名: "<<run->name;
if (run->pf.run1>0)
{
run->property--;
cout<<"仍未完成,插入就绪队列,优先数减1"<<endl;
InsertR();
run->state='w';
}
if(run->pf.run1==0&&run->pf.io>0){
cout<<"仍未完成,有I/O请求插入阻塞队列"<<endl;
InsertB();
run->state='b';
ok2=1;
run->waitTime=run->pf.run1+run->pf.io+run->pf.run2;
}
printB();
if(ok2==1){
Sleep(sleepT*1000);
cout<<"仍未完成,I/O请求完成从阻塞队列删除,插入就绪队列"<<endl;
run->pf.io=0;
run->allTime+=run->pf.io;
run->state='r';
InsertR();
deletB();
}
if(ok1==1){
run->pf.run2--;
if(run->pf.run2>0)
{
cout<<"仍未完成,插入就绪队列,优先数减1"<<endl;
run->property--;
InsertR();
run->state='w';
}
if(run->pf.run2==0)
{
run->state ='f';
cout<<"完成运行,从就绪队列删除."<<endl;
InsertF();
}
}
run->waitTime=run->pf.run1+run->pf.io+run->pf.run2;
printR();
printF();
cout<<endl;
cout<<endl;
return 1;
}
else
{
cout<<"所有进程调度完毕"<<endl;
printF();
return 0;
}
}
//运行各个进程
void runall(){
while (ready->next!=NULL)
{
int ok1=0;
int ok2=0;
q=ready->next->next;
run=ready->next;
run->next=NULL;
ready->next=q;
run->runTime++;
run->allTime++;
int sleepT=run->pf.io;
if (run->pf.run1>0)
{
run->pf.run1--;
}
if (run->pf.run1==0&&run->pf.io==0)
{
ok1=1;
}
Changeother();
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"本次调度进程名: "<<run->name;
if (run->pf.run1>0)
{
run->property--;
cout<<"仍未完成,插入就绪队列,优先数减1"<<endl;
InsertR();
run->state='w';
}
if(run->pf.run1==0&&run->pf.io>0){
cout<<"仍未完成,有I/O请求插入阻塞队列"<<endl;
InsertB();
run->state='b';
ok2=1;
run->waitTime=run->pf.run1+run->pf.io+run->pf.run2;
}
printB();
if(ok2==1){
Sleep(sleepT*1000);
cout<<"仍未完成,I/O请求完成从阻塞队列删除,插入就绪队列"<<endl;
run->pf.io=0;
run->allTime+=run->pf.io;
run->state='r';
InsertR();
deletB();
}
if(ok1==1){
run->pf.run2--;
if(run->pf.run2>0)
{
cout<<"仍未完成,插入就绪队列,优先数减1"<<endl;
run->property--;
InsertR();
run->state='w';
}
if(run->pf.run2==0)
{
run->state ='f';
cout<<" 完成运行,从就绪队列删除."<<endl;
InsertF();
}
}
run->waitTime=run->pf.run1+run->pf.io+run->pf.run2;
printR();
printF();
cout<<endl;
cout<<endl;
}
}
void menu(){
int n;
cout<<"待运行的进程数量:";
cin>>num;
initiate();
newQue();
printR();
while(n!=0){
cout<<" **********************************"<<endl;
cout<<" 1---继续下一次调度"<<endl;
cout<<" 2---自动执行"<<endl;
cout<<" 0---退出"<<endl;
cout<<" **********************************"<<endl;
cout<<"请选择 :";
cin>>n;
switch(n){
case 1:
runone(); break;
case 2:
runall(); break;
default:
cout<<"输入错误!!"<<endl;
}
}
}
int main(){
menu();
return 1;
}