⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shijianpianlunzhuanfa.cpp

📁 里面有5个关于操作系统进程调度的算法源码
💻 CPP
字号:
#include <iostream.h>
#include <iomanip.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class PCB
{
public:
char name[10];     
int pr;          
int round;         
int costtime;       
int needtime;      
int count;         
char state;        
PCB *next; 
};
PCB *finish,*ready,*tail,*run; 
int N;    
void firstin()
{
run=ready;     
run->state='R'; 
ready=ready->next; 
}

void prt1(char a)
{
if(toupper(a)=='P') 
   cout<<" "<<endl;
cout<<"进程名     已用时间      所需时间    轮转数量   状态字"<<endl;
}


void prt2(char a,PCB *q)
{
if(toupper(a)=='P') 
   cout<<setw(6)<<q->name<<"   "<<setw(6)<<q->costtime<<"      "<<setw(6)<<q->needtime<<"        "<<setw(6)<<
   q->round<<"       "<<setw(6)<<q->state<<endl;
}


void prt(char stu)
{
PCB *p;
prt1(stu); 
if(run!=NULL) 
   prt2(stu,run); 
p=ready; 
while(p!=NULL)
{
   prt2(stu,p);
   p=p->next;
}
p=finish;   
while(p!=NULL)
{
   prt2(stu,p);
   p=p->next;
}
getchar(); 
}


void insert(PCB *q)
{
PCB *p1,*s,*r;
s=q; 
p1=ready; 
r=p1; 
while(p1!=NULL)
   if(p1->round<=s->round)
   {
    r=p1;
    p1=p1->next;
   }
   if(r!=p1)
   {
    r->next=s;
    s->next=p1;
   }
   else
   {
    s->next=p1; 
    ready=s;
   }
}


void load(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL; 
finish=NULL; 
run=NULL;    
cout<<"输入进程名及其需要运行的时间:"<<endl; 
for(i=1;i<=N;i++)
{
   p=new PCB;
   cin>>na;
   cin>>time;
   strcpy(p->name,na);
   p->costtime=0;
   p->needtime=time;
   p->state='R';
   p->round=0;
   if(ready!=NULL) 
    insert(p);
   else
   {
    p->next=ready; 
    ready=p;
   }
   cout<<"输入进程名及其需要运行的时间:"<<endl; 
}
prt(alg); 
run=ready; 
ready=ready->next;
run->state='R';
}


void timeslicecycle(char alg)
{
while(run!=NULL)
{
   run->costtime=run->costtime+1;
   run->needtime=run->needtime-1;
   run->round=run->round+1;
   if(run->needtime<=0)
   {
    run->next=finish;
    finish=run;
    run->state='E';
    run=NULL;
    if(ready!=NULL)
     firstin();
   }
   else
   {
    run->state='R';
    insert(run);
    firstin();
   }
   prt(alg);
}
}


void main()
{
char stu='P'; 
cout<<"输入进程的个数:";
cin>>N; 
load(stu); 
timeslicecycle(stu); 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -