操作系统实验进程调算法模拟

一、实验目的

进程调度是处理机管理的核心内容。本实验要求用高级语言编写模拟进程调度程序,以便加深理解有关进程控制快、进程队列等概念,并体会和了解优先数算法和时间片轮转算法的具体实施办法。

二、实验内容和要求

  1. 设计进程控制块PCB的结构,通常应包括:进程名、进程优先数(或轮转时间片数)、进程已占用的CPU时间、进程到完成还需要的时间、进程的状态、当前队列指针等。
  2. 编写两种调度算法程序:

1)  优先数调度算法程序;

2)  循环轮转调度算法程序。

  1. 程序源代码和运行截图写入实验报告并提交

三、实验步骤

  1. 进程控制模块结构:

Name:进程标示符

Prio/Round:进程优先数

Cputime:进程累计占用

Needtime:进程到完成还需时间片数

State:进程状态

Next:链指针

  1. 进程就绪态和等待态为链表结构:

Run:当前运行进程指针

Ready:就绪队列头指针

Tall:就绪队尾指针

Finish:完成队列头指针

  1. 程序模块结构:

Insert1:优先数算法中,将尚未完成的pcb按优先数顺序插入到就绪队列中

Insert2:在轮转法中,将执行了一个时间片单位(2),但尚未完成的进程的pcb插到就绪队列的队尾

Firstin:调度就行队列的第一个进程投入运行

Print:显示每执行一次后所有进程的状态及相关信息

Create:创建新进程,并将他的pcb插入就绪队列

Prisch:按优先数算法调度进程

Roundsch:按时间片轮转法调度进程

优先数算法中,进程优先数的初值为50needtime

  1. 实验代码

#include<iostream>

#include<string>

 

using namespace std;

 

typedef struct node

{

        char name[20];  //进程名

        int prio;       //进程优先级

        int round;      //分配cpu的时间片

        int cputime;    //cpu执行时间

        int needtime;   //进程执行所需时间

        char state;     //进程状态

        int count;      //记录执行次数

        struct node *next; //链表指针

}PCB;

 

int num;

 

PCB *ready = NULL;   //就绪队列

PCB *run = NULL;     //执行队列

PCB *finish = NULL; //完成队列

 

//取得第一个就绪节点

void GetFirst()

{

        run = ready;

        if(ready!=NULL)

        {

                run->state='R';

                ready=ready->next;

                run->next=NULL;

        }

}

 

//优先级输出队列

void Output1()

{

        PCB *p;

        p=ready;

        while(p != NULL)

        {

                cout<<p->name<<"\t"<<p->prio<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<

                p->state<<"\t"<<p->count<<endl;

                p=p->next;

        }

        p=finish;

        while(p!=NULL)

        {

                    cout<<p->name<<"\t"<<p->prio<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<

                p->state<<"\t"<<p->count<<endl;

                p=p->next;

        }

        p=run;

        while(p!=NULL)

        {

                    cout<<p->name<<"\t"<<p->prio<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<

                p->state<<"\t"<<p->count<<endl;

                p=p->next;

        }

}

 

//轮转法输出队列

void Output2()

{

        PCB *p;

        p=ready;

        while(p!=NULL)

        {

                cout<<p->name<<"\t"<<p->round<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<

                p->state<<"\t"<<p->count<<endl;

                p=p->next;

        }

        p=finish;

        while(p!=NULL)

        {

                cout<<p->name<<"\t"<<p->round<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<

                p->state<<"\t"<<p->count<<endl;

                p=p->next;

        }

        p=run;

        while(p!=NULL)

        {

                cout<<p->name<<"\t"<<p->round<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<

                p->state<<"\t"<<p->count<<endl;

                p=p->next;

        }

}

 

 

void InsertPrio(PCB *in)

{

        PCB *fst,*nxt;

        fst=nxt=ready;

        if(ready==NULL)

        {

                in->next=ready;

                ready=in;

        }

        else

        {

                if(in->prio>=fst->prio)

                {

                        in->next=ready;

                        ready =in;

                }

                else

                {

                        while(fst->next!=NULL)

                        {

                                nxt=fst;

                                fst=fst->next;

                        }

                        if(fst->next == NULL)

                        {

                                in->next=fst->next;

                                fst->next=in;

                        }

                        else

                        {

                                nxt=in;

                                in->next=fst;

                        }

                }

        }

}

 

void InsertTime(PCB *in)

