操作系统原理课程实验报告.doc
文本预览下载声明
武汉纺织大学《操作系统原理》课程实验报告
姓名: 班级: _ __ 学号:
实验时间: 2012 年 11 月 5 日 指导教师:
实验名称: 进程调度与管理
一、实验目的
(1)了解进程的基本状态和创建方法;
(2)熟悉优先级进程调度的实现原理。
二、实验内容
(1)调试下面的进程管理程序,描述其实现的基本过程。
(2)了解进程的创建及优先级进程的调度的实现原理和方法,并观察实现的结果。
三、操作步骤
(1)进程管理程序实例:
/*进程管理*/
#includestdio.h
#includestdlib.h
#include string.h
#include ctime
#define WAIT 1
#define RUN 2
#define FINISH 3
typedef struct pcb
{
int num;
struct pcb *next;
int priority;
int timeneed;
int state;
}pcb;/*用此结构体来模拟一个进程*/
struct pcb *head;
struct pcb *run;
pcb *jccreat(int n)/*此函数用于创建进程队列*/
{
int i=1;
pcb *head,*p,*q;
srand(unsigned(time(NULL))); /*随机函数的初始化*/
head=(pcb *)malloc(sizeof(pcb));/*创建一个空头表*/
p=head;
for(i=1;i=n;i++)/*用循环来创建指定个结点*/
{
q=(pcb *)malloc(sizeof(pcb));
p-next=q;
q-num=i;
q-next=NULL;
q-priority=rand()%10+1;/*随机产生优先级*/
q-timeneed=rand()%10;/*随机产生运行时间*/
q-state=WAIT;
p=q;
}
return head;/*返回表头指针*/
}
pcb *getmaxpriority(struct pcb *head)/*此函数用来挑选一个优先级最大的进程来执行*/
{
struct pcb *p,*q;
int max;
p=head-next;
max=p-priority;/*初始max为队首结点的优先级*/
q=p;
while(p)
{
if(p-prioritymax)/*逐一比较,选出优先级最大的结点*/
{max=p-priority;
q=p;}
p=p-next;
}
return q;
}
void delect(struct pcb *head,struct pcb *run)/*此函数用来将运行完的进程删除出进程队列*/
{
struct pcb *q=head;
while(q-next)/*扫描进程队列,找到执行完了的进程*/
{
if(q-next-num==run-num)/*判断是不是已完成的进程*/
{
if(run-next!=NULL)
q-next=run-next;
else q-next=NULL;
free(run);/*释放申请的空间*/
return;
}
q=q-next;
}
}
void control()/*此函数是用来控制各个进程的执行和调度*/
{
struct pcb *p;
run=head-next;/* 初始让第一个进程运行*/
run-state=RUN;
while(run)
{
if(run-timeneed0)/*如果当前run指针指向的进程所需时间不为零,状态为运行状态,就让这个进程运行*/
if(run-state==RUN)
{printf(pcb%d is running.\n,run-num);
printf(Waiting list:);/*显示整个等待队列*/
p=head-next;
while(p)
{
if(p!=run)
printf(pcb%d
显示全部