实 验 报 告 书
课 程 名 :《操作系统原理》
题 目:
进程调度
班 级:
学 号:
姓 名:
《 操作系统原理 》实验报告
- 1 -
操作系统原理实验——进程调度实验报告
1 目的与要求:
1)本实验目的是通过对进程调度算法的实现和实验模拟,加深对操作系统进程调度操作功
能和进程调度算法的完整理解,培养和提高学生对操作系统开发的兴趣,以及进程调度程序的
开发与应用能力;
2)理论上必须深刻理解优先权调度算法和时间片轮转调度算法的基本思想和原理;
3)独立使用 C 编程语言编写优先权调度或时间片轮转算算法调度模拟程序;
4)按照实验题目要求独立正确地完成实验内容(编写、调试算法程序,提交程序清单及及
相关实验数据与运行结果)
2 实验内容或题目
1)设计有 5 个进程并发执行的模拟调度程序,每个程序由一个 PCB 表示。
2)模拟调度程序可任选两种调度算法之一实现(有能力的同学可同时实现两个调度算法)。
3)程序执行中应能在屏幕上显示出各进程的状态变化,以便于观察调度的整个过程。
4)本次实验内容(项目)的详细说明以及要求请参见实验指导书。
/*进程优先数*/
3 实验步骤与源程序
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node /*创建 PCB*/
{
char name[10];
int prio;
int cputime; /*进程占用 CPU 时间*/
int needtime; /*进程完成所需时间*/
int count;
char state; /*进程的状态*/
struct node *next; /*链指针*/
}PCB;
PCB *finish,*ready,*tail,*run;
int N;
firstin()
{
run=ready;
run->state='R';
ready=ready->next;
}
/*进程标识*/
/*计数器*/
/*创建就绪队列对头指针*/
void prt()
{
/*演示进程调度*/
《 操作系统原理 》实验报告
- 2 -
NAME
CPUTIME
NEEDTIME
PRIORITY
STATUS\n");
PCB *p;
printf("
if(run!=NULL)
printf("
%-10s%-10d%-10d%-10d %c\n",run->name,
run->cputime,run->needtime,run->prio,run->state);
p=ready;
while(p!=NULL)
{
%-10s%-10d%-10d%-10d %c\n",p->name,
printf("
p->cputime,p->needtime,p->prio,p->state);
p=p->next;
}
p=finish;
while(p!=NULL)
{
%-10s%-10d%-10d%-10d %c\n",p->name,
printf("
p->cputime,p->needtime,p->prio,p->state);
p=p->next;
}
getchar();
}
insert(PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q;
p1=ready;
r=p1;
b=1;
while((p1!=NULL)&&b)
if(p1->prio>=s->prio)
{
r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1)
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1;
ready=s;
《 操作系统原理 》实验报告
- 3 -
}
}
void create()
{
/*创建各个进程*/
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
for(i=1;i<=N;i++)
{
p=(PCB*)malloc(sizeof(PCB));
printf("Enter NAME of process:\n");
scanf("%s",na);
printf("Enter TIME of process(less than 50):\n");
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='w';
p->prio=50-time;
if(ready!=NULL)
/*假设优先级与耗时之和为 50*/
insert(p);
else
{
p->next=ready;
ready=p;
}
DISPLAY OF THE PROGRESS:\n");
}
system("cls");
printf("
printf("************************************************\n");
prt();
run=ready;
ready=ready->next;
run->state='R';
}
priority()
{
/*优先级算法调度*/
while(run!=NULL&&run->prio>=0)
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3;
if(run->needtime==0)
《 操作系统原理 》实验报告
- 4 -
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
firstin();
}
else
if((ready!=NULL)&&(run->prioprio))
{
run->state='W';
insert(run);
firstin();
}
}
prt();
}
main()
{
system("cls");
printf("Enter THE TOTAL NUMBER of PCB(less than 10 is better):\n");
scanf("%d",&N);
create();
priority();
}
4 测试数据与实验结果
《 操作系统原理 》实验报告
- 5 -
5 结果分