进程调度—先来先服务

来源:互联网 发布:ah ips 知乎 编辑:程序博客网 时间:2024/06/06 15:36
#include <malloc.h>#include <stdio.h>#include <string.h>typedef struct table{ int key;/*进程ID号*/int sequence;/*进程进入队列顺序号*/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->sequence);gets(p1->message);head = NULL;while (p1->key != 0) {//输入0表示结束++n;if (n == 1)head = p1;else p2->next = p1;p2 = p1;p1 = (node *)malloc(sizeof(node));scanf("%d%d", &p1->key, &p1->sequence);gets(p1->message);}p2->next = NULL;return head;}/*模拟当前就绪进程队列中最先进入进程出队并输出的调用过程*/node *fcfs(node *head){node *p, *q;p = head;printf("key=%d,sequence=%d,message=%s\n",p->key,p->sequence,p->message);q = p;p = p->next;free(q);return p;}void print(node *head)//输出链表{node *p;printf("\n The table is : \n");p = head;while (p) {printf("%d, %d, %s\n", p->key, p->sequence, p->message);p = p->next;}}int main(void){int count = 0;node *p, *q;printf("新建的进程控制表为:\nkey sequence message\n");p = creat();//输入进程控制表print(p);while (p) {++count;printf("\n第%d次被调度的就绪进程为:\n",count);q = fcfs(p);p = q;}return 0;}