操作系统//进程状态转换模拟(STLqueue、时间片轮转)

这是一个C++实现的进程状态转换模拟程序,包括就绪、运行、阻塞三种状态,并实现了时间片轮转调度算法。用户可以输入进程信息,程序会根据选择进行状态转换操作,并展示转换后的进程队列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
using namespace std;
//设置时间片
int timeSlice = 3;    //自定义时间片的长度
//CPU最大数量
int cpu_size = 2;//CPU容量
//设置进程数目
int number = 0;        //进程的数目

//节点的作业控制块PCB信息
typedef struct PCB {
    int Name;            //进程的名称
    int Time;            //进程需要运行的时间
}PCB;
//创建队列
queue<PCB>readyqueue;
queue<PCB>runqueue;
queue<PCB>blockqueue;


//就绪态队列(往就绪态里放进程)
void SetReadyQueue()
{
    number++;//进程数量上限为100
    if (number > 100)
    {
        return;
    }
    PCB readyQ;//创建进程
    cout << "请输入进程名称:" << endl;
    cin >> readyQ.Name;
    cout << "请输入进程所需运行时间:" << endl;
    cin >> readyQ.Time;
    readyqueue.push(readyQ);
}

//就绪态->运行态
void ReadyToRun()
{
    while (cpu_size-- && !readyqueue.empty())//当满足CPU大小且就绪态不为空时
    {
        PCB runQ = readyqueue.front();//获得就绪态中最在前的进程
        readyqueue.pop();//就绪态弹出进程
        runqueue.push(runQ);
        cout << "运行态进程信息:" << "  " << "进程名称:" << runQ.Name << " " << "进程所需运行时间:" << runQ.Time << endl;
    }
    cpu_size = 2;//每次都要刷新定义cpu容量
}

//运行态到阻塞态
void RunToBlock()
{
    while (!runqueue.empty())//当运行态不为空时
    {
        PCB blockQ = runqueue.front();
        runqueue.pop();//弹出运行态进程
        blockqueue.push(blockQ);
        cout << "阻塞态进程信息:" << "  " << "进程名称:" << blockQ.Name << " " << "进程所需运行时间:" << blockQ.Time << endl;
    }
}

//阻塞态到就绪态
void BlockToReady()
{
    while (!blockqueue.empty())
    {
        PCB readyQ_1 = blockqueue.front();
        blockqueue.pop();//弹出阻塞态进程
        readyqueue.push(readyQ_1);
    }
}
//输出队列进程信息
void PrintReadyQueue(queue<PCB> q)
{
    while (!q.empty())
    {
        cout << "就绪态进程信息:" << "  " << "进程名称:" << q.front().Name << " " << "进程所需运行时间:" << q.front().Time << endl;
        q.pop();
    }
}

//时间片轮转(就绪到运行->判断时间片->回到就绪or运行结束)
void RunToReady()
{
    while (!runqueue.empty())//当运行态不为空时
    {
        if (runqueue.front().Time > timeSlice)//若时间片不够
        {
            runqueue.front().Time = runqueue.front().Time - timeSlice;
            PCB readyQ_1 = runqueue.front();//获得更改后的进程
            readyqueue.push(readyQ_1);
            runqueue.pop();//弹出转移回就绪态的进程
            cout << "时间片用完,回到就绪态" << endl;
        }
        else
        {
            cout << "运行态中的进程运行完毕!" << endl;
            runqueue.pop();//弹出小于时间片的进程
        }
    }
}


