@蕾蕾老师 补交作业,之前忘记提交了
#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; //优先级:p_priority大的优先级高 struct PCB{ int p_priority; //优先数 //int p_atime; //到达时间 int p_rtime; //需要运行时间 int p_cpu; //已占CPU时间 char p_name; //进程名 char p_state; //进程状态:Wait、Run、Finish PCB *next; //指向下一个PCB }; struct Link{ PCB*front; //队头指针 PCB* rear; //队尾指针 }; bool InitQueue(Link * P) //构造一个空就绪队列 { P->front=P->rear =new PCB; P->front->next=NULL; return true; } void EnQueue(Link * P,PCB*e_pcb)//插入元素 { if(P->front->next==NULL) { P->front->next=e_pcb; e_pcb->next=NULL; } else { PCB*p=P->front->next; while(p->next!=NULL){ p=p->next; } p->next=e_pcb; e_pcb->next=NULL; } } void DeQueue(Link * P,PCB*e_pcb) //将进程从就绪队列中删除 { if(e_pcb->next==NULL) { P->front->next=NULL; } else { P->front->next=e_pcb->next; } } char RunState(PCB*e_pcb){//进程状态改变 if(e_pcb->p_cpu==0) e_pcb->p_state='W'; else if(e_pcb->p_cpu<e_pcb->p_rtime) e_pcb->p_state='R'; else e_pcb->p_state='F'; return e_pcb->p_state; } int main(void){ PCB*p1=new PCB; p1->p_priority = 4; p1->p_rtime=3; p1->p_name='f'; p1->p_state='W'; p1->p_cpu=0; PCB*p2=new PCB; p2->p_priority=2; p2->p_rtime=2; p2->p_name='s'; p2->p_state='W'; p2->p_cpu=0; Link R; InitQueue(&R); EnQueue(&R,p1); EnQueue(&R,p2); PCB*tempt=R.front->next; do{ cout<<"进程名称"<<tempt->p_name<<endl; //名称 cout<<"优先数"<<tempt->p_priority<<endl; //优先数 cout<<"运行时间"<<tempt->p_rtime<<endl; //运行时间 cout<<"运行状态"<<tempt->p_state<<endl; //运行状态 cout<<"已运行时间"<<tempt->p_cpu<<endl; //已运行时间 tempt->p_cpu++; tempt->p_priority--; RunState(tempt); cout<<"时间片运行过后: " << "优先数: "<< tempt->p_priority <<"; 已运行时间: "<< tempt->p_cpu <<"; 运行状态: "<<tempt->p_state <<endl; if(tempt->p_cpu < tempt->p_rtime){ DeQueue(&R,tempt); EnQueue(&R,tempt); } else{ DeQueue(&R,tempt); } tempt=R.front->next; }while(1); return 0; }