操作系统处理机调度实验报告.doc
文本预览下载声明
操作系统 实验报告
学 号 姓 名 时 间 2011年12月16日 专 业 网络工程 班 级 5班 实验题目: 处理机调度 实验目的:
(1)通过编写程序实现进程或作业先来先服务、高优先权、按时间片轮转调度算法,进一步掌握进程调度的概念和算法,加深对处理机分配的理解。
(2)了解Linux中进程(线程)的调度机制。
(3)学习使用Linux中进程(线程)调度算法,掌握相应的与调度有关的函数。
实验内容与步骤:
程序说明:?
1)先来先服务算法:如果早就绪的进程排在就绪队列的前面,迟就绪的进程排在就绪队列的后面,那么先来先服务(FCFS:first come first service)总是把当前处于就绪队列之首的那个进程调度到运行状态。
2)轮转法就是按一定时间片(记为q)轮番运行各个进程。如果q是一个定值,则轮转法是一种对各进程机会均等的调度方法。
3)优先级调度的基本思想是,把当前处于就绪队列中优先级最高的进程投入运行,而不管各进程的下一个CPU周期的长短和其他因素。
具体步骤:
分析问题,提出解决问题的算法
编制程序
程序调试
记录实验结果,以及思考是否能够改善算法
程序及运行结果:
#include stdio.h
#include stdlib.h
#define P_NUM 5
#define P_TIME 50
enum state{
ready,
execute,
block,
finish
};
struct pcbb{
char name[4];
int priority; //数越大优先级越高
int cputime; //已占用CPU的时间
int needtime; //执行时间
int count;
enum state process;
struct pcbb *next;
};
typedef struct pcbb pcb;
void display_menu(){
printf(CHOOSE THE ALGORITHM:\n);
printf(1 PRIORITY\n);
printf(2 ROUNDROBIN\n);
printf(3 EXIT\n);
}
pcb* get_process(){
pcb *q;
pcb *p; //头指针
pcb *t; //尾指针
int i = 0;
printf(input name and time\n);
while (i P_NUM){
q=(pcb *)malloc(sizeof(pcb));
scanf(%s,q-name);
scanf(%d,q-needtime);
q-cputime = 0;
q-priority = P_TIME - q-needtime;
q-process = ready;
q-next = NULL;
if(i==0){
p = q;
t = q;
}
else{
t-next = q;
t = q;
}
i++;
}
return p;
}
void free_process(pcb *p){
pcb *q;
while(p!= NULL){
q = p;
p = p-next;
free(q);
}
}
void display(pcb *p){
printf(name cputime needtime priority state\n);
while(p){
printf(%s,p-name);
printf( );
printf(%d,p-cputime);
printf( );
printf(%d,p-needtime);
printf( );
printf(%d,p-priority);
printf( );
switch(p-process){
case ready:printf(ready\n);break;
case execute:printf(execute\n); break;
case block:printf(block\n); break;
case finish:printf(fini
显示全部