Demo entry 6640972

111

   

Submitted by 111 on Sep 14, 2017 at 15:03
Language: C++. Code size: 4.7 kB.

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct PCB{
	int id;//进程号
	int arrive;//到达时间
	int service;//服务时间
	int begin;//开始运行时间
	int end;//完成运行时间
	int cycle;//周转时间 
	double response;//响应比
}pcb[100],queue[100];
int n;
int vis[100];
int flag[100];
bool cmp1(PCB p1,PCB p2){//到达时间排序 
	return p1.arrive < p2.arrive;	
} 
bool cmp2(PCB p1,PCB p2){//服务时间排序 
	return p1.service < p2.service;
}
bool cmp3(PCB p1,PCB p2){//响应比排序 
	return p1.response > p2.response;
}
void Fcfs(){//先来先服务算法 
	printf("-------------------------------\n\n");
	printf("先来先服务算法\n\n");
	printf("进程号 开始时间 完成时间 周转时间\n");
	sort(pcb,pcb+n,cmp1);
	pcb[0].begin = pcb[0].arrive;
	pcb[0].end = pcb[0].begin + pcb[0].service;
	pcb[0].cycle = pcb[0].end - pcb[0].arrive;
	for(int i=1;i<n;i++){
		if(pcb[i].arrive > pcb[i-1].end)
			pcb[i].begin = pcb[i].arrive;
		else
			pcb[i].begin = pcb[i-1].end;
		pcb[i].end = pcb[i].begin + pcb[i].service;
		pcb[i].cycle = pcb[i].end-pcb[i].arrive;
	}
	
	for(int i=0;i<n;i++)
		printf("%-8d%-9d%-9d%-9d\n",pcb[i].id,pcb[i].begin,pcb[i].end,pcb[i].cycle);
	printf("-------------------------------\n");
}
void Sjf(){//短进程优先算法(非抢占) 
	printf("-------------------------------\n\n");
	printf("短进程优先算法\n\n");
	printf("进程号 开始时间 完成时间 周转时间\n"); 
	memset(vis,0,sizeof(vis));
	int count = 0;
	int time = 0;
	int front = 0,tail = 0;
	while(true){
		for(int i=0;i<n;i++){//选出所有在当前时间之前到达的进程 
			if(pcb[i].arrive <= time && vis[pcb[i].id]==0){
				queue[tail++] = pcb[i];
				vis[pcb[i].id] = 1;
			}				
		}
		if(front != tail){
			sort(queue+front,queue+tail,cmp2);
			queue[front].begin = time;
			queue[front].end = time + queue[front].service;
			queue[front].cycle = queue[front].end - queue[front].arrive;			
			printf("%-8d%-9d%-9d%-9d\n",queue[front].id,queue[front].begin,queue[front].end,queue[front].cycle);
			time = queue[front].end;
			front++;
			count++;
		}
		else
			time++;
		if(count == n)	break;
	}
	printf("-------------------------------\n");
}
void Hrrn(){//高响应比优先算法 
	printf("-------------------------------\n\n");
	printf("高响应比优先算法\n\n");
	printf("进程号 开始时间 完成时间 周转时间\n"); 
	memset(vis,0,sizeof(vis));
	int count = 0;
	int time = 0;
	int tail=0,front=0; 
	while(true){
		for(int i=0;i<n;i++){//选出所有在当前时间之前到达的进程 
			if(pcb[i].arrive <= time && vis[pcb[i].id]==0){
				queue[tail++] = pcb[i];
				vis[pcb[i].id] = 1;				
			}				
		}
		if(front != tail){
			for(int j=front;j<tail;j++){
				time = (double) time;
				queue[j].arrive = (double) queue[j].arrive;
				queue[j].service = (double) queue[j].service;				
				queue[j].response = (time-queue[j].arrive+queue[j].service)/queue[j].service;	
			}					 
			sort(queue+front,queue+tail,cmp3);
			queue[front].begin = time;
			queue[front].end = time + queue[front].service;
			queue[front].cycle = queue[front].end - queue[front].arrive;
			printf("%-8d%-9d%-9d%-9d\n",queue[front].id,queue[front].begin,queue[front].end,queue[front].cycle);
			time = queue[front].end;
			front++;
			count++;
		}
		else
			time++;
		if(count == n)	break;
	}
	printf("-------------------------------\n");
}
void Timearound(){//时间片轮转算法 
	printf("-------------------------------\n\n");
	printf("时间片轮转算法\n\n");
	printf("进程号 开始时间 完成时间 周转时间\n"); 
	memset(vis,0,sizeof(vis));
	memset(flag,0,sizeof(flag));
	int time = 0;
	int count = 0;
	int flag1 = 0;
	int front = 0,tail = 0;
	while(true){
		for(int i=0;i<n;i++){
			if(pcb[i].arrive <= time && vis[pcb[i].id] == 0){
				queue[tail++] = pcb[i];
				vis[pcb[i].id] = 1;
			}
		}
		if(flag1){
			queue[tail++] = queue[front];
			front++;
			flag1 = 0;
		}
		if(front != tail){
			if(queue[front].service <= 1){
				if(flag[queue[front].id]==0){
					queue[front].begin = time;
					flag[queue[front].id]=1;
				}                     
				queue[front].end = time + queue[front].service;
				queue[front].cycle = queue[front].end - queue[front].arrive;
				printf("%-8d%-9d%-9d%-9d\n",queue[front].id,queue[front].begin,queue[front].end,queue[front].cycle);
				count++;				
				time = queue[front].end;
				front++;
			}
			else{               
                if(flag[queue[front].id]==0){
					queue[front].begin = time;
					flag[queue[front].id]=1;
				}  
				queue[front].service --;
				time ++;	
				flag1 = 1;			
			}		
		}
		else
			time++;
		if(count == n)	break;
	}
	printf("-------------------------------\n");
}
int main(){
	printf("请输入进程数:\n");	 
	scanf("%d",&n);
	printf("请分别输入进程号、到达时间、运行时间:\n");
	for(int i=0;i<n;i++)
		scanf("%d%d%d",&pcb[i].id,&pcb[i].arrive,&pcb[i].service);
	Fcfs();
	Sjf();
	Hrrn();
	Timearound();
	return 0;
} 

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).