C语言实现优先级调度算法

一、问题描述

采用C语言实现优先级调度算法。编制进程调度模拟程序,并计算每个进程的周转时间,带权周转时间,以及所有进程的平均周转时间和平均带权周转时间。

二、源代码

#include <iostream> 
#include <iomanip> //格式化输出 
using namespace std;

//最大进程数 
#define MAXSIZE_N 10

//定义数据结构
struct PCB{
		int number;//进程代号 
		int arrive_time;//到达时间 
		int serve_time;//服务时间 
		int priority;//优先级
		int finish_time=0;//记录结束运行时刻 
    };
	
int main(){
	int n;//进程总数
	PCB p_list[MAXSIZE_N];//PCB数组 
	
	cout<<"======非抢占的优先级调度算法======\n";
	
	//读入进程信息 
	cout<<"请输入进程总数:" ;
	cin>>n;
	for(int i=0;i<n;i++){
		cout<<"\n"<<i+1<<":\n请依次输入进程的信息\n请输入进程代号number = ";
		cin>>p_list[i].number;
		cout<<"请输入到达时间arrive_time = " ;
		cin>> p_list[i].arrive_time;
		cout<<"请输入服务时间serve_time = " ;
		cin>> p_list[i].serve_time;
		cout<<"请输入优先级priority = " ;
		cin>> p_list[i].priority;
	}
	
	//找到进程执行的序列 work_list
	int work_list[n];
	for(int i=0;i<n;i++){//元素代表执行的进程在p_list中的下标,下标代表执行顺序 
		work_list[i]=i;
	}
	int temp=0; 
	for(int i=0;i<n;i++){//先按到达时间排序 
		for(int j=0;j<n-i-1;j++){
			if(p_list[j].arrive_time>p_list[j+1].arrive_time){
				temp=work_list[j];
				work_list[j]=work_list[j+1];
				work_list[j+1]=temp;
			}
		}	
	}
	
	for(int i=0;i<n;i++){//再按优先级排序 
		for(int j=0;j<n-i-1;j++){
			if(p_list[j].arrive_time==p_list[j+1].arrive_time&&p_list[j].priority<p_list[j+1].priority){
				temp=work_list[j];
				work_list[j]=work_list[j+1];
				work_list[j+1]=temp;
			}
		}	
	}

	//执行进程
	int time=p_list[work_list[0]].arrive_time;//记录运行的时刻 
	cout<<"\n调度序号"<<setw(20)<<"运行的进程代号"<<setw(15)<<"优先级"<<setw(20)<<"开始运行的时刻"<<setw(15)<<"运行时间"<<setw(20)<<"结束运行时间"; 
	for(int i=0;i<n;i++){//执行进程并输出 
		cout<<"\n   "<<i+1<<setw(17)<<p_list[work_list[i]].number<<setw(19)<<p_list[work_list[i]].priority<<setw(16)<<time<<setw(18)<<p_list[work_list[i]].serve_time<<setw(19);
		time=time+p_list[work_list[i]].serve_time;
		p_list[work_list[i]].finish_time=time; 
		cout<<time;
	}
	
	//每个进程的周转时间 
	cout<<"\n\n进程代号"<<setw(17)<<"完成时刻"<<setw(18)<<"周转时间"<<setw(20)<<"带权周转时间"; 
	float work_time;//记录周转时间
	float time_sum=0;//记录总周转时间
	float weight_time; //记录带权周转时间
	float weight_sum=0;// 记录总带权周转时间 
	
	for(int i=0;i<n;i++){
		
		work_time=p_list[i].finish_time-p_list[i].arrive_time;
		time_sum+=work_time;
		weight_time=work_time/(float)p_list[i].serve_time;
		weight_sum+=weight_time;
		
		cout<<"\n   "<<p_list[i].number<<setw(17)<<p_list[i].finish_time<<setw(19)<<work_time<<setw(19)<<weight_time;
	}
	
	//平均(带权)周转时间
	cout<<"\n\n平均周转时间:"<<time_sum/n;
	cout<<"\n平均带权周转时间"<<weight_sum/n;	 
}    

三、运行结果截图

第一屏
在这里插入图片描述

第二屏
在这里插入图片描述

  • 14
    点赞
  • 171
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,关于优先级调度算法的模拟,我们可以考虑以下步骤: 1. 定义进程结构体 为了方便模拟,我们可以定义一个进程结构体,包含进程的ID、优先级、状态、运行时间、剩余时间等属性。 ``` struct process { int id; // 进程ID int priority; // 进程优先级 int state; // 进程状态,0表示就绪,1表示运行,2表示完成 int run_time; // 进程已运行时间 int remain_time; // 进程剩余运行时间 }; ``` 2. 定义进程队列 为了实现优先级调度算法,我们需要定义一个进程队列,用于存储所有就绪状态的进程。可以使用链表或数组来实现。 ``` struct node { struct process p; struct node *next; }; struct node *head = NULL; // 队列头指针 ``` 3. 添加进程到队列中 当一个进程创建时,它会被加入到就绪队列中。我们可以定义一个函数来实现这个功能。 ``` void addProcess(int id, int priority, int run_time) { struct process p; p.id = id; p.priority = priority; p.state = 0; p.run_time = 0; p.remain_time = run_time; struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->p = p; newNode->next = NULL; if (head == NULL) { head = newNode; } else { struct node *p = head; while (p->next != NULL) { p = p->next; } p->next = newNode; } } ``` 4. 模拟优先级调度算法优先级调度算法中,进程会按照优先级高低依次运行。我们可以通过循环来模拟这个过程。 ``` void run() { struct node *current = head; // 当前运行的进程 while (head != NULL) { // 找到优先级最高的进程 struct node *p = head; struct node *max = head; while (p != NULL) { if (p->p.priority > max->p.priority) { max = p; } p = p->next; } // 运行当前进程 current = max; printf("Running process %d\n", current->p.id); current->p.state = 1; // 进程状态改为运行 current->p.run_time += 1; current->p.remain_time -= 1; if (current->p.remain_time <= 0) { // 进程已完成 printf("Process %d has finished.\n", current->p.id); current->p.state = 2; // 进程状态改为完成 } // 从队列中删除当前进程 if (current == head) { head = current->next; } else { struct node *p = head; while (p->next != current) { p = p->next; } p->next = current->next; } // 将其他就绪进程的优先级降低 p = head; while (p != NULL) { if (p->p.state == 0) { p->p.priority -= 1; } p = p->next; } } } ``` 以上就是基于C语言优先级调度算法的模拟的主要步骤,需要注意的是,本算法的进程优先级可以根据实际需要进行调整。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值