c++ 模拟进程调度 (链表实现)

所用的数据结构:
1.struct Process{
    string proname;
    int time,pri;
    bool status;    
}; 包含了进程名,优先权,运行时间和运行状态的结构体。         
2.struct Node {
    Process val;
    Node *next;
    Node(Process x):val(x),next(NULL){}
};定义链表结点。
3.class Linklist{
    public:
        Node *head;
        Linklist();
        void Insert(Process val,int pos);
        void Remove(Process val);
        void Print();
        int  Find(Process val);
        int  Length();
        ~Linklist();
    private:

        int length; 

};创建链表类,根据方法名可知其意。方法实现通过内联函数。详见linklish.h。

遇到的两个误区:
1.-> 和 . 的区别 ->前面是指针,比如 temp->,而 . 后面跟的是结构体内容,例如val.proname 。
2.整个程序处理的是node结点连接成的链表,并没有改变原来的结构体内容。
算法流程图注释
1.初始化:将各个进程数据输入后,利用c++自带sort函数,对pro数组进行第一次排序,按pri的大小顺序;
2.此处移除temp结点,因为temp在这为头结点,在linklist类中的remove函数对删除头结点的处理是,head = head->next,所以不用担心头结点未发生改变引发错误。

算法流程图

题目
设计一个按优先数调度算法实现处理器调度的程序。
(1) 假定系统有五个进程,每一个进程用一个进程控制块PCB来代表,进程控制块的格式为:
进程名
指针
要求运行时间
优先数
状态
其中,进程名——作为进程的标识,假设五个进程的进程名分别为P1,P2,P3,P4,P5。
指针——按优先数的大小把五个进程连成队列,用指针指出下一个进程的进程控制块的首地址,最后一个进程中的指针为“0”。
要求运行时间——假设进程需要运行的单位时间数。
优先数——赋予进程的优先数,调度时总是选取优先数大的进程先执行。
状态——可假设有两种状态,“就绪”状态和“结束”状态。五个进程的初始状态都为“就绪”,用“R”表示,当一个进程运行结束后,它的状态为“结束”,用“E”表示。
(2) 在每次运行你所设计的处理器调度程序之前,为每个进程任意确定它的“优先数”和“要求运行时间”。
(3) 为了调度方便,把五个进程按给定的优先数从大到小连成队列。用一单元指出队首进程,用指针指出队列的连接情况。例:
队首标志
K2
K1 P1 K2 P2 K3 P3 K4 P4 K5 P5
0 K4 K5 K3 K1
2 3 1 2 4
1 5 3 4 2
R R R R R
PCB1 PCB2 PCB3 PCB4 PCB5

(4) 处理器调度总是选队首进程运行。采用动态改变优先数的办法,进程每运行一次优先数就减“1”。由于本实验是模拟处理器调度,所以,对被选中的进程并不实际的启动运行,而是执行:
优先数-1
要求运行时间-1
来模拟进程的一次运行。
提醒注意的是:在实际的系统中,当一个进程被选中运行时,必须恢复进程的现场,让它占有处理器运行,直到出现等待事件或运行结束。在这里省去了这些工作。
(5) 进程运行一次后,若要求运行时间¹0,则再将它加入队列(按优先数大小插入,且置队首标志);若要求运行时间=0,则把它的状态修改成“结束”(E),且退出队列。
(6) 若“就绪”状态的进程队列不为空,则重复上面(4)和(5)的步骤,直到所有进程都成为“结束”状态。
(7) 在所设计的程序中应有显示或打印语句,能显示或打印每次被选中进程的进程名以及运行一次后进程队列的变化。
(8) 为五个进程任意确定一组“优先数”和“要求运行时间”,启动所设计的处理器调度程序,显示或打印逐次被选中进程的进程名以及进程控制块的动态变化过程。

主程序代码

//hanZheng's Process Scheduling
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <sstream>
#include "Linklist.h"
using namespace std;

int cmp(const Process &a ,const Process &b){
    return a.pri>b.pri;
}
int main(){
    Process pro[6];
    string str;
    int time,pri;
    for(int i=1;i<=5;i++){
        cout<<"进程p"<<i<<"的"<<endl;
        stringstream ss;               //流字符串转换i为字符,也可以用spritf,或者直接char数组 
        ss<<i;
        str=ss.str();
        pro[i].proname = "P"+str;
        cout<<"输入需要运行时间:"; 
        cin>>time;
        pro[i].time = time;
        cout<<"输入进程优先级:"; 
        cin>>pri;
        pro[i].pri = pri;
        pro[i].status = "R";     //true 为待执行 
        cout<<endl; 
    }

    Linklist li;


    sort(pro+1,pro+5+1,cmp);
    for(int i = 1;i<=5;i++){   //初始化 ,pri从大到小 
        li.Insert(pro[i],i-1);
        //li.Print();   
    }
    Node *temp = li.head;  //测试链表性能 
//  temp = temp->next;
//  li.Remove(temp->val);
//  li.Print();
    cout<<"当前进程序列:\n" ;
    li.Print();
    while(temp!=NULL){              //调度主程序 
        int ipos=-1;
        Node *temp = li.head;
        if(temp->val.time>0){          //执行一次的操作 
            temp->val.pri--;
            temp->val.time--;

        } 
        if(temp->val.time==0){                  
            cout<<"after Excute process ----"<<temp->val.proname<<endl;
            temp->val.status = "E"; 
            cout<<"进程----"<<temp->val.proname<<"状态为:"<<temp->val.status<<" 已经运行结束"<<endl;
            li.Remove(temp->val);
        }
        else{

            cout<<"after Excute process ----"<<temp->val.proname<<endl; 
            li.Remove(temp->val);
        //  cout<<"Head name:"<<li.head->val.proname<<endl;
            Node *temp2 =li.head;

            for(int j=0;j<=5;j++){


                if(temp2==NULL){             //如果只剩一个节点了,rm后队列为空,插0位置 
                    ipos=0;
                    break;
                }   
                if((temp->val.pri)>temp2->val.pri){   
                    ipos=j;

                    break;
                }
                if(temp2->next==NULL){        //遍历到队尾了,加到最后 
                    ipos=li.Length();

                    break;
                }
        //      printf("j= %d temp.pri=%d temp2.pri=%d\n",j,temp->val.pri,temp2->val.pri);
                temp2 = temp2->next;
            }
        //  printf("ipos=%d\n",ipos);
        //  cout<<"Insert node name"<<temp->val.proname<<endl;
            li.Insert(temp->val,ipos);

        }
        if(li.head==NULL){
            cout<<"\n--------------The process is over!-------------"<<endl;
            break;
        }
    //  cout<<"name:"<<li.head->val.proname<<endl;

    //  cout<<"temp name:"<<temp->val.proname<<endl;
        li.Print();
    } 

}

链表类代码

//linklist.h
/*hanzheng 's linklist class*/
#include <string>
#include <iostream>
using namespace std;

struct Process{
    string proname;
    int time,pri;
    string status;  
};


struct Node {
    Process val;
    Node *next;
    Node(Process x):val(x),next(NULL){}
};

class Linklist{
    public:
        Node *head;
        Linklist();
        void Insert(Process val,int pos);
        void Remove(Process val);
        void Print();
        int  Find(Process val);
        int  Length();
        ~Linklist();
    private:

        int length; 


};

Linklist::Linklist(){
    head=NULL;
    length =0;
}

Linklist::~Linklist(){
    Node *temp;
    for (int i=0;i<length;i++){
        temp=head;
        head=head->next;
        delete temp;

    }   
}

int Linklist::Length(){

    return length;

}

void Linklist::Insert(Process val,int pos){                  //pos 是两个节点的间隔,从0开始 
    if (pos<0){

        cout<<"error:position is less than 0 \n ";
        return;
    }

    int index=1;
    Node *temp = head;
    Node *node = new Node(val);

    if (pos==0){
        node->next=temp;
        head = node;
        length++;
        return;

    }
    while(temp!=NULL&&index<pos){
        index++;
        temp =temp->next;

    }

    if(temp == NULL){
        cout<<"error: pos was not exsist\n";
        return;
    }

    node->next=temp->next;
    temp->next=node;
    length++;

}

int Linklist::Find(Process val){              //返回值是第几个节点,从1开始 

    Node *temp = head;
    int index =1;
    while(temp!=NULL){
        if(temp->val.proname==val.proname){
            return index;
        }
        temp = temp->next;
        index++;
    }
    return -1;
}

void Linklist::Remove(Process val){

    int pos = Find(val);

    if(pos==-1){
        cout<<"Remove failed"<<endl;
        return;
    }
    if(pos==1){
        head = head->next;
        length--;
        return;
    }
    Node *temp = head;

    int index =1;
    while(temp!=NULL&&index<pos-1){
        index++;
        temp = temp->next;      
    }
    temp->next = temp->next->next;
    length--;


}

void Linklist::Print(){

    if(head==NULL){
        cout<<"Linklist is empty\n";
        return;
    }
    Node *temp = head;

    while(temp!=NULL){
        cout<<temp->val.proname<<",优先级:"<<temp->val.pri<<",所需时间:"<<temp->val.time<<",状态:"<<temp->val.status<<endl;
        temp=temp->next;

    }
    cout<<endl;
}





  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值