#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;
}
进程调度—最大优先数
最新推荐文章于 2024-11-03 11:36:12 发布