进程调度
#include<stdio.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#define furthest 5 //process number
#define Finished -999
using namespace std;
void init();
void timesch();
void prisch();
void print();
void insert(int q);
int finished[furthest+1];
int j = 0;
//PCB
struct process{
int id;
int priority;
int cputime;
int alltime;
char state;
}prochain[furthest];
bool cmp(process a,process b){
return a.priority > b.priority;
}
int rand();
int algo;
int run;
int start;
//waiting process number ,running finished number
int main(){
L:printf("type the algorithm is (1:RR,2:PRIO)\n");
scanf("%d",&algo);
if(algo == 1){
init();
timesch();
}else if(algo == 2){
init();
prisch();
}else{
printf("the algo you input is useless,please input again\n");
goto L;
}
cout << "the plain of the finished queue:";
for(int i = 0;i < furthest;i++){
cout << " "<<finished[i] ;
}cout << endl;
}
// init a waiting quene
void init(){
srand((int)time(0));
if(algo == 1){
for (int i = 1;i <= furthest;i++){
prochain[i].id = i;
prochain[i].priority = (rand() + 1) % 3 + 1;
prochain[i].cputime = 0;
prochain[i].alltime = (rand() + 1)% 7 + 1;
prochain[i].state = 'W';
}
}
else if(algo == 2){
for(int i = 1;i <= furthest;i++){
prochain[i].id = i;
prochain[i].state = 'W';
prochain[i].cputime = 0;
prochain[i].alltime = (rand() + 1) % 7 + 1;
prochain[i].priority = (rand() + 11) % 41 + 1;
}
sort(prochain+1,prochain+1+furthest,cmp);
}
print();
}
void print() /*PRINT THE RUNNING PROCESS,WAITING QUEUE AND PCB SEQUENCE LIST*/
{
for (int i = 1;i <= 40;i++)
printf("=");
printf("");
printf("\n queue. ");
printf("\n");
for (int i = 1;i <= 40;i++)
printf("=");
printf("\n");
printf(" id ");
for (int i = 1;i <= furthest;i++)
printf("%5d",prochain[i].id);
printf("\n");
printf("priority ");
for (int i = 1;i <= furthest;i++)
printf("%5d",prochain[i].priority);
printf("\n");
printf("cputime ");
for (int i = 1;i <= furthest;i++)
printf("%5d",prochain[i].cputime);
printf("\n");
printf("alltime ");
for (int i = 1;i <= furthest;i++)
printf("%5d",prochain[i].alltime);
printf("\n");
printf("state ");
for (int i = 1;i <= furthest;i++)
printf("%5c",prochain[i].state);
printf("\n");
printf("\n");
printf("\n");
}
void prisch(){
cout << "the alog you used is prisch" << endl;
while(1){
sort(prochain+1,prochain+furthest+1,cmp);
process *run = prochain;
for(int i = 1;i <= furthest;i++){
if(prochain[i].alltime != 0){
run = &prochain[i];
break;
}
}
cout<<"####################################################"<<endl;
cout<<endl;
cout <<"process "<< run->id << " is running" << endl;
cout<<endl;
cout<<"####################################################"<<endl;
cout<<endl;
run->state = 'R';
run->alltime--;
run->priority -= 3;
print();
run->state = 'W';
if(run->alltime == 0){
cout<<"****************************"<<endl;
cout<<endl;
cout <<"process "<< run->id << " is Finished"<<endl;
cout<<endl;
cout<<"****************************"<<endl;
cout<<endl;
run->state = 'F';
run->priority = Finished;
finished[j++] = run->id;
print();
}
//stop
int count = 0;
for(int i = 1;i <= furthest;i++){
if(prochain[i].state == 'F')
count++;
}
if(count == 5){
print();
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
cout<<endl;
cout << "all process ok" << endl;
break;
}
}
}
void timesch(){
cout << "the alog you used is timesch"<<endl;
int time = 1 ;
while(1){
process *run = prochain + time % 6 ;
time++;
if(run->alltime == 0){
continue;
}
cout<<"####################################################"<<endl;
cout<<endl;
cout <<"process "<< run->id << " is running" << endl;
cout<<endl;
cout<<"####################################################"<<endl;
cout<<endl;
while(run->cputime != run->priority && run->alltime != 0){
run->state = 'R';
run->cputime++;
run->alltime--;
if(run->alltime == 0){
cout<<endl;
cout<<"****************************"<<endl;
cout<<endl;
cout <<"process "<< run->id << " is Finished"<<endl;
cout<<endl;
cout<<"****************************"<<endl;
cout<<endl;
run->state = 'F';
run->priority = 0;
finished[j++] = run->id;
print();
break;
}
print();
}
if(run->state != 'F')
run->state = 'W';
run->cputime = 0;
int count = 0;
for(int i = 1;i <= furthest;i++){
if(prochain[i].state == 'F')
count++;
}
if(count == 5){
print();
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
cout<<endl;
cout << "all process is ok" << endl;
break;
}
}
}