#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int n; 
class PCB 

public:  
int pri;//进程优先数 
int runtime;//进程运行CPU时间  
int pieceOftime;//轮转时间片    
string procname;//进程名   
string state;//进程状态 
int needOftime;//还需要时间  
int Counter;    
PCB * next; 
}; 
PCB * run = NULL;
PCB * ready = NULL;
PCB * finish = NULL;
PCB * tial = ready; 
void Dtime(int t)
{   
time_t current_time;  
time_t start_time;   
time(&start_time);  
do  
{      
time(& current_time);  
}
while((current_time-start_time)<t); 

void Prinft(int a)
{   
if(a==1)   
{        
cout<<"进程名称"<<"\t"<<"优先数"<<"\t"<<"还需要时间"<<"\t"<<"已运行时间"<<"\t"<<"状态:"<<endl; 
}   
else     
cout<<"进程名称"<<"\t"<<"已运行时间"<<"\t"<<"还需要时间"<<"\t"<<"计数器"<<"\t"<<"时间片"<<"\t"<<"状态"<<endl; 
}
void Prinft(int b,PCB * p)
{   
if(b==1)   
{      
cout<<p->procname<<"\t\t"<<p->pri<<"\t"<<p->needOftime<<"\t\t"<<p->runtime<<"\t\t"<<p->state<<endl;
}    
else  
cout<<p->procname<<"\t\t"<<p->runtime<<"\t\t"<<p->needOftime<<"\t\t"<<p->Counter<<"\t"<<p->pieceOftime<<"\t"<<p->state<<endl;
}
void display(int c)
{   
PCB *p; 
if(run!=NULL) /*如果运行指针不空*/     
Prinft(c,run); /*输出当前正在运行的PCB*/     //Dtime(2);   
p=ready; /*输出就绪队列PCB*/  
while(p!=NULL)    
{       
Prinft(c,p); 
p=p->next;
}     //Dtime(2);     
p=finish; /*输出完成队列的PCB*/   
while(p!=NULL)  
{       
Prinft(c,p);     
p=p->next; 

}
void insert(PCB *p)//插入就绪队列按Pri大小
{    
PCB *S1,*S2;
if(ready==NULL)   
{     
p->next = NULL; 
ready = p;  
}   
else    
{     
S1 = ready;      
S2 = S1;      
while(S1!=NULL)  
{         
if(S1->pri >= p->pri)    
{          
S2 = S1;        
S1 = S1->next;      
}       
else        
break;    
}       
if(S2->pri >= p->pri)   
{         
S2->next = p;   
p->next = S1;    
}       
else      
{            
p->next = ready; 
ready = p;    
}  
}

bool CTProcessOfPri() 
{   
PCB * Node;   
cout <<"输入创建进程的数目:"<<endl;
cin  >>n;   
for(int j = 0;j < n; j++)  
{     
Node = new PCB;     
if(Node==NULL)   
return false;    
else    
{

cout <<"输入进程的名称,进程需CPU时间:"<<endl;     
cin >>Node->procname>>Node->needOftime;      
Node->runtime = 0;       
Node->state ="就绪";        
Node->pri =Node->needOftime;           
cout <<"进程"<<Node->procname<<"创建完毕!"<<endl; 
}        
insert(Node);   
}    
return true;

void priority(int i)
{  
run = ready;  
ready = ready->next;  
run->state = "运行";  
Prinft(i);     
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/ 
{       
run->runtime=run->runtime+1;      
run->needOftime=run->needOftime-1;  
run->pri=run->pri-1; /*每运行一次优先数降低1个单位*/   
if(run->needOftime==0) /*如所需时间为0将其插入完成队列*/    
{        
run->state = "完成";   
run->next = finish;         
finish = run;    
run=NULL; /*运行队列头指针为空*/         
if(ready!=NULL) /*如就绪队列不空*/          
{            
run = ready;         
run->state = "运行";        
ready = ready->next;         
}      
}    
else if((ready!=NULL)&&(run->pri<ready->pri))    
{             
run->state="就绪";      
insert(run);      
run = ready;        
run->state = "运行";    
ready = ready->next;  
}       
display(i); /*输出进程PCB信息*/

}
}
void queue(PCB *p) 
{    
if(ready==NULL)
{       
p->next = NULL;    
ready = p;   
tial = p;   
}    
else   
{    
tial->next = p;  
tial = p;      
p->next = NULL; 
    } 

bool CTProcessOfRuntime()
{    
PCB * Node;  
int m;   
cout <<"输入创建进程的数目:"<<endl;   
cin  >>n;    
cout <<"输入时间片:"<<endl;  
cin >>m;   
for(int j = 0;j < n; j++)   
{        
Node = new PCB;    
if(Node==NULL)     
return false;   
else  
{      
cout <<"输入进程的名称,进程需CPU时间:"<<endl;    
cin >>Node->procname>>Node->needOftime;    
Node->runtime = 0;        
Node->state ="就绪";        
Node->Counter = 0;        
Node->pieceOftime = m;     
cout <<"进程"<<Node->procname<<"创建完毕!"<<endl;
}      
queue(Node);   
}
return true;
}
void Runtime(int c)
{    
run = ready;   
ready = ready->next;   
run->state = "运行";   
Prinft(c); 
while(run!=NULL)    
{      
run->runtime=run->runtime+1; 
run->needOftime=run->needOftime-1;  
run->Counter = run->Counter + 1;     
if(run->needOftime==0)     
{          
run->state = "完成";  
run->next = finish;       
finish = run;       
run = NULL;       
if(ready!=NULL)   
{           
run = ready;    
ready = ready->next;
}    
}     
else if(run->Counter == run->pieceOftime)  
{          
run->Counter = 0;     
run->state = "就绪";         
queue(run);          
run=NULL;      
if(ready!=NULL)   
{             
run = ready;      
run->state = "运行";   
ready = ready->next;   
}    
}      
display(c); 


int main() 
{    
int i;
cout <<"*******************************************"<<endl;

cout <<"* 1 优先数调度算法    2 循环时间片轮转算法*"<<endl;  
cout <<"*************** 0 退出  *******************"<<endl;  
cin >>i;   
switch(i)    
{    
case 1: 
CTProcessOfPri();    
priority(i);       
break;     
case 2:       
CTProcessOfRuntime();   
Runtime(i);      
break;   
default:      
break;   
}    
return 0;