赞
踩
用C语言编写实现对n个进程采用动态优先权算法的进程调度模拟程序。
1.首先设计定义变量
进程状态STATE。假设实验中的进程只有三种状态:就绪(Wait)、运行(Running)和完成(Finished)。
2.设计所需结构体和函数
定义进程控制块PCB
- struct pcb /*定义进程控制块PCB*/
- { char name[10]; /*进程名*/
- char state; /*状态:R-运行,W-就绪,F-完成*/
- int super; /*优先数*/
- int ntime; /*所需运行时间 need time*/
- int rtime; /*已经运行时间 run time*/
- struct pcb * next;
- }*ready=NULL,*p; /* ready指向就绪队列,P指向当前节点 */
- typedef struct pcb PCB;
对进程进行优先级排列函数
- void sort(){
- PCB *first, *second;
- int insert = 0;
- if ((ready == NULL) || ((p->super) > (ready->super))) //优先级最大者,插入就绪队列队首
- {
- p->next =ready;//插入的数据作为链表头结点
- ready = p;//头结点指针指向插入数
- }
- else //进程比较优先级,插入适当的位置中
- {
- first = ready;
- second = first->next;
- while (second != NULL)
- {
- if ((p->super) > (second->super)) //若插入进程比当前进程优先数大
- { //插入到当前进程前面
- p->next = second;
- first->next = p;
- second = NULL;
- insert = 1;
- }
- else//插入进程优先数最低,则插入到队尾
- {
- first = first->next;
- second = second->next;
- }
- }
- if (insert == 0)
- first->next = p;
- }
- }
输入进程控制块函数
- void input()
- {
- int n;
- printf("Please Input the Number of Process:");
- scanf("%d",&n);
- for(int i=0;i<n;i++){
- printf("process id No.%d\n",i+1);
- p=getpch(PCB);
- printf("process name:");
- scanf("%s", p->name);
- printf("process priority:");
- scanf("%d", &p->super);
- printf("process need running time:");
- scanf("%d", &p->ntime);
- p->rtime=0;
- p->state='W';
- p->next=NULL;
- sort();
- }
- }
获取就绪状态的进程数
- int space()
- {
- int len=0;
- PCB* pr=ready;
- while (pr!=NULL)
- {
- len++;
- pr = pr->next;
- }
- return(len);
- }
展示进程各个变量的具体内容
- void disp(PCB * pr)
- {
- printf("\n name state super ntime rtime\n");
- printf(" %s\t", pr->name);
- printf(" %c\t", pr->state);
- printf(" %d\t", pr->super);
- printf(" %d\t", pr->ntime);
- printf(" %d\t", pr->rtime);
- printf(" \n");
- }
查看进程信息,显示当前处于运行态的进程和处于就绪队列的进程
- void look()
- {
- PCB* pr;
- printf("\n**** the running process is:%s", p->name);
- disp(p);
- pr = ready;
- printf("\n**** the ready process is:\n");
- while (pr != NULL)
- {
- disp(pr);
- pr = pr->next;
- }
- }
建立进程就绪函数(进程运行时间到,置就绪状态)
- void running()
- {
- PCB *pr;
- (p->rtime)++;
- pr=ready;
- while(ready!=NULL){
- (ready->super)++;
- ready=ready->next;
- }ready=pr;
- if (p->rtime == p->ntime)
- {
- printf("\n process[%s] is finished.\n", p->name);
- free(p);
- }
- else
- {
- (p->super)=(p->super)-2;
- p->state = 'W';
- sort();
- }
- }
下面为程序源代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #define getpch(type) (type *)malloc(sizeof(type))
-
- struct pcb /*定义进程控制块PCB*/
- { char name[10]; /*进程名*/
- char state; /*状态:R-运行,W-就绪,F-完成*/
- int super; /*优先数*/
- int ntime; /*所需运行时间 need time*/
- int rtime; /*已经运行时间 run time*/
- struct pcb * next;
- }*ready=NULL,*p; /* ready指向就绪队列,P指向当前节点 */
- typedef struct pcb PCB;
-
- //对进程进行优先级排列函数
- void sort(){
- PCB *first, *second;
- int insert = 0;
- if ((ready == NULL) || ((p->super) > (ready->super))) //优先级最大者,插入就绪队列队首
- {
- p->next =ready;//插入的数据作为链表头结点
- ready = p;//头结点指针指向插入数
- }
- else //进程比较优先级,插入适当的位置中
- {
- first = ready;
- second = first->next;
- while (second != NULL)
- {
- if ((p->super) > (second->super)) //若插入进程比当前进程优先数大
- { //插入到当前进程前面
- p->next = second;
- first->next = p;
- second = NULL;
- insert = 1;
- }
- else//插入进程优先数最低,则插入到队尾
- {
- first = first->next;
- second = second->next;
- }
- }
- if (insert == 0)
- first->next = p;
- }
- }
-
- void input()
- {
- int n;
- printf("Please Input the Number of Process:");
- scanf("%d",&n);
- for(int i=0;i<n;i++){
- printf("process id No.%d\n",i+1);
- p=getpch(PCB);
- printf("process name:");
- scanf("%s", p->name);
- printf("process priority:");
- scanf("%d", &p->super);
- printf("process need running time:");
- scanf("%d", &p->ntime);
- p->rtime=0;
- p->state='W';
- p->next=NULL;
- sort();
- }
- }
-
- int space()
- {
- int len=0;
- PCB* pr=ready;
- while (pr!=NULL)
- {
- len++;
- pr = pr->next;
- }
- return(len);
- }
-
- void disp(PCB * pr)
- {
- printf("\n name state super ntime rtime\n");
- printf(" %s\t", pr->name);
- printf(" %c\t", pr->state);
- printf(" %d\t", pr->super);
- printf(" %d\t", pr->ntime);
- printf(" %d\t", pr->rtime);
- printf(" \n");
- }
-
- void look()
- {
- PCB* pr;
- printf("\n**** the running process is:%s", p->name);
- disp(p);
- pr = ready;
- printf("\n**** the ready process is:\n");
- while (pr != NULL)
- {
- disp(pr);
- pr = pr->next;
- }
- }
-
- void running()
- {
- PCB *pr;
- (p->rtime)++;
- pr=ready;
- while(ready!=NULL){
- (ready->super)++;
- ready=ready->next;
- }ready=pr;
- if (p->rtime == p->ntime)
- {
- printf("\n process[%s] is finished.\n", p->name);
- free(p);
- }
- else
- {
- (p->super)=(p->super)-2;
- p->state = 'W';
- sort();
- }
- }
-
- main( )
- { int len, h=0; /*len存放就绪进程的数量,h为运行的时间片序号*/
- char ch;
- input( ); /*接收用户输入,建立ready指向的进程链表*/
- len=space( ); /*获取就绪状态的进程数*/
- while((len!=0)&&(ready!=NULL))
- { ch=getchar( );
- h++;
- printf("\n The execute number:%d",h);
- p=ready; /*运行就绪队列的队首进程*/
- ready=p->next; /*更改就绪队列指针*/
- p->next=NULL; /*摘下队首进程*/
- p->state='R'; /*置为运行状态*/
- look( ); /*查看并显示运行和就绪的进程信息*/
- running( );
- printf("\n please press any key to continue......");
- ch=getchar( );
- }
- printf("\n\n All processes are finished.\n");
- ch=getchar( );
- }
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。