操作系统实验——进程调度(2)

实验任务

设计一个程序,根据不同的调度算法模拟操作系统对进程的调度。
调度算法: 时间片循环法
1、 设计进程控制块PBC表结构,适用循环时间片轮转算法。
2、 PBC结构通常包括以下信息:进程名、进程优先数、轮转时间片、进程的CPU时间,进程状态等。根据调度算法不同,PCB结构可作适当的调整。
3、 建立进程队列。对不同的算法编制不同的入链程序。
程序要求达到的运行效果:在设置好进程数量、调度算法后,系统能按设定的参数运行,并在屏幕上交替显示就绪队列和完成队列的进程名等信息。

windows环境下代码:

#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int n;
class PCB
{
    public:
        int runtime;
        string name;
        string state;
        int needtime;
        int round;
        int Counter;
        PCB * next;
};
PCB * run = NULL;
PCB * ready = NULL;
PCB * finish = NULL;
PCB * tial = ready;
void Dtime(int t);
void Prinft()
{
    cout<<"进程名称"<<"\t"<<"已运行时间"<<"\t"<<"还需要时间"<<"\t"<<"计数器"<<"\t"<<"时间片"<<"\t"<<"状态"<<endl;
}
void Prinft(PCB * p)
{
    cout<<p->name<<"\t\t\t"<<p->runtime<<"\t\t"<<p->needtime<<" \t"<<p->Counter<<"\t"<<p->round<<"\t"<<p->state<<endl;
}
void display()
{
    PCB *p;
    if(run != NULL)
        Prinft(run);
    Dtime(1);
    p = ready;
    while(p != NULL)
    {
        Prinft(p);
        p = p->next;
    }
    Dtime(1);
    p = finish;
    while(p != NULL)
    {
        Prinft(p);
        p = p->next;
    }
    cout<<"--------------------------------------------------------------\n";
}

void queue(PCB *p)
{
    if(ready == NULL)
    {
        p->next = NULL;
        ready = p;
        tial = p;
    }
    else
    {
        tial->next = p;
        tial = p;
        p->next = NULL;
    }
}

bool CTProcessOfRuntime()
{
    PCB * Node;
    int m;
    cout <<"输入创建进程的数目:"<<endl;
    cin >>n;
    cout <<"输入时间片:"<<endl;
    cin >>m;
    for(int j = 0;j < n; j++)
    {
        Node = new PCB;
        if(Node==NULL)
            return false;
        else
        {
            cout <<"输入进程的名称,进程需CPU时间:"<<endl;
            cin >>Node->name>>Node->needtime;
            Node->runtime = 0;
            Node->state ="就绪";
            Node->Counter = 0;
            Node->round = m;
          //  cout <<"进程"<name<<"创建完毕!"<
        }
        queue(Node);
    }
    return true;
}

void Runtime()
{
    run = ready;
    ready = ready->next;
    run->state = "运行";
    Prinft();
    while(run!=NULL)
    {
        run->runtime = run->runtime+1;
        run->needtime = run->needtime-1;
        run->Counter = run->Counter + 1;
        if(run->needtime == 0)
        {
            run->state = "完成";
            run->next = finish;
            finish = run;
            run = NULL;
            if(ready!=NULL)
            {
                run = ready;
                run->state = "运行";
                ready = ready->next;
            }
        }
        else if(run->Counter == run->round)
        {
            run->Counter = 0;
            run->state = "就绪";
            queue(run);
            run=NULL;
            if(ready!=NULL)
            {
                run = ready;
                run->state = "运行";
                ready = ready->next;
            }
        }
        display();
    }
}

int main()
{
    CTProcessOfRuntime();
    Runtime();
    return 0;
}

void Dtime(int t)
{
    time_t current_time;
    time_t start_time;
    time(&start_time);
    do
    {
        time(& current_time);
    }while((current_time-start_time)<t);
}

你可能感兴趣的:(操作系统(大二下))