进程调度—最大优先数

#include <malloc.h>
#include <stdio.h>
#include <string.h>

typedef struct table
{ 
	int key;				/*进程ID号*/
	int priority;			/*优先数值*/
	char message[10];		/*进程说明信息*/
	struct table *next;
}node;

/**************************************/
/* 定义函数  建立进程链表			 */
/**************************************/
node *creat(void)
{
	node *head;
	node *p1, *p2;
	int n = 0;

	p1 = p2 = (node *)malloc(sizeof (node));
	scanf("%d %d",&p1->key,&p1->priority);
	gets(p1->message);
	head = NULL;
	
	while (p1->key != 0) {  /* 输入0表示结束 */
		n += 1;
		if (n == 1)
			head = p1;
		else 
			p2->next = p1;
		p2 = p1;
		p1 = (node *)malloc(sizeof (node));
		scanf("%d %d",&p1->key,&p1->priority);
		gets(p1->message);
	}
	p2->next = NULL;

	return head;
}

/****************************/
/*	输出链表			   */
/***************************/
void print (node *head)  
{ 
	node *p;
	
	printf("\n The table is:\n");
	p=head;
	
	while(p) { 
		printf("%d,%d,%s\n",p->key,p->priority,p->message);
		p=p->next;
    }
 }

/************************************/
/* 模拟当前最大优先数进程出队的过程 */
/************************************/
node *processdo(node *head)
{
	int prior;
	static int count = 0;
	node *p, *pre;
	p = pre = head;
	prior = p->priority;
	count++;
	
	while (p) {		/* 找出优先数最大进程 */
		if (p->priority > prior) 
			prior = p->priority;
		p = p->next;
	}
	
	p = head;
	while (p->priority != prior) {
		pre = p;
		p = p->next;
	}

	printf("\n第%d次出队的进程为:\n",count);
	printf("key=%d,priority=%d,message=%s\n",p->key,p->priority,p->message);

	if (p == head)
		head = head->next;
	else
		pre->next = p->next;
	free(p);

	return head;
}

int main(void)
{
	node *p, *q;
	printf("新建的进程控制表为:\nkey priority message\n");
	p = q = creat();		
	print(p);

	while (p) {		/*模拟逐个出队并进入CPU运行的过程*/
		q = processdo(p);
		p = q;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值