操作系统

#include <bits/stdc++.h>
using namespace std;
struct pcb
{
    int id;//进程id
    int priority;//优先权
    int status;//状态0执行1就绪2阻塞
    int last;//执行所需时间
    bool operator < (const pcb &r)const
    {
        if(priority!=r.priority) return priority>r.priority;
        return id<r.id;
    }
};
set<pcb>jxt,zst;
int getinput,idtot;
char refer[3][10]={"执行态","就绪态","阻塞态"};
int main()
{
    while(1)//while一次过去1个时间单位
    {
        printf("请输入您想进行的操作(1创建、2阻塞、3唤醒):\n");
        scanf("%d",&getinput);
        if(getinput==1)//创建进程
        {
            pcb newjc;
            printf("您要创建的进程的id为:%d\n",++idtot);
            newjc.id=idtot;
            printf("请输入您要创建的进程的优先级\n");
            scanf("%d",&newjc.priority);
            printf("请输入您要创建的进程的执行所需时间\n");
            scanf("%d",&newjc.last);
            newjc.status=1;
            jxt.insert(newjc);
            printf("进程创建成功,已进入就绪队列\n");
        }
        else if(getinput==2)//阻塞进程
        {
            if(jxt.size())
            {
                printf("id为%d的进程被阻塞\n",(*jxt.begin()).id);
                zst.insert(*jxt.begin());
                jxt.erase(jxt.begin());
            }
            else
            {
                printf("没有正在执行的进程,不能阻塞\n");
                continue;
            }
        }
        else//唤醒进程
        {
            if(zst.size())
            {
                printf("id为%d的进程被唤醒\n",(*zst.begin()).id);
                jxt.insert(*zst.begin());
                zst.erase(zst.begin());
            }
            else
            {
                printf("没有被阻塞的进程,不能唤醒\n");
                continue;
            }
        }
        if(jxt.size())
        {
            pcb tmp=*jxt.begin();
            jxt.erase(jxt.begin());
            tmp.last--;
            jxt.insert(tmp);
            if((*jxt.begin()).last==0)
            {
                printf("id为%d的进程执行完毕,已经被撤销\n",(*jxt.begin()).id);
                jxt.erase(jxt.begin());
            }
        }
        if(jxt.size()) printf("正在执行的进程:\nid:%d\t优先权:%d\t状态:%s\t执行所需时间:%d\n",(*jxt.begin()).id,(*jxt.begin()).priority,refer[(*jxt.begin()).status-1],(*jxt.begin()).last);
        else printf("当前没有进程在执行\n");
        printf("就绪态中的进程:\n");
        for(set<pcb>::iterator it=jxt.begin();it!=jxt.end();it++)
        {
            if(it==jxt.begin()) continue;
            printf("id:%d\t优先权:%d\t状态:%s\t执行所需时间:%d\n",(*it).id,(*it).priority,refer[(*it).status],(*it).last);
        }
        printf("阻塞态中的进程:\n");
        for(set<pcb>::iterator it=zst.begin();it!=zst.end();it++)
            printf("id:%d\t优先权:%d\t状态:%s\t执行所需时间:%d\n",(*it).id,(*it).priority,refer[(*it).status],(*it).last);
    }
    return 0;
}

posted on 2017-04-10 20:47  恶devil魔  阅读(164)  评论(0编辑  收藏  举报

导航