进程调度模拟实验

it2022-05-05  119

 

#include<bits/stdc++.h>

using namespace std;int cal=0;

struct PCB{    // bool use;//是否可用    //bool sta;//是否就绪    int  pronum;//进程号(进程唯一标识符,不可重复)    int time;//时间片    PCB* next;};void createList(PCB* &pHead){    pHead = new PCB;    pHead->pronum= -1;    // pHead->use=false;    pHead->time=5;    pHead->next = NULL;    return;}void print(PCB* pHead){

    PCB* pCur = pHead->next;    while(pCur != NULL)    {        cout<<pCur->pronum<<" ";        pCur = pCur->next;    }    cout<<endl;    return;}void delet(PCB* &pHeadR,PCB* &pHeadB,PCB* &pHeadD,int id){    PCB* pCurR = pHeadR;    PCB* pCurB = pHeadB;    PCB* pCurD = pHeadD;

    while(pCurR->next != NULL)    {        if(pCurR->next->pronum==id)        {            pCurR->next = pCurR->next->next;            return;        }        pCurR = pCurR->next;    }    while(pCurB->next != NULL)    {        if(pCurB->next->pronum==id)        {            pCurB->next = pCurB->next->next;            return;        }        pCurB = pCurB->next;    }    if(pCurD->next->pronum==id)    {        pCurD->next = pCurD->next->next;        return;    }    cout<<"该进程不存在,无法删除!"<<endl;}void destoryList(PCB* pHead){    assert(pHead!=NULL);//如果它的条件返回错误,则终止程序执行    PCB* pNext = pHead->next;    while(pNext != NULL)    {        delete pHead;        pHead = pNext;        pNext = pHead->next;    }    delete pHead;    pHead = NULL;    return;}void create(PCB* pHead){    PCB* pCur = pHead;

    while(pCur->next != NULL)    {        pCur = pCur->next;    }    PCB* pNewNode = new PCB;    pNewNode->pronum = cal;    pNewNode->time=5;    //pNewNode->use=true;    pNewNode->next = NULL;    pCur->next=pNewNode;    cal++;}bool empty(PCB* pHead){    if(pHead->next != NULL)    {        return false;    }    else        return true;}

void insert(PCB* &pPush,PCB* &pPop){

    PCB* pCurU = pPush;    PCB* pCurO = pPop;    PCB* ptem=new PCB;    ptem=pCurO->next;    pCurO->next=pCurO->next->next;    while(pCurU->next != NULL)    {        pCurU = pCurU->next;    }    pCurU->next=ptem;    ptem->next=NULL;}bool timeout(PCB* pCheck){    if(pCheck->next!=NULL)    {        pCheck=pCheck->next;        pCheck->time=pCheck->time-1;

        if(pCheck->time==0)        {            pCheck->time=5;            return true;        }        else return false;    }    else return false;}int main(){    cout<<"          **************************************************"<<endl;    cout<<"          **               进程调度模拟实验               **"<<endl;    cout<<"          **************************************************"<<endl;    PCB* Ready = NULL;    PCB* Block = NULL;    PCB* Doing = NULL;    createList(Ready);    createList(Block);    createList(Doing);    char chose;    int id;    do    {        if(timeout(Doing))        insert(Ready,Doing);        if(empty(Doing)&&!empty(Ready))  insert(Doing,Ready);        cout<<endl<<"当前进程在队列中的情况:"<<endl;        cout<<"就绪态:";        print(Ready);        cout<<"阻塞态:";        print(Block);        cout<<"执行态:";        print(Doing);        cout<<endl;        cout<<"................................................................."<<endl;        cout<<"请输入要进行的操作:"<<endl<<"1.创建  2.撤销  3.阻塞  4.唤醒 0.退出"<<endl;        cout<<"================================================================"<<endl;        cin>>chose;        switch(chose)        {        case '1':            create(Ready);            break;        case '2':            cout<<"请输入删除id"<<endl;            cin>>id;            delet(Ready,Block,Doing,id);            break;        case '3':            if(!empty(Doing))                insert(Block,Doing);            else                cout<<"没有执行态进程,无法阻塞"<<endl;            break;        case '4':            if(!empty(Block))                insert(Ready,Block);            else                cout<<"没有阻塞态进程,无法唤醒"<<endl;            break;        case '0':            destoryList(Doing);            destoryList(Block);            destoryList(Ready);            exit(0);        default:            cout<<"该操作不存在,请重新输入"<<endl;        }    }    while(1);    return 0;}

转载于:https://www.cnblogs.com/jiangnanyanyuchen/p/6758842.html

相关资源:计算机操作系统—— 进程调度模拟实验 报告

最新回复(0)