实验目的
多道程序设计中,经常是若干个进程同时处于就绪状态,为了使系统中的各进程有条不紊地运行,必须选择某种调度策略,以选择一个进程占用处理机。本次实验设计一个模拟单处理机调度的算法,以加深处理机调度的算法的理解。
实验内容
1.按照轮转(时间片)算法设计模拟调度程序。
2.输出进程的调度过程。
源程序
#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<malloc.h>
#define slice_time 10 //定义时间片的长度为10
//定义进程控制块PCB
struct pcb
{
int id; //进程号
int status; //进程状态 0-Ready, 1-Run, 2-Finish
int arrive_time; //进程到达时间
int time; //估计运行时间
int run_time; //已运行时间
int wait_time; //等待时间
int priority; //优先级
struct pcb* next; //链接指针
};
#define length sizeof(struct pcb)
int cur_time=0; //系统当前 运行 时间
int num=0; //进程的个数
struct pcb *ready_head=NULL;
struct pcb *pcb_head=NULL;
struct pcb *finish_head=NULL;
/*读文件数据,将文件中给出的进程放入PCB队列,以便后续使用
返回值: 0-失败 1-成功*/
int readData()
{
FILE *fp;
char fname[20];
cout<<"注意:文件中应包含以下信息:\n";
cout<<"进程ID 到达时间 估计运行时间 优先级\n";
cout<<"并且应按到达时间顺序排列!\n\n";
cout<<"请输入进程流文件名:";
cin>>fname;
if((fp=fopen(fname,"r"))==NULL)
{
cout<<"错误,文件打不开,请检查文件名"<<endl;
return 0;
}
else
{
//建立PCB链表
struct pcb *p1, *p2;
pcb_head=NULL;
p1=p2