处理机调度

处理机调度


先来先服务

#include <iostream>

using namespace std;
const int R=1;//就绪
const int E=2;//结束
const int B=3;//阻塞
struct pcb{
   string name;
   int time;
   int status;
   pcb *next;//用于指向同一个状态的pcb队列里的下一个结点
};
pcb *R_head=new pcb,*R_tail=NULL,*B_head=new pcb,*B_tail=NULL;//两个队列的头指针与尾指针
void New(){
    cout<<"新建进程......"<<endl;
    pcb *process=new pcb;
    if(R_head->next==NULL)
    {//判断就绪队列是否为空
        R_head->next=process;
    }
    else
    {
        R_tail->next=process;
    }
    cout<<"输入线程的名称:"<<endl;
    cin>>process->name;
    cout<<"输入线程的运行时间:"<<endl;
    cin>>process->time;
    process->status=R;
    process->next=NULL;
    //将尾指针指向队列的最后结点
    R_tail=process;
    cout<<"进程创建成功!"<<endl;


}
void Run_finish()
{
    if(R_head->next==NULL)
    {
        cout<<"就绪队列为空"<<endl;
        return;
    }
    pcb *del=R_head->next;
    R_head->next=R_head->next->next;
    cout<<"进程"<<del->name<<"运行完毕"<<endl;
    delete del;
}
void Run_wait()
{
    if(R_head->next==NULL)
    {
        cout<<"就绪队列为空"<<endl;
        return;
    }
    pcb *process=R_head->next;
    R_head->next=R_head->next->next;
    cout<<"进程"<<process->name<<"运行中"<<endl;
    process->status=B;
    cout<<"该进程阻塞"<<endl;
    if(B_head->next==NULL)
    {
        B_head->next=process;
    }
    else
    {
        B_tail->next=process;
    }
    B_tail=process;
    B_tail->next=NULL;
    cout<<"进程阻塞成功"<<endl;
}
void Signal()
{
    if(B_head==NULL)
    {
        cout<<"阻塞队列为空!"<<endl;
        return;
    }
    pcb *process=B_head->next;
    B_head->next=B_head->next->next;
    if(R_head->next==NULL)
    {//判断就绪队列是否为空
        R_head->next=process;
    }
    else
    {
        R_tail->next=process;
    }
    process->status=R;
    process->next=NULL;
}
void Show()
{
    cout<<"进程名\t"<<"指针\t"<<"进程状态\t"<<"运行时间\t"<<endl;
    pcb *R_iter=R_head->next;
    pcb *B_iter=B_head->next;
    while(R_iter!=NULL)
    {
        cout<<R_iter->name<<"\t"<<R_iter->next<<"\t"<<R_iter->status<<"\t"<<R_iter->time<<endl;
        R_iter=R_iter->next;
    }
    while(B_iter!=NULL)
    {
        cout<<B_iter->name<<"\t"<<B_iter->next<<"\t"<<B_iter->status<<"\t"<<B_iter->time<<endl;
        B_iter=B_iter->next;
    }
}
int main()
{
    R_head->next=NULL;
    B_head->next=NULL;
    cout<<"==================欢迎使用进程控制系统========================="<<endl;
    while(true)
    {
        cout<<"----------------------------------------------------"<<endl;
        cout<<"New:新建进程\t\t"<<"Run(finish):运行进程并结束\t"<<endl;
        cout<<"Run(wait):运行后阻塞\t"<<"Signal:唤醒阻塞进程\t"<<endl;
        cout<<"Show:显示所有进程PCB信息\t"<<"Exit:结束"<<endl;
        cout<<"----------------------------------------------------"<<endl;
        cout<<"请输入命令>>";
        string conmand;
        cin>>conmand;
        if("New"==conmand)
        {
            New();
        }
        else if("Run(finish)"==conmand)
        {
            Run_finish();
        }
        else if("Run(wait)"==conmand)
        {
            Run_wait();
        }
        else if("Signal"==conmand)
        {
            Signal();
        }
        else if("Show"==conmand)
        {
            Show();
        }
        else if("Exit"==conmand)
        {
            break;
        }
        else
        {
            cout<<"错误的输入"<<endl;
        }
    }
    cout<<"======================欢迎您再次使用====================="<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Silly and happy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值