{

        PCB *fst;

        fst =ready;

        if(ready==NULL)

        {

                in->next = ready;

                ready = in;

        }

        else

        {

                while(fst->next!=NULL)

                {

                        fst=fst->next;

                }

                in->next=fst->next;

                fst->next=in;

        }

}

 

void InsertFinish(PCB *in)

{

        PCB *fst;

        fst=finish;

        if(finish==NULL)

        {

                in->next=finish;

                finish=in;

        }

        else

        {

                while(fst->next!=NULL)

                {

                        fst=fst->next;

                }

                in->next=finish;

                finish=in;

        }

}

 

void PrioCreate()

{

        PCB *tmp;

        int i;

        cout<<"Enter the name and needtime   :"<<endl;

        for(i=0;i<num;i++)

        {

                if((tmp=(PCB*)malloc(sizeof(PCB)))==NULL)

                {

                        cerr<<"malloc"<<endl;

                        exit(1);

                }

                cin>>tmp->name;

                getchar();

                cin>>tmp->needtime;

                tmp->cputime=0;

                tmp->state='W';

                tmp->prio=50-tmp->needtime;

                tmp->round=0;

                tmp->count=0;

                InsertPrio(tmp);

        }

        cout<<"进程名\t优先级\tcpu时间\t需要时间进程状态计数器"<<endl;

}

 

void TimeCreate()

{

        PCB *tmp;

        int i;

 

        cout<<"输入进程名字和进程时间片所需时间:"<<endl;

        for(i=0;i<num;i++)

        {

                if((tmp=(PCB*)malloc(sizeof(PCB)))==NULL)

                {

                        cerr<<"malloc"<<endl;

                        exit(1);

                }

                cin>>tmp->name;

                getchar();

                cin>>tmp->needtime;

                tmp->cputime=0;

                tmp->state='W';

                tmp->prio=0;

                tmp->round=2;

                tmp->count=0;

                InsertTime(tmp);

        }

         cout<<"进程名\t优先级\tcpu时间\t需要时间进程状态计数器"<<endl;

}

 

void Priority()

{

        int flag=1;

        GetFirst();

        while(run!=NULL)

        {

                Output1();

                while(flag)

                {

                        run->prio=3;

                        run->cputime++;

                        run->needtime--;

                        if(run->needtime==0)

                        {

                                run->state='F';

                                run->count++;

                                InsertFinish(run);

                                flag=0;

                        }

                        else

                        {

                                run->state='W';

                                run->count++;

                                InsertTime(run);

                                flag=0;

                        }

                }

                flag=1;

                GetFirst();

        }

}

void RoundRun()

{

        int flag=1;

        GetFirst();

        while(run!=NULL)

        {

                Output2();

                while(flag)

                {

                        run->count++;

                        run->cputime++;

                        run->needtime--;

                        if(run->needtime==0)

                        {

                                run->state='F';

                                InsertFinish(run);

                                flag=0;

                        }

                        else if(run->count==run->round)

                        {

                                run->state='W';

                                run->count=0;

                                InsertTime(run);

                                flag=0;

                        }

                }

                flag=1;

                GetFirst();

        }

}

int main(void)

{

        int n;

        cout<<"输入进程个个数:  "<<endl;

        cin>>num;

        getchar();

        cout<<"----------进程调度算法模拟-----------"<<endl;

        cout<<"          1.优先级调度算法"<<endl;

        cout<<"          2.循环轮转调度算法"<<endl;

        cout<<"-------------------------------------"<<endl;

 

        cout<<"输入序号:   "<<endl;

        cin>>n;

        switch(n)

        {

                case 1:

                    cout<<"优先级调度  :"<<endl;

                    PrioCreate();

                    Priority();

                    Output1();

                    break;

 

                case 2:

                    cout<<"循环轮转算法:  "<<endl;

                    TimeCreate();

                    RoundRun();

                    Output2();

                    break;

                case 0:

                    exit(1);

                    break;

                default:

                    cout<<"Enter error!"<<endl;

                    break;

        }

        cout<<endl;

        return 0;

}

四、实验结果

 

 

 

 

五、实验总结

通过本次实验,我学到了进程调度算法,了解到了进程调度是cpu管理的核心,不同的调度算法会使进程的运行时间不同,选择使用合适的算法是提供运行速度的一个关键,同时本次实验也让我久违的使用了c++编程,算是一次有意义的复习吧。

posted @   Ryuuko-  阅读(1030)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示