进程调度模拟算法

 

一、实验目的

通过用高级语言编写模拟进程调度程序,以便加深理解有关进程控制块、进程队列等概念,并体会和了解优先数算法和时间片轮转算法的具体实施办法。

1.进程控制块结构如下:

       NAME——进程标示符

       PRIO/ROUND——进程优先数/进程每次轮转的时间片数(设为常数2)

       CPUTIME——进程累计占用CPU 的时间片数

       NEEDTIME——进程到完成还需要的时间片数

       STATE——进程状态

       NEXT——链指针

     注:1. 为了便于处理,程序中进程的的运行时间以时间片为单位进行计算;

         2. 各进程的优先数或轮转时间片数,以及进程运行时间片数的初值,均由用户在程序运行时给定。

(二)进程的就绪态和等待态均为链表结构,共有四个指针如下:

       RUN——当前运行进程指针

       READY——就需队列头指针

       TAIL—— 就需队列尾指针

       FINISH—— 完成队列头指针

(三)程序说明

       1. 在优先数算法中,进程优先数的初值设为:

          50-NEEDTIME每执行一次,优先数减1,CPU 时间片数加1,进程还需要的时间片数减1。

          在轮转法中,采用固定时间片单位(两个时间片为一个单位),进程每轮转一次,CPU时间片数加2,进程还需要的时间片数减2,并退出CPU,排到就绪队列尾,等待下一次调度。

       2. 程序的模块结构提示如下:

          整个程序可由主程序和如下7 个过程组成:

         1)INSERT1——在优先数算法中,将尚未完成的PCB 按优先数顺序插入到就绪队列中;

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

 

         3)FIRSTIN——调度就绪队列的第一个进程投入运行;

         4)PRINT——显示每执行一次后所有进程的状态及有关信息。

         5)CREATE——创建新进程,并将它的PCB 插入就绪队列;

         6)PRISCH——按优先数算法调度进程;

         7)ROUNDSCH——按时间片轮转法调度进程。

       主程序定义PCB 结构和其他有关变量。

四、实验结果

优先级调度算法

 

循环轮转调度算法

 

代码

#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;

}

 

posted @ 2025-04-17 21:40  涨涨涨张  阅读(10)  评论(0)    收藏  举报