• 进程状态转换及其PCB的变化


    代码实现了模拟进程状态转换及其相应PCB内容、组织结构的变化。

    #include<iostream>
    using namespace std;
    
    typedef struct pcb{
        int id;
        struct pcb *next;
    }pcb;
    
    pcb *ready,*run,*block;
    
    pcb* cre()//创建带头结点的ready队列
    {
        pcb *head=new pcb;
        head->next=NULL;
        pcb *q=head;
        int n;
        cout<<"输入进程数:
    ";
        cin>>n;
        for(int i=0;i<n;i++)
        {
            pcb *p=new pcb;
            cin>>p->id;
            p->next=NULL;
            q->next=p;
            q=p;
        }
        return head;
    }
    
    void ins(pcb *head,pcb *node)//插入节点
    {
        pcb *p=head;
        while(p->next)
        {
            p=p->next;
        }
        pcb *n=new pcb;
        n->id=node->id;
        p->next=n;
        n->next=NULL;
    }
    
    void del(pcb *head)
    {
        pcb *p=head->next;
        if(p)
        {
        head->next=head->next->next;
        delete p;
        }
    }
    
    void dis()
    {
        pcb *p;
        p=ready->next;
        cout<<"ready:	";
        while(p)
        {
            cout<<p->id<<" ";
            p=p->next;
        }
        cout<<endl;
        p=run->next;
        cout<<"run  :	";
        while(p)
        {
            cout<<p->id<<" ";
            p=p->next;
        }
        cout<<endl;
        p=block->next;
        cout<<"block:	";
        while(p)
        {
            cout<<p->id<<" ";
            p=p->next;
        }
        cout<<endl;
    }
    
    int main()
    {
        ready=cre();
        run=new pcb;
        run->next=NULL;
        block=new pcb;
        block->next=NULL;
        dis();
        int i;
        cout<<"chose:	1:ready->run	2:run->ready	3:run->block	4:block->ready
    ";
        while(cin>>i)
        {
            if(i==1)
            {
                if(ready->next)
                {
                    if(run->next)
                    {
                        ins(block,run->next);
                        del(run);
                    }
                    ins(run,ready->next);
                    del(ready);
                }
                else
                {
                    if(block->next)
                    {
                        ins(block,run->next);
                        del(run);
                        ins(run,block->next);
                        del(block);
                    }
                }
            }
            else if(i==2)
            {
                if(run->next)
                {
                    if(ready->next)
                    {
                        ins(ready,run->next);
                        del(run);
                        ins(run,ready->next);
                        del(ready);
                    }
                }
            }
            else if(i==3)
            {
                if(run->next)
                {
                    if(ready->next)
                    {
                        ins(block,run->next);
                        ins(run,ready->next);
                        del(run);
                        del(ready);
                    }
                    else
                    {
                        ins(block,run->next);
                        ins(run,block->next);
                        del(block);
                        del(run);
                    }
    
                }
            }
            else if(i==4)
            {
                if(block->next)
                {
                    ins(ready,block->next);
                    del(block);
                }
            }
            else
                break;
            dis();
            cout<<"chose:	1:ready->run	2:run->ready	3:run->block	4:block->ready
    ";
        }
        return 0;
    }
    



  • 相关阅读:
    0X03异常错误处理
    (组合数学)AtCoder Grand Contest 019 F
    (NTT)AtCoder Grand Contest 019 E
    (dp)AtCoder Grand Contest 019 D
    (dp)AtCoder Regular Contest 081 E
    (最小费用流)hdu 6118(2017百度之星初赛B 1005) 度度熊的交易计划
    (容斥)Codeforces Round #428 (Div. 2) D. Winter is here
    (最大团)Codeforces Round #428 (Div. 2) E. Mother of Dragons
    (FFT)HDU 6088(2017 多校第5场 1004)Rikka with Rock-paper-scissors
    近期部分题目汇总
  • 原文地址:https://www.cnblogs.com/deepspace/p/10260735.html
Copyright © 2020-2023  润新知