操作系统实习
进程调度算法
设计目的:
进程管理是操作系统中的重要功能,用来创建进程、撤消进程、实现进程状态转换,它提供了在可运行的进程之间复用CPU的方法。在进程管理中,进程调度是核心,因为在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态,当就绪进程个数大于处理器数目时,就必须依照某种策略决定哪些进程优先占用处理器。本设计模拟在单处理器情况下的进程调度,加深对进程运行状态和进程调度过程、调度算法的理解。
设计内容:
设计程序模拟单处理机系统中的进程调度算法,实现动态优先权进程调度算法, 对N个进程采用动态优先权算法的进程调度。
设计思路:
1.每个用来标识进程的进程控制块PCB,包括以下信息:进程标识数ID,进程优先数PRIORITY,进程已占用的CPU时间CPUTIME,进程还需占用的CPU时间NEEDTIME,进程状态STATE等。
2.优先数改变的原则:进程在就绪队列中呆一个时间片,优先数增加1,进程每运行一个时间片优先数减3。(注:优先数改变策略也可以自己设计)
3.设置调度前的初始状态。
4.将每个时间片内的进程情况显示出来。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
/*****************************************/
struct pcb { /* 定义进程控制块PCB */
char id[10];
char state;
int priority;
int needtime;
int CPUtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
/*****************************************/
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->priority)>(ready->priority))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->priority)>(second->priority)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else
{
first=first->link;
second=second->link;
}
}/* 插入进程优先数最低,则插入到队尾*/
if(insert==0) first->link=p; // p->link=NULL
}
}
void input() /* 建立进程控制块函数*/
{
int i,num;
system("CLS"); /*清屏*/
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->id);
printf("\n 输入进程优先数:");
scanf("%d",&p->priority);
printf("\n 输入进程运行时间:");
scanf("%d",&p->needtime);
printf("\n");
p->CPUtime=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 id state priority needtime CPUtime \n");
printf("|%s ",pr->id);
printf("|%c ",pr->state);
printf("|%d ",pr->priority);
printf("|%d ",pr->needtime);
printf("|%d ",pr->CPUtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:%s",p->id); /*显示当前运行进程*/
disp(p); //输出显示当前进程
pr=ready;
printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] Finish.\n",p->id);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
PCB* q;
int len_w;
len_w=space();//统计就绪队列长度
q=ready;//指向就绪队列
while((len_w!=0)&&(q!=NULL))
{
q->priority=(q->priority)+1;//进程在就绪队列中呆一个时间片,优先数增加1
q=q->link;
}
(p->CPUtime)++;
if(p->CPUtime==p->needtime)
destroy(); /* 调用destroy函数*/
else
{
p->priority=(p->priority)-3; //进程每运行一个时间片优先数减3
p->state='W';
sort(); /*调用sort函数*/
}
}
void main() /*主函数*/
{
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; //将p指针指向就绪队列第一个
ready=p->link;//ready指向下一个进程
p->link=NULL; //将第一个进程弹出就绪队列
p->state='R'; //弹出就绪队列的第一个进程状态置为运行态
check();
running();
printf("\n 按任一键继续......");
ch=getchar();
}
printf("\n\n 进程已经完成.\n");
ch=getchar();
}
算法流程图: