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

📄 123.txt

📁 Progress_Scheduling 操作系统课程设计:进程调度.用优先数算法和轮转
💻 TXT
字号:
#include<stdio.h> 
#include <dos.h> 
#include<stdlib.h> 
#include<conio.h> 
#include<iostream.h> 
int  P_NUM;
#define P_TIME 50 
enum state
{ 
	ready, 
	run, 
	block, 
	finish 
}; 

struct pcb
{ 
	char name[4]; 
	int priority; 
	int cputime; 
	int needtime; 
	int count; 
	int round; 
	state process; 
	pcb * next; 
}; 

pcb * get_process(); 
pcb * get_process()
{ 
	pcb *q; 
	pcb *t; 
	pcb *p; 
	int i=0; 

	while (i<P_NUM)
	{ 
		q=(struct pcb *)malloc(sizeof(pcb)); 
		cout<<"输入进程号和所需时间: ";
		cin>>q->name; 
		cin>>q->needtime; 
		q->cputime=0; 
		q->priority=P_TIME-q->needtime; 
		q->process=ready; 
		q->next=NULL; 
		if (i==0)
		{ 
			p=q; 
			t=q; 
		} 
		else
		{ 
			t->next=q; 
			t=q; 
		} 
		i++;
	} 
	cout<<"*******************************************\n";
	return p; 
} 

void display(pcb *p)
{ 
	cout<<"进程号"<<" "<<"cpu时间"<<" "<<"所需时间"<<" "<<"优先数"<<" "<<"状态"<<endl; 
	while(p)
	{ 
		cout<<p->name; 
		cout<<"      "; 
		cout<<p->cputime; 
		cout<<"      "; 
		cout<<p->needtime; 
		cout<<"      "; 
		cout<<p->priority; 
		cout<<"      "; 
		switch(p->process)
		{ 
			case ready:cout<<"ready"<<endl;break; 
			case run:cout<<"run"<<endl;break; 
			case block:cout<<"block"<<endl;break; 
			case finish:cout<<"finish"<<endl;break; 
		} 
		p=p->next; 
	} 
} 

int process_finish(pcb *q)
{ 
	int bl=1; 
	while(bl&&q)
	{ 
		bl=bl&&q->needtime==0; 
		q=q->next; 
	} 
	return bl; 
} 

void cpuexe(pcb *q)
{ 
	pcb *t=q; 
	int tp=0; 
	while(q)
	{ 
		if (q->process!=finish)
		{ 
			q->process=ready; 
			if(q->needtime==0) 
			  q->process=finish;  
		}  
		if(tp<q->priority&&q->process!=finish)
		{ 
			tp=q->priority; 
			t=q; 
		} 
		q=q->next; 
		} 
		if(t->needtime!=0)
		{ 
			t->priority-=3; 
			t->needtime--; 
			t->process=run; 
			t->cputime++; 
	} 
} 

void priority_cal()
{ 
	pcb * p; 
 
	p=get_process(); 
	int cpu=0; 
 
	while(!process_finish(p))
	{ 
		cpu++; 
		cout<<"cputime:"<<cpu<<endl; 
		cpuexe(p); 
		display(p); 
 
	} 
	printf("*******************************************"); 
	getch(); 
} 

void display_menu()
{ 
	cout<<"*******************************************\n";
	cout<<"选择算法:"<<endl; 
	cout<<"1--优先数算法"<<endl; 
	cout<<"2--时间片轮转算法"<<endl; 
	cout<<"3--退出"<<endl; 
	cout<<"*******************************************\n";
} 

pcb * get_process_round()
{ 
	pcb *q; 
	pcb *t; 
	pcb *p; 
	int i=0; 


	while (i<P_NUM)
	{ 
	q=(struct pcb *)malloc(sizeof(pcb)); 
	cout<<"输入进程号和所需时间:"<<endl; 
	cin>>q->name; 
	cin>>q->needtime; 
	q->cputime=0; 
	q->round=0; 
	q->count=0; 
	q->process=ready; 
	q->next=NULL; 
	if (i==0){ 
	p=q; 
	t=q; 
	} 
	else{ 
	t->next=q; 
	t=q; 
	} 
	i++; 
	} 
   cout<<"*******************************************\n";
	return p; 
} 

void cpu_round(pcb *q)
{ 
	q->cputime+=2; 
	q->needtime-=2; 

	if(q->needtime<0)
	{ 
		q->needtime=0; 
	} 
	q->count++; 
	q->round++; 
	q->process=run; 
} 

pcb * get_next(pcb * k,pcb * head)
{ 
	pcb * t; 
	t=k; 
	do{ 
	     t=t->next; 
	} while (t && t->process==finish); 

	if(t==NULL)
	{ 
		t=head; 
		while (t->next!=k && t->process==finish)
		{ 
			 t=t->next; 
		} 
	} 
	return t; 
} 

void set_state(pcb *p)
{ 
	while(p)
	{
		if (p->needtime==0) 
		  p->process=finish; 
 
		if (p->process==run)		
		  p->process=ready;  
		p=p->next; 
	}
	
} 

void display_round(pcb *p)
{ 
	cout<<"进程号"<<" "<<"CPU时间"<<" "<<"所需时间"<<" "<<"记数"<<" "<<"时间片"<<" "<<"状态"<<endl; 
	while(p)
	{ 
		cout<<p->name; 
		cout<<"      "; 
		cout<<p->cputime; 
		cout<<"      "; 
		cout<<p->needtime; 
		cout<<"      "; 
		cout<<p->count; 
		cout<<"      "; 
		cout<<p->round; 
		cout<<"   "; 
		switch(p->process)
		{ 
			case ready:cout<<"ready"<<endl;break; 
			case run:cout<<"run"<<endl;break; 
			case finish:cout<<"finish"<<endl;break; 
		} 
		p=p->next; 
	}  
} 

void round_cal()
{ 
	pcb * p; 
	pcb * r; 
 
	p=get_process_round(); 
	int cpu=0; 
 
	r=p; 
	while(!process_finish(p))
	{ 
		cpu+=2; 
		cpu_round(r); 
		r=get_next(r,p); 
		cout<<"cpu "<<cpu<<endl; 
		display_round(p); 
		set_state(p);  
	} 
} 

void main()
{ 
	printf("*******************************************\n");
	printf("输入进程数: ");
	scanf("%d",&P_NUM);
	display_menu(); 
	int k; 
	scanf("%d",&k); 
	switch(k)
	{ 
		case 1:priority_cal();break; 
		case 2:round_cal();break; 
		case 3:break; 
		display_menu(); 
		scanf("%d",&k); 
	} 
}

⌨️ 快捷键说明

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