进程调度—分级调度

#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 *insert(node *head, node *news)
{
	node *p, *pre;
	p = head;

	while (p && p->priority >= news->priority) {
		pre = p;
		p = p->next;
	}
	if (p == head) {		/* 只有一个节点由head指向 且该节点的优先数小于news的优先数 */
		news->next = head;
		head = news;
	} else if (p == NULL) {	/* 在当前进程链表中没有找到比news的优先数更小的进程 news节点链到链表最后 */
		pre->next = news;
		pre = news;
	} else {				/* 在链表中间插入 */
		news->next = p;
		pre->next = news;
	}

	return head;
}

node *creat()
{
	node *head;
	node *p1;
	int n = 0;

	p1 = (node *)malloc(sizeof(node));
	scanf("%d %d",&p1->key,&p1->priority);
	gets(p1->message);
	p1->next = NULL;
	head = NULL;

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



/**************************************/
/*	模拟按优先数大小进程分级出队的过程*/
/**************************************/
void rank_out(node *head)
{
	int count = 1;
	node *p;
	p = head;

	while (p) {
		printf("第%d个出队进程为:%d\n",count, p->key);
		printf("key=%d,priority=%d,message=%s\n",p->key,p->priority,p->message);
		p=p->next;
		count++;
	}
}

int main(void)
{
	node *p;

	printf("新建的进程控制表为:\nkey priority message\n");
	p=creat(); /*输入进程控制表*/
	rank_out(p); 

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值