【无标题】

进程调度

#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;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值