进程调度优先级算法基于时间片算法

实验代码:
#include<stdio.h>
#include<stdlib.h>
#include"procelink.h"
void main()
{
	process *PQ;
	int n;
	pcb pro1;
	Initlink(&PQ);
	printf("请输入进程个数:");
	scanf("%d",&n);
		printf("请输入各个进程的进程标识数ID,进程优先数,进程一占用时间片,进程还需占用时间片\n");
	for(int i=1;i<=n;i++)
	{
		printf("第%d进程:",i);
		scanf("%d%d%d%d",&pro1.ID,&pro1.PRIORITY,&pro1.CPUTIME,&pro1.ALLTIME);
		EnInitlink(PQ,pro1);
	}
	while(!IsEmpty(PQ))
	{
		dispatch(DeInitlink(PQ),PQ);
	}
	if(IsEmpty(PQ))
		printf("所有进程一运行完!\n");
	free(PQ);
}
Procelink头文件
typedef struct node1
{
	int ID;
	int PRIORITY;
	int CPUTIME;
	int ALLTIME;
	char STATE;
	struct node *next;
}Block,pcb;
typedef struct node2
{
	pcb data;
	struct node2 *next;
}process;
void Initlink(process **PQ)
{
	if((*PQ=(process *)malloc(sizeof(process)))==NULL)exit(1);
	(*PQ)->next=NULL;
}
int IsEmpty(process *PQ)
{
	if(PQ->next==NULL)
		return 1;
	else return 0;
}
void EnInitlink(process *PQ,pcb p)
{
	while(PQ->next!=NULL)PQ=PQ->next;
	process *temp=(process *)malloc(sizeof(Block));
	temp->data.PRIORITY=p.PRIORITY;
	temp->data.ID=p.ID;
	temp->data.CPUTIME=p.CPUTIME;
	temp->data.ALLTIME=p.ALLTIME;
	temp->next=PQ->next;
	PQ->next=temp;
}
pcb DeInitlink(process *PQ)
{
	if(IsEmpty(PQ))
	{
		printf("所有程序已运行完!\n");
		exit(0);
	}
	process *temp=(process *)malloc(sizeof(Block));
	temp=PQ->next;
	process *te=(process *)malloc(sizeof(Block));
	process *t=(process *)malloc(sizeof(Block));
	te=PQ->next;
	while(temp->next!=NULL)
	{
		if(te->data.PRIORITY<temp->next->data.PRIORITY)
		{
			te=temp->next;
			t=temp->next->next;
			PQ=temp;
		}
		temp=temp->next;
	}
	PQ->next=PQ->next->next;
	pcb pp=te->data;
	//free(temp);
	//free(t);
	//free(te);
	return pp;
}//出队列
void outid(process *PQ)
{
	printf("当前就绪队列为:");
	while(!IsEmpty(PQ))
	{
		printf("%d ",PQ->next->data.ID);
		PQ=PQ->next;
	}
	printf("\n");
}
void dispatch(pcb p,process *PQ)
{
	if((p.ALLTIME)!=0)
	{
		p.PRIORITY-=3;
		p.CPUTIME+=1;
		p.ALLTIME-=1;
		printf("进程%d运行\n",p.ID);
		outid(PQ);
	}
	if((p.ALLTIME)==0)
	{
		printf("进程%d运行完成!\n",p.ID);
		return;
	}
	EnInitlink(PQ,p);
	return;
}

发布了4 篇原创文章 · 获赞 1 · 访问量 204

猜你喜欢

转载自blog.csdn.net/weixin_43225235/article/details/102907214
今日推荐