进程调度模拟算法

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

编写两种调度算法程序:

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

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

#include <iostream>
#include <string>
#pragma warning(disable:4996)

using namespace std;

class PCB
{
public:
    char STATE;         // 进程状态 state
    char NAME[20];      //进程标示符 name
    int PRIO;           // ROUND进程优先数 rank
    int ROUND;          //进程每次轮转的时间片数(设为常数2)
    int CPUTIME;        //进程累计占用CPU的时间片数 runtime
    int NEEDTIME;       //进程到完成还需要的时间片数 needtime
    PCB* NEXT;          // 链指针 next
    PCB(char name[20], int prio, int needtime) {
        strcpy(NAME, name);
        STATE = 'W';
        PRIO = prio;
        NEEDTIME = needtime;
        CPUTIME = 0;
        NEXT = NULL;
    }
    PCB(){}
};
PCB* ready = NULL, * now = NULL,* endlist=NULL,* end_0=NULL;

void output(PCB* point)
{
    cout << ".................................................";
    cout << "\n name \t state \t rank \t needtime \t runtime" << endl;
    cout << "|" << point->NAME << "\t";
    cout << "|" << point->STATE << "\t";
    cout << "|" << point->PRIO << "\t";
    cout << "|" << point->NEEDTIME << "\t\t";
    cout << "|" << point->CPUTIME << "\t" << endl;
    cout << "................................................." << endl;
}

//进程依次排序
void sort(PCB* point)
{
    if (ready == NULL)//若链表为空
    {
        ready = point;
    }
    else
    {
        PCB* point1 = ready, * point2 = ready->NEXT;
        if (point->PRIO > point1->PRIO)//若为优先等级最高,放在队首
        {
            point->NEXT = ready;
            ready = point;
        }
        else
        {
            while (point2 != NULL)//遍历链表,直到找到合适位置
            {
                if (point->PRIO > point2->PRIO)
                {
                    point1->NEXT = point;
                    point->NEXT = point2;
                    break;
                }
                point1 = point2;
                point2 = point2->NEXT;
            }
            if (point2 == NULL)//若为优先等级最低,放在队尾
            {
                point1->NEXT = point;
            }
        }
    }
}

//进程结束函数,打印已完成进程,释放内存
void finish(PCB* point)
{
    point->STATE = 'F';
    cout << "################################################" << endl;
    cout << "**************** 进程" << point->NAME << "已经完成 *****************" << endl;
    cout << "\n name \t state \t rank \t needtime \t runtime" << endl;
    cout << "|" << point->NAME << "\t";
    cout << "|" << point->STATE << "\t";
    cout << "|" << point->PRIO << "\t";
    cout << "|" << point->NEEDTIME << "\t\t";
    cout << "|" << point->CPUTIME << "\t" << endl;
    cout << "################################################" << endl;
    delete(point);
    point = NULL;
}

//运行一个单位时间
void running()
{
    now = ready;
    ready = ready->NEXT;
    now->NEXT = NULL;
    now->STATE = 'R';
    now->PRIO--;
    now->CPUTIME++;

    if (now->NEEDTIME == now->CPUTIME)
    {
        //完成队列
        PCB* endflag=new PCB();
        endflag->CPUTIME = now->CPUTIME;
        for (int i = 0; i < 20; i++)
        {
            endflag->NAME[i] = now->NAME[i];
        }
        endflag->NEEDTIME = now->NEEDTIME;
        endflag->NEXT = NULL;
        endflag->PRIO = now->PRIO;
        endflag->ROUND = now->ROUND;
        endflag->STATE = 'F';
        endlist->NEXT=endflag;
        endlist = endflag;
        //若当前进程执行完毕
        finish(now);
        now = NULL;
    }
    else
    {
        //若当前进程没有执行完毕
        cout << "************* 当前正在运行的进程是 **************" << endl;
        output(now);
    }
    //打印就绪队列
    cout << "************* 当前就绪队列状态为 ***************" << endl;
    PCB* point = ready;
    while (point != NULL)
    {
        output(point);
        point = point->NEXT;
    }
    //打印完成队列
    cout << "************* 当前完成队列状态为 ***************" << endl;
    point = end_0->NEXT;
    while (point->NEXT!= NULL)
    {
        output(point->NEXT);
        point = point->NEXT;
    }
    //把没执行完的进程重新放入队列
    if (now != NULL)
    {
        now->STATE = 'W';
        sort(now);
    }
}
int main()
{
    //freopen("in.txt", "r", stdin);
    end_0 = new PCB();
    endlist = new PCB();
    end_0->NEXT = endlist;
    int t;
    cout << "**** 输入进程数目:";
    cin >> t;
    cout << endl;
    //进程信息输入
    for (int i = 1; i <= t; i++)
    {
        PCB* temp;
        char name[20]; int prio; int needtime;
        cout << "**** 请输入第" << i << "个进程的信息 ****" << endl;
        cout << "请输入该进程的名字:";
        cin >> name;
        cout << "请输入该进程的优先等级:";
        cin >> prio;
        cout << "请输入该进程需要的运行时间:";
        cin >> needtime;
        temp = new PCB(name, prio, needtime);
        sort(temp);
        temp = NULL;
        cout << endl;
    }

    //打印执行前的就绪队列
    cout << "************* 执行前就绪队列排序 ***************" << endl;
    PCB* point = ready;
    while (point != NULL)
    {
        output(point);
        point = point->NEXT;
    }
    cout << "************ 程序准备正式开始执行 **************" << endl;

    //程序开始执行
    char ch;
    while (now != NULL || ready != NULL)
    {
        //一次运行一个单位时间
        cout << "\n\n 按任一键继续......" << endl;
        ch = getchar();
        running();
        cout << endl;
    }
    //所有进程完成
    cout << "\n************************************************" << endl;
    cout << "************* 所有进程均已执行完成 *************" << endl;
    cout << "************************************************\n" << endl;
    return 0;
}
进程调度时间片轮转算法:
#include <iostream>
#include <string>
#pragma warning(disable:4996)

using namespace std;

class PCB
{
public:
    char STATE;         // 进程状态 state
    char NAME[20];      //进程标示符 name
    int PRIO;           // ROUND进程优先数 rank
    int ROUND;          //进程每次轮转的时间片数(设为常数2)
    int CPUTIME;        //进程累计占用CPU的时间片数 runtime
    int NEEDTIME;       //进程到完成还需要的时间片数 needtime
    PCB* NEXT;          // 链指针 next
    PCB(char name[20], int prio, int needtime) {
        strcpy(NAME, name);
        STATE = 'W';
        PRIO = prio;
        NEEDTIME = needtime;
        CPUTIME = 0;
        NEXT = NULL;
    }
    PCB() {}
};
PCB* ready = NULL, * now = NULL, * endlist = NULL, * end_0 = NULL;

void output(PCB* point)
{
    cout << ".................................................";
    cout << "\n name \t state \t rank \t needtime \t runtime" << endl;
    cout << "|" << point->NAME << "\t";
    cout << "|" << point->STATE << "\t";
    cout << "|" << point->PRIO << "\t";
    cout << "|" << point->NEEDTIME << "\t\t";
    cout << "|" << point->CPUTIME << "\t" << endl;
    cout << "................................................." << endl;
}

//进程依次排序
void sort(PCB* point)
{
    if (ready == NULL)//若链表为空
    {
        ready = point;
    }
    else
    {
        PCB* point1 = ready, * point2 = ready->NEXT;
        while (point2 != NULL)//遍历链表,直到找到合适位置
        {
            point1 = point2;
            point2 = point2->NEXT;
        }
        if (point2 == NULL)//若为优先等级最低,放在队尾
        {
            point1->NEXT = point;
        }

    }
}

//进程结束函数,打印已完成进程,释放内存
void finish(PCB* point)
{
    point->STATE = 'F';
    cout << "################################################" << endl;
    cout << "**************** 进程" << point->NAME << "已经完成 *****************" << endl;
    cout << "\n name \t state \t rank \t needtime \t runtime" << endl;
    cout << "|" << point->NAME << "\t";
    cout << "|" << point->STATE << "\t";
    cout << "|" << point->PRIO << "\t";
    cout << "|" << point->NEEDTIME << "\t\t";
    cout << "|" << point->CPUTIME << "\t" << endl;
    cout << "################################################" << endl;
    delete(point);
    point = NULL;
}

//运行一个单位时间
void running()
{
    now = ready;
    ready = ready->NEXT;
    now->NEXT = NULL;
    now->STATE = 'R';
    now->PRIO--;
    now->CPUTIME++;

    if (now->NEEDTIME == now->CPUTIME)
    {
        //完成队列
        PCB* endflag = new PCB();
        endflag->CPUTIME = now->CPUTIME;
        for (int i = 0; i < 20; i++)
        {
            endflag->NAME[i] = now->NAME[i];
        }
        endflag->NEEDTIME = now->NEEDTIME;
        endflag->NEXT = NULL;
        endflag->PRIO = now->PRIO;
        endflag->ROUND = now->ROUND;
        endflag->STATE = 'F';
        endlist->NEXT = endflag;
        endlist = endflag;
        //若当前进程执行完毕
        finish(now);
        now = NULL;
    }
    else
    {
        //若当前进程没有执行完毕
        cout << "************* 当前正在运行的进程是 **************" << endl;
        output(now);
    }
    //打印就绪队列
    cout << "************* 当前就绪队列状态为 ***************" << endl;
    PCB* point = ready;
    while (point != NULL)
    {
        output(point);
        point = point->NEXT;
    }
    //打印完成队列
    cout << "************* 当前完成队列状态为 ***************" << endl;
    point = end_0->NEXT;
    while (point->NEXT != NULL)
    {
        output(point->NEXT);
        point = point->NEXT;
    }
    //把没执行完的进程重新放入队列
    if (now != NULL)
    {
        now->STATE = 'W';
        sort(now);
    }
}
int main()
{
    //freopen("in.txt", "r", stdin);
    end_0 = new PCB();
    endlist = new PCB();
    end_0->NEXT = endlist;
    int t;
    cout << "**** 输入进程数目:";
    cin >> t;
    cout << endl;
    //进程信息输入
    for (int i = 1; i <= t; i++)
    {
        PCB* temp;
        char name[20]; int prio; int needtime;
        cout << "**** 请输入第" << i << "个进程的信息 ****" << endl;
        cout << "请输入该进程的名字:";
        cin >> name;
        cout << "请输入该进程的优先等级:";
        cin >> prio;
        cout << "请输入该进程需要的运行时间:";
        cin >> needtime;
        temp = new PCB(name, prio, needtime);
        sort(temp);
        temp = NULL;
        cout << endl;
    }

    //打印执行前的就绪队列
    cout << "************* 执行前就绪队列排序 ***************" << endl;
    PCB* point = ready;
    while (point != NULL)
    {
        output(point);
        point = point->NEXT;
    }
    cout << "************ 程序准备正式开始执行 **************" << endl;

    //程序开始执行
    char ch;
    while (now != NULL || ready != NULL)
    {
        //一次运行一个单位时间
        cout << "\n\n 按任一键继续......" << endl;
        ch = getchar();
        running();
        cout << endl;
    }
    //所有进程完成
    cout << "\n************************************************" << endl;
    cout << "************* 所有进程均已执行完成 *************" << endl;
    cout << "************************************************\n" << endl;
    return 0;
}

 

posted @ 2022-05-17 07:51  风吹过半夏  阅读(325)  评论(0编辑  收藏  举报