进程管理

 要求:        理解进程的三状态调度过程,及各状态间的转换关系;

模拟若干个进程的运行过程,将其存入进程文件中。如:进程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;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
【实验目的】 1. 理解进程的概念,熟悉进程的组成; 2. 用高级语言编写和调试一个进程调度程序,以加深对进程调度算法理解。 【实验准备】 1. 几种进程调度算法  短进程优先调度算法  高优先权优先调度算法  先来先服务调度算法  基于时片的轮转调度算法 2. 进程的组成  进程控制块(PCB)  程序段  数据段 3. 进程的基本状态  就绪W(Wait)  执行R(Run)  阻塞B(Block) 【实验内容】 1. 例题 设计一个有 N个进程共行的进程调度程序。 进程调度算法:采用最高优先数优先的调度算法(即把处理机分配给优先数最高的进程)和先来先服务算法。 每个进程有一个进程控制块(PCB)表示。进程控制块可以包含如下信息:进程名、优先数、到达时、需要运行、已用CPU时进程状态等等。进程的优先数及需要的运行可以事先人为地指定(也可以由随机数产生)。进程的到达时进程输入的时进程运行以时片为单位进行计算。每个进程状态可以是就绪 W(Wait)、运行R(Run)、或完成F(Finish)状态之一。就绪进程获得 CPU后都只能运行一个时片。用已占用CPU时加1来表示。如果运行一个时片后,进程的已占用 CPU时已达到所需要的运行,则撤消该进程,如果运行一个时片后进程的已占用CPU时还未达所需要的运行,也就是进程还需要继续运行,此时应将进程的优先数减1(即降低一级),然后把它插入就绪队列等待CPU。每进行一次调度程序都打印一次运行进程、就绪队列、以及各个进程的 PCB,以便进行检查。重复以上过程,直到所要进程都完成为止。 4. 实验题目  编写并调试一个模拟进程调度程序,采用“最高优先数优先”调度算法对五个进程进行调度。“最高优先数优先”调度算法的基本思想是把CPU分配给就绪队列中优先数最高的进程。静态优先数是在创建进程时确定的,并在整个进程运行不再改变。动态优先数是指进程的优先数在创建进程时可以给定一个初始值,并且可以按一定原则修改优先数。例如在进程获得一次CPU后就将其优先数减少1。或者,进程等待的时超过某一时限时增加其优先数的值,等等。  编写并调试一个模拟进程调度程序,采用“轮转法”调度算法对五个进程进行调度。轮转法可以是简单轮转法、可变时片轮转法,或多队列轮转法。简单轮转法的基本思想是:所有就绪进程按 FCFS排成一个队列,总是把处理机分配给队首的进程,各进程占用CPU的时片相同。如果运行进程用完它的时片后还为完成,就把它送回到就绪队列的末尾,把处理机重新分配给队首的进程。直至所有的进程运行完毕。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值