优先级调度算法

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node {
	char name[20];     /*进程的名字*/
	int prio;          /*进程的优先级*/
	//int cputime;     /*CPU执行时间*/
	int needtime;      /*进程执行所需要的时间*/
	char state;        /*进程的状态,W--就绪态,R--执行态,F--完成态*/
	struct node *next;/*链表指针*/
}PCB;
PCB*ready=NULL,*run=NULL,*finish=NULL;/*定义三个队列,就绪队列,执行队列和完成队列*/
int num;
void GetFirst();       /*从就绪队列取得第一个节点*/
void Output();         /*输出队列信息*/
void InsertPrio(PCB*in);/*创建优先级队列,规定优先数越小,优先级越高*/
void InsertTime(PCB*in);/*时间片队列*/
void InsertFinish(PCB*in);/*时间片队列*/
void PrioCreate();     /*优先级输入函数*/
//void TimeCreate();   /*时间片输入函数*/
void Priority();       /*按照优先级调度*/
//void RoundRun();     /*时间片轮转调度*/
void main() {
	printf("优先级调度算法\n");
	printf("please input the number of processes:");
	scanf("%d",&num);
	PrioCreate();
	Priority();
	Output();
}
/*取得第一个就绪队列节点*/
void GetFirst(){
	run=ready;
	if(ready!=NULL){
		run->state='R';
		ready=ready->next;
		run->next=NULL;
	}
}
void Output()/*输出队列信息*/
{
	PCB*p;
	p=ready;
	printf("pcb_name\tpriority\tneed_time\tpro_state\n");
	while(p!=NULL){
		printf("%s\t%d\t%d\t\t%c\t\n",p->name,p->prio,p->needtime,p->state);
		p=p->next;
	}
	p=finish;
	while(p!=NULL){
		printf("%s\t%d\t%d\t\t%c\t\n",p->name,p->prio,p->needtime,p->state);
		p=p->next;
	}
	p=run;
	while(p!=NULL){
		printf("%s\t%d\t%d\t\t%c\t\n",p->name,p->prio,p->needtime,p->state);
		p=p->next;
	}
}
/*创建优先级队列,规定优先数越小,优先级越低*/
void InsertPrio(PCB*in){
	PCB*fst,*nxt;
	fst=nxt=ready;
	/*如果队列为空,则为第一个元素*/
	if(ready==NULL){
		in->next=ready;
		ready=in;
	}
	/*查到合适的位置进行插入*/
	else 
	{
		if(in->prio>=fst->prio)/*比第一个还要大,则插入到队头*/
		{
			in->next=ready;
			ready=in;
		}else{
			while(fst->next!=NULL)/*移动指针查找第一个别它小的元素的位置进行插入*/
			{
				nxt=fst;
				fst=fst->next;
			}
			if(fst->next==NULL)/*已经搜索到队尾,则其优先级数最小,将其插入到队尾即可*/
			{
				in->next=fst->next;
				fst->next=in;
			}
			else/*插入到队列中*/
			{
				nxt=in;
				in->next=fst;
			}
		}
	}
}
void InsertFinish(PCB*in)/*将进程插入到完成队列尾部*/
{
	PCB*fst;
	fst=finish;
	if(finish==NULL){
		in->next=finish;
		finish=in;
	}
	else{
		while(fst->next!=NULL){
			fst=fst->next;
		}
		in->next=fst->next;
		fst->next=in;
	}
}
void PrioCreate()/*优先级调度输入函数*/
{
	PCB*tmp;
	int i;
	printf("please input process_name,need_time,priority:\n");
	for(i=0;i<num;i++){
		if((tmp=(PCB*)malloc(sizeof(PCB)))==NULL){
			perror("malloc");
			exit(1);
		}
		scanf("%s",tmp->name);
		getchar();/*吸收回车符号*/
		scanf("%d",&(tmp->needtime));
		getchar();
		scanf("%d",&(tmp->prio));
		//tmp->cputime=0;
		tmp->state='W';
		//tmp->prio=20-tmp->needtime;
		/*设置其优先级,需要的时间越多,优先级越低*/
		InsertPrio(tmp);/*按照优先级从高到低,插入到就绪队列*/
	}
}
void Priority()/*按照优先级调度,每次执行一个时间片*/
{
	int flag=1;
	GetFirst();
	while(run!=NULL)/*当就绪队列不为空时,则调度进程如执行队列执行*/
	{
		Output();/*输出每次调度过程中各个节点的状态*/
		while(flag){
			run->prio-=3;/*优先级减去三*/
			run->needtime--;/*进程执行完成的剩余时间减一*/
			if(run->needtime == 0)/*如果进程执行完毕,将进程状态置为F,将其插入到完成队列*/
			{
				run->state='F';
				InsertFinish(run);
				flag=0;
			}
			else/*将进程状态置为W,入就绪队列*/
			{
				run->state='W';
				InsertPrio(run);
				flag=0;
			}
		}
		flag=1;
		GetFirst();/*继续取就绪队列队头进程进入执行队列*/
	}
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优先级调度算法(Priority Scheduling Algorithm)是一种用于任务调度的算法,根据任务的优先级进行排序和执行。该算法根据任务的重要性或优先级来确定任务的执行顺序。在目前提供的引用中,并没有特别提及优先级调度算法的具体实现和代码。引用提到了一个动态优先级编程算法的Matlab代码,但没有明确指出这个算法是优先级调度算法。引用提到了遗传算法(Genetic Algorithms),但并没有提到优先级调度算法。引用提到了一段Matlab代码,但没有提到与优先级调度算法相关的内容。 因此,在这种情况下,我们无法提供关于优先级调度算法在Matlab中的具体实现和代码。如果您需要了解更多关于优先级调度算法的信息,建议您参考相关的计算机科学和调度算法的教材、论文或在线资源。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [动态优先级调度算法代码matlab-scheduler:CPU调度模拟器](https://download.csdn.net/download/weixin_38735541/18905280)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【优化调度】基于matlab遗传算法求解工件的并行调度组合优化问题【含Matlab源码 2234期】](https://blog.csdn.net/TIQCmatlab/article/details/127970490)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值