1.编写并调试一个模拟的进程调度程序,采用“最高优先数优先”调度算法对五个进程进行调度。
“最高优先数优先”调度算法的基本思想是把 CPU 分配给就绪队列中优先数最高的进程。静态优先数是在创建进程时确定的,并在整个进程运行期间不再改变。 动态优先数是指进程的优先数在创建进程时可以给定一个初始值,并且可以按一定原则修改优先数。例如:在进程获得一次 CPU 后就将其优先数减少 1,或者,进程等待的时间超过某一时限时增加其优先数的值,等等。
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type *)malloc(sizeof(type))
struct pcb
{ /* 定义进程控制块 PCB */
char name[10]; //进程名
char state; //进程状态
int super; //优先数
int ntime; //需要运行时间
int rtime; //已用 CPU 时间
struct pcb *link;
} *ready = NULL, *p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert = 0;
if ((ready == NULL) || ((p->super) > (ready->super))) /*优先级最大者,插入队首*/
{
p->link = ready;
ready = p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first = ready;
second = first->link;
while (second != NULL)
{
if ((p->super) > (second->super)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p->link = second;
first->link = p;
second = NULL;
insert = 1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first = first->link;
second = second->link;
}
}
if (insert == 0)
first->link = p;
}
}
void input() /* 建立进程控制块函数*/
{
int i, num;
printf("\n 请输入进程数?");
scanf("%d", &num);
for (i = 0; i < num; i++)
{
printf("\n 进程号 No.%d:\n", i);
p = getpch(PCB);
printf("\n 输入进程名:");
scanf("%s", p->name);
printf("\n 输入进程优先数:");
scanf("%d", &p->super);
printf("\n 输入进程运行时间:");
scanf("%d", &p->ntime);
printf("\n");
p->rtime = 0;
p->state = 'w';
p->link = NULL;
sort(); /* 调用 sort 函数*/
}
}
int space()
{
int l = 0;
PCB *pr = ready;
while (pr != NULL)
{
l++;
pr = pr->link;
}
return (l);
}
void disp(PCB *pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n qname \t state \t super \t ntime \t rtime \t \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 check() /* 建立进程查看函数 */
{
PCB *pr;
printf("\n **** 当前正在运行的进程是:%s", p->name); /*显示当前运行进程*/
disp(p);
pr = ready;
printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while (pr != NULL)
{
disp(pr);
pr = pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n", p->name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if (p->rtime == p->ntime)
destroy(); /* 调用 destroy 函数*/
else
{
(p->super)--;
p->state = 'w';
sort(); /*调用 sort 函数*/
}
}
int main() /*主函数*/
{
printf("实验一 采用“最高优先数优先”调度算法对五个进程进行调度\n\n");
int len, h = 0;
char ch;
input();
len = space();
while ((len != 0) && (ready != NULL))
{
ch = getchar();
h++;
printf("\n The execute number:%d \n", h);
p = ready;
ready = p->link;
p->link = NULL;
p->state = 'R';
check();
running();
printf("\n 按任一键继续......");
}
printf("\n\n 进程已经完成.\n");
ch = getchar();
}
2.编写并调试一个模拟的进程调度程序,采用“轮转法”调度算法对五个进程进行调度。
轮转法可以是简单轮转法、可变时间片轮转法,或多队列轮转法。
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type *)malloc(sizeof(type))
struct pcb
{ /* 定义进程控制块 PCB */
char name[10];
char state; //进程状态
int super; //进程优先级
int ntime; //进程运行时间
int rtime; //已运行时间
struct pcb *link;
} *ready = NULL, *p;
typedef struct pcb PCB;
void insert(pcb *node) /*在队尾添加新进程*/
{
if(ready == NULL) {
node->link = ready;
ready = node;
}
pcb *temp;
temp = ready;
while(temp->link != NULL) {
temp = temp->link;
}
temp->link = node;
node->link = NULL;
}
void input() /* 建立进程控制块函数*/
{
int i, num;
printf("\n 请输入进程数?");
scanf("%d", &num);
for (i = 0; i < num; i++)
{
printf("\n 进程号 No.%d:\n", i);
p = getpch(PCB);
printf("\n 输入进程名:");
scanf("%s", p->name);
printf("\n 输入进程运行时间:");
scanf("%d", &p->ntime);
printf("\n");
p->rtime = 0;
p->state = 'w';
p->link = NULL;
insert(p);
}
}
int space()
{
int l = 0;
PCB *pr = ready;
while (pr != NULL)
{
l++;
pr = pr->link;
}
return (l);
}
void disp(PCB *pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n qname \t state \t ntime \t rtime \t \n");
printf("|%s\t", pr->name);
printf("|%c\t", pr->state);
printf("|%d\t", pr->ntime);
printf("|%d\t", pr->rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB *pr;
printf("\n **** 当前正在运行的进程是:%s", p->name); /*显示当前运行进程*/
disp(p);
pr = ready;
printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while (pr != NULL)
{
disp(pr);
pr = pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n", p->name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if (p->rtime == p->ntime)
destroy(); /* 调用 destroy 函数*/
else
{
p->state = 'w';
insert(p);
}
}
int main() /*主函数*/
{
printf("实验一 采用“轮转法”调度算法对五个进程进行调度。\n\n");
int len, h = 0;
char ch;
input();
len = space();
while ((len != 0) && (ready != NULL))
{
ch = getchar();
h++;
printf("\n The execute number:%d \n", h);
p = ready;
ready = p->link;
p->link = NULL;
p->state = 'R';
check();
running();
printf("\n 按任一键继续......");
}
printf("\n\n 进程已经完成.\n");
ch = getchar();
}