正文

多进程调度模拟程序2007-04-16 18:18:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/super/24939.html

分享到:

#include <windows.h>
#include <dos.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream.h>

#define P_NUM 5  //进程数
#define P_TIME 50//时间片长度
#define MIN -9999 //最小优先级

enum state{
    ready,   //就绪
    execute, //执行
    block,   //阻塞
    finish   //完成
};

struct pcb{  //进程控制块结构
    char name[4]; //进程名
    int priority; //优先级
    int cputime;  //占用CPU时间
    int needtime; //还需要执行的时间
    int count;
    int round;
    state process; //进程状态
    pcb * next;
};

pcb * get_process()//获取进程信息
{
    pcb *q;
    pcb *t;
    pcb *p;//进程链表头指针
    int i=0;
    cout<<"input "<<P_NUM<<" names and times"<<endl;
    while (i<P_NUM){
        q=(struct pcb *)malloc(sizeof(pcb));
        cin>>q->name;
        cin>>q->needtime;
        q->cputime   = 0;
        q->priority  = P_TIME - q->needtime;//计算优先级
     q->round = 0;
     q->count = 0;
        q->process   = ready;
        q->next      = NULL;
        if (i==0)  //第一个pcb
     {
            p = q;
            t = q;
        }
        else
  {
            t->next = q;
            t = q;
        }
        i++;
    }  //while
    return p;
}

void  display(pcb *p){
    cout<<"NAME"<<"    "<<"CPUTIME"<<"    "<<"NEEDTIME"
  <<"    "<<"PRIORITY"<<"    "<<"STATE"<<endl;
    while(p){
        cout<<p->name;
        cout<<"        ";
        cout<<p->cputime;
        cout<<"          ";
        cout<<p->needtime;
        cout<<"          ";
        cout<<p->priority;
        cout<<"          ";
        switch(p->process){
            case ready:cout<<"ready"<<endl;break;
            case execute:cout<<"execute"<<endl;break;
            case block:cout<<"block"<<endl;break;
            case finish:cout<<"finish"<<endl;break;
        }
        p=p->next;
    }
}

int process_finish(pcb *q){//判断进程是否均已执行完毕
    int bl=1;
    while(bl&&q)
 {
        bl = bl&&(q->needtime==0);
        q = q->next;
    }
    return bl;
}

void cpuexe(pcb *q){
    pcb* t=q;
    int tp= MIN;
    while(q){  //寻找优先级最大的进程
        if (q->process!=finish){
            q->process=ready;
            if(q->needtime==0){
                q->process=finish;
            }
        }
        if(tp<q->priority&&q->process!=finish){
            tp=q->priority;
            t=q;
        }
        q=q->next;
    }
    if(t->needtime!=0){  //执行进程t
        t->priority-=3;
        t->needtime--;
        t->process=execute;
        t->cputime++;
    }
}

void priority_cal()//优先级调度
{
    pcb * p;
    system("cls");//清屏clrscr();
    p = get_process(); //获取进程链表
 system("cls");
    int cpu=0;
 system("cls");  //clrscr();
    while(!process_finish(p)){
        cpu++;
        cout<<"cputime:"<<cpu<<endl;
        cpuexe(p);
        display(p);
     Sleep(2000);
  system("cls");
    }
    cout<<"All processes have finished,press any key to exit"<<endl;
    getch();
}

void display_menu()//显示菜单
{
    cout<<"CHOOSE THE ALGORITHM:"<<endl;
    cout<<"1 PRIORITY"<<endl;//优先级调度
    cout<<"2 ROUNDROBIN"<<endl;//时间片轮转
    cout<<"3 EXIT"<<endl;//推出
}

void cpu_round(pcb *q){
    q->cputime+=2;
    q->needtime-=2;
    if(q->needtime<0) {
        q->needtime=0;
    }
    q->count++;
    q->round++;
    q->process=execute;

}

pcb * get_next(pcb * k,pcb * head){//获取下一个没执行完的进程
    pcb * t;
    t=k;
    do{
     t=t->next;
    }
    while (t && t->process==finish);

    if(t==NULL){
        t=head;
        while (t->next!=k && t->process==finish){
            t=t->next;
        }
    }
    return t;
}

void set_state(pcb *p){//设置进程状态
    while(p){
        if (p->needtime==0){
            p->process=finish;
        }
        if (p->process==execute){
            p->process=ready;
        }
        p=p->next;
    }
}

void display_round(pcb *p){
    cout<<"NAME"<<"  "<<"CPUTIME"<<"  "<<"NEEDTIME"<<"  "<<"COUNT"<<"  "<<"ROUND"<<"  "<<"STATE"<<endl;
    while(p){
        cout<<p->name<<"      "<<p->cputime<<"       "<<p->needtime
   <<"         "<<p->count<<"       "<<p->round<<"     ";
        switch(p->process){
            case ready:cout<<"ready"<<endl;break;
            case execute:cout<<"execute"<<endl;break;
            case finish:cout<<"finish"<<endl;break;
        }
        p=p->next;
    }
}

void round_cal(){
    pcb * p;
    pcb * r;
 system("cls");
    p=get_process();
    int cpu=0;
 system("cls");
    r=p;
    while(!process_finish(p)){
        cpu+=2;
        cpu_round(r);
        r=get_next(r,p);
        cout<<"cpu "<<cpu<<endl;
        display_round(p);
        set_state(p);
        Sleep(5000);
  system("cls");
    }
 cout<<"All processes have finished,press any key to exit"<<endl;
    getch();
}

void main(){
    display_menu();//显示菜单
    int k;
 cin>>k;
    switch(k)
 {
  case 1:
   priority_cal();  //调用优先级调度函数
   break;
        case 2:
   round_cal();     //调用轮转调度函数
   break;
        case 3:
   break;           //推出
  default:
   cout<<"Input error!";
    }
}

阅读(3450) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册