OS进程调度实验

来源:互联网 发布:图标制作软件手机版 编辑:程序博客网 时间:2024/05/29 03:50

OS进程调度实验

一、实验目的
通过实验加强对进程调度算法的理解和掌握。
二、实验内容
编写程序实现基于优先级的时间片轮转调度算法。
三、实验要求
1、假定系统有5个进程,每个进程用一个进程控制块PCB来代表,进程控制块的结构如下图1.1所示:

进程名优先级要求运行时间已运行时间进程状态指针其中:进程名:作为进程的标识,假设五个进程的进程名分别为p1,p2,p3,p4,p5。指针:进程按顺序排成循环链表,用指针指出下一个进程的进程控制块首地址,最后一个进程中的指针指出第一个进程的进程控制块首地址。要求运行时间:假设进程需要运行的单位时间数。已运行时间:假设进程已经运行的单位时间数,初值为0。状态:可假设有两种状态,就绪状态和结束状态。进程的初始状态都为就绪状态。

2、每次运行所设计的处理器调度程序调度进程之前,为每个进程随机确定它的要求运行时间。
3、此程序是模拟处理器调度,因此,被选中的进程并不实际启动运行,而是执行

已运行时间+1来模拟进程的一次运行,表示进程已经运行过一个单位时间。

4、在所设计的程序中应有显示语句,能显示每次被选中的进程名以及运行一次后进程队列的变化。

  • 再次犯错了!pre是指针变量,可以修改pre向的内容,但修改pre的指向是不会影响外部的!
    PCB*getHp(PCB *pre);
  • code:
#include<iostream>#include<cstring>#include<cstdlib>using namespace std;struct PCB{    char name[20];    int priority;    int all_time;    int run_time;    int status;//1 ready 0 stop    PCB* next;    PCB(){        priority=0;        all_time=run_time;        status=1;    }};const int piece=10;PCB *pcb=NULL;PCB* getHp(PCB* pre);void  create(PCB*p){    if(pcb==NULL)    {      pcb=p;      pcb->next=pcb;    }    PCB*pn=pcb;    while(pcb!=pn->next)pn=pn->next;    pn->next=p;    p->next=pcb;}void run(){    PCB*pre=pcb;    PCB*p=pcb->next;    while(pcb!=NULL)    {        pre=getHp(pre);        p=pre->next;        cout<<p->name<<" is running!"<<endl;        int i=0;        while(++i)        {            p->run_time++;            if(p->run_time>=p->all_time)            {                cout<<p->name<<" over!"<<"all_time"<<  \                    p->all_time<<" running time:"<<p->run_time<<endl;                p->status=0;                if(p->next==p)                {                    delete p;                    pcb=NULL;                    break;                }else                {                    PCB*pdone=p;                    pre->next=p->next;                    p=p->next;                    delete pdone;                    break;                }            }else if(i>piece)//            {                cout<<"piece time done!"<<endl;                pre=pre->next;                p=pre->next;                break;            }        }    }    cout<<"no process is running!"<<endl;}//ret:next param:pre-pre of next//pre->next==p//再次犯错了!pre是指针变量,可以修改pre向的内容,但修改pre的指向是不会影响外部的!PCB*getHp(PCB *pre){    PCB*hp=pre;//记录优先级最高的pcb    PCB*pn=pre->next;//迭代    while(pre!=pn)    {        if(pn->next->priority>hp->next->priority)          hp=pn;        pn=pn->next;    }    pre=hp;    return pre;}int main(){    cout<<"---------create process---------"<<endl;    char name[5]="pc0";    for(int i=0;i<5;i++)    {        PCB* p=new PCB();        p->priority=rand()%10;        name[2]='0'+i;        strcpy(p->name,name);        p->all_time=rand()%50;        cout<<p->name<<" priority:"<<p->priority<<"`all_time:"<<p->all_time<<endl;        create(p);    }    cout<<"------------run---------------"<<endl;    //时间片    run();    cout<<"------------exit---------------"<<endl;    return 0;}/*output:---------create process---------pc0 priority:3`all_time:36pc1 priority:7`all_time:15pc2 priority:3`all_time:35pc3 priority:6`all_time:42pc4 priority:9`all_time:21------------run---------------pc4 is running!piece time done!pc4 is running!pc4 over!all_time21 running time:21pc1 is running!piece time done!pc1 is running!pc1 over!all_time15 running time:15pc3 is running!piece time done!pc3 is running!piece time done!pc3 is running!piece time done!pc3 is running!pc3 over!all_time42 running time:42pc0 is running!piece time done!pc2 is running!piece time done!pc0 is running!piece time done!pc2 is running!piece time done!pc0 is running!piece time done!pc2 is running!piece time done!pc0 is running!pc0 over!all_time36 running time:36pc2 is running!pc2 over!all_time35 running time:35no process is running!------------exit---------------; */
0 0
原创粉丝点击