#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;
}
进程调度—分级调度
最新推荐文章于 2024-08-10 16:46:04 发布