int main()
{

    int t = 0;//用于选择执行那哪一条过程
    while (t != 7)
    {
        cout << "============================================" << endl;
        cout << "进程状态转换模拟" << endl;
        cout << "============================================" << endl;
        cout << "1:创建就绪态" << endl;
        cout << "2:就绪态->运行态" << endl;
        cout << "3:运行态->阻塞态" << endl;
        cout << "4:阻塞态->就绪态  " << endl;
        cout << "5:运行态->就绪态或运行完毕 " << endl;
        cout << "6:时间片轮转  " << endl;
        cout << "7:退出程序 " << endl;
        cout << "------------------------------------------------------------------" << endl;
        cout << "请选择(1~7):" << endl;
        cin >> t;
        switch (t) {
        case 1:
            SetReadyQueue();
            break;
        case 2:
            ReadyToRun();
            break;
        case 3:
            RunToBlock();
            break;
        case 4:
            BlockToReady();
            PrintReadyQueue(readyqueue);
            break;
        case 5:
            RunToReady();
            PrintReadyQueue(readyqueue);
            break;
        case 6:
            while (!readyqueue.empty())
            {
                ReadyToRun();
                cout << endl;
                RunToReady();
                PrintReadyQueue(readyqueue);
                cout << endl;
            }
            break;
        case 7:
            cout << "模拟过程结束!感谢使用 scuec" << endl;
            exit(0);
            break;
        }
    }
    return 0;

}


 

流程图,代码,截图三、程序源代码: #include"stdlib.h" #include"stdio.h" #include"string.h" /********** globle structure and viable ******/ struct PCB { int P_Id; //PCB的ID号 char P_Name[10]; //PCB的名称 char P_State[10]; //PCB状态 int P_Runtime; //PCB的所需要的运行时间 int P_Requiry; //PCB所需要的资源要求 struct PCB * next ; //PCB块的下一个指针 } ; struct PCB * Create_state; //创建状态 struct PCB * Run_state; //运行状态 struct PCB * Ready_state; //就绪状态 struct PCB * Block_state; //阻塞状态 struct PCB * Exit_state; //退出状态 int signal4=0; //标示进程4的完成状态 int signal5=0; //标示进程5的完成状态 void InsertQueue(struct PCB **head,struct PCB *node) /* insert node function */ { struct PCB * p,*q; node->next=NULL; if(*head==NULL) //如果队列为空 { *head=node; } Else //队列不空 { p=*head; q=p->next; while(q!=NULL) //找到最后的元素位置 { p=q; q=q->next; } p->next=node; //将节点插入队列 } } void DeleteQueue(struct PCB **head,struct PCB *node) //撤销进程,从队列中删除元素 { struct PCB *p,*q; q=*head; if(*head==NULL||node==NULL) //如果队列为空,返回 return ; if(*head==node) //如果要删除的元素是队首元素 { *head=(*head)->next; return; } Else //如果不是队列的首元素 { while(q->next!=p&&q->next!=NULL) q=q->next; q=p->next; p->next=NULL; } } void Display_Process(struct PCB * node) //打印进程状态的元素函数 { printf("\n\nthis process Id is : %d \n",node->P_Id); printf("this process name is : %s \n",node->P_Name); printf("this process state is : on %s \n ",node->P_State); printf("this process Runtime is : %d \n",node->P_Runtime); if(node->P_Requiry) printf("this process resource is ready \n"); else printf("this process resource is not ready ! \n"); } void DispatchToBlock(struct PCB *node) // /* dispatch to block function*/ { //调度到阻塞状态的函数 //struct PCB *p=(struct PCB *)malloc(sizeof(struct PCB)); if(!node->P_Requiry) //如果所需要的资源没有满足则,调度到阻塞状态 { strcpy(node->P_State,"block"); InsertQueue(&Block_state,node); //插入到阻塞队列 Display_Process(node); } } void DispatchToReady(struct PCB *node) // dispatch to ready state { //调度到就绪状态的函数 if(node->P_Requiry) //如果所需的资源满足,则调度 { strcpy(node->P_State,"Ready"); InsertQueue(&Ready_state,node); Display_Process(node); } } void DispatchBlockToReady() //dispatch the process to readyqueue { //从阻塞状态调度到就绪状态函数 struct PCB*p,*q; q=Block_state; while(q!=NULL) //如果阻塞状态队列不空 { p=q; q=q->next; if(signal4&&p->P_Id==4) //如果所需要的资源满足 { DeleteQueue(&Block_state,p); strcpy(p->P_State,"ready"); InsertQueue(&Ready_state,p); printf("process4 will be in the state of ready!\n"); Display_Process(p); } if(signal5&&p->P_Id==5) { DeleteQueue(&Block_state,p); strcpy(p->P_State,"ready"); InsertQueue(&Ready_state,p); printf("process5 will be in the state of ready!\n"); Display_Process(p); } } } void Create_Process() //创建进程函数 { int i; struct PCB *p; char name[10]; strcpy(name,"process"); for(i=1;iP_Id=i; name[7]=i+'0'; name[8]='\0'; strcpy(p->P_Name,name); strcpy(p->P_State,"create"); p->P_Runtime=1; //所需要的时间片为1 p->P_Requiry=0; Display_Process(p); sleep(4); printf(" \n process%d will be in the state of Block, waiting the resource ready \n\n",i); DispatchToBlock(p); //同时调度到阻塞队列 } for(i=3;iP_Id=i; name[7]=i+'0'; name[8]='\0'; strcpy(p->P_Name,name); strcpy(p->P_State,"create"); p->P_Requiry=1; if(i==6) //在这里个进程6所需要的时间片为2 p->P_Runtime=2; else p->P_Runtime=1; Display_Process(p); sleep(4); printf(" \n process%d will be in the state of Ready, waiting to run \n\n",i); DispatchToReady(p); } } void display(struct PCB **head) //打印各个状态队列里进程数目 { struct PCB *p,*q; p=*head; while(p!=NULL) { sleep(2); //printf("\n\n///////////////////////////////////\n"); printf("\n\nthis process Id is : %d \n",p->P_Id); printf("this process name is : %s \n",p->P_Name); printf("this process state is : on %s \n ",p->P_State); printf("this process Runtime is : %d \n",p->P_Runtime); if(p->P_Requiry) printf("this process resource is ready \n"); else printf("this process resource is not ready ! \n"); p=p->next; } } void Process_Run() //进程运行函数 { struct PCB *p,*q; p=Ready_state; q=p; while(p!=NULL) //就绪队列不空则继续执行 { if(p->P_RuntimeP_State,"running"); Display_Process(p); p->P_Runtime=p->P_Runtime-1; sleep(4); if(p->P_Runtime>0) //没有完成,则进入就绪队列 { printf("this process is not finished,will be dispatch to the ready queue!!\n"); DeleteQueue(&Ready_state,p); strcpy(p->P_State,"ready"); InsertQueue(&Ready_state,p); Display_Process(p); } Else //执行完成,则跳出,并发送相应的信息 { printf("\n\nProcess%d is finished and will be in the state of exit!\n\n",p->P_Id); if(p->P_Id==4) signal4=1; if(p->P_Id==5) signal5=1; } if(signal4||signal5) DispatchBlockToReady(); //如果资源满足,则将进程调度到就绪队列 q=q->next; p=q; } if(q==NULL) printf("\nthere is no process ready!\n STOP Machine!!!\n"); } int main(int argc,char * argv[]) //主函数 { int i; char c='c'; //界面 printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n"); printf("...................................Ding Hai bo\n"); printf("......Press s to start the process.......\n"); scanf("%c",&c); while(1) { if(c=='s')break; scanf("%c",&c); } Create_Process(); //调用创建进程函数 printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); printf("\n>>>>>>> Display the Ready queue >>>>>>>>>>>>>>>\n"); sleep(5); display(&Ready_state); ////////////////显示就绪队列里的进程 printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); printf("\n>>>>>>>> Display the Block queue >>>>>>>>>>>>\n"); sleep(5); //显示阻塞队列函数 display(&Block_state); ///////////////////// printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n"); printf("\n>>>>>>>> Now the process start to run >>>>>>>>>>>\n"); sleep(5); Process_Run(); //调用进程运行函数 } 都有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值