进程调度 作业调度 主存空间的分配与回收 文件系统 联系客服

发布时间 : 星期三 文章进程调度 作业调度 主存空间的分配与回收 文件系统更新完毕开始阅读da65e0ee551810a6f5248675

4、程序源代码

#include #include #include #define

(type*)malloc(sizeof(type)) getpch(type)

typedef struct jcb JCB; typedef struct source SOURCE;

int Select;

int Systemtime = 0; 间*/

int JCBnum = 0; int JCBtime = 0; */

double JCBtotaltime =0; 时间*/

struct source{ 求结构*/

char memery[5]; */ int machine; 量*/

};

struct jcb { 制块PCB */

char name[10];

char state; double super; 先权 */

int ntime; 时间 */

int rtime; 时间 */ int ptime;

int ftime; source *needsources; */

struct jcb* link; 控制块的地址 */ }*ready=NULL,*run,*p;

void display() { /*系统初始时 int c; do

/*总作业数*/ {

/*总周转时间 system(\

/*总带权周转 printf(\ <<单道批处理系统作业调度演示>>\\n\

printf(\ /*定义资源需******************\\t\\t\

printf(\ 1.先来先服务算法.\ /*主存需求 printf(\ 2.最短作业优先算法.\ printf(\ 3.响应比高者优先算法 /*磁带机数\

printf(\ 0.退出程序.\

printf(\/* 定义作业控******************\\t\\t\\n\

printf(\请输入选择所要操作(0-3):\ scanf(\ /* 状态 */ system(\ /* 响应比优 }while(c<0||c>3) ; switch(c) /* 需要运行 {

case 0: /* 开始运行 exit(0);

break; /*提交时间*/ case 1: /*完成时间*/

Select=1; /*资源需求链

break;

/* 下一个作业 case 2: Select=2;

17

break;

case 3: Select=3; break; default: }

void sort() /* 建立对进程进行优先级排列函数*/

{

JCB *first, *second,*temp; int insert=0; switch(Select)

{

case 1: /*先来先去 break; }

if((ready==NULL)||((p->ntime)<(ready->ntime))) /*队首*/

{

p->link=ready; ready=p;

}

else /* 往后搜索适当的位置插入*/ { first=ready;

second=first->link;

while(second!=NULL) /*插入队伍中间*/ {

if((p->ntime)<(second->ntime)) { p->link=second; first->link=p; second=NULL; insert=1; }

else {

first=first->link; second=second->link; /*

} } if(insert==0) 插入队尾*/

服务算法*/ if(ready==NULL) /*队首空插入队首*/

{

p->link=ready; ready=p;

}

else /*否则插入{

队尾*/

first=ready;

second=ready->link; while(second!=NULL) { }

first=first->link; second=second->link;

first->link=p;

}

break; case 3: /*响应比高者优先算法*/

/*响应比=(等待

时间+需求时间)/需求时间*/

temp=ready;

p->super=(double)(Systemtime-p->ptime+p->ntime)/(double)(p->ntime); /*计算插入队列作业优先权*/

first->link=p;

}

break;

case 2: /*最短作业

优先算法*/

18

while(temp) /*计算就

{

temp->super=(double)(Systemtime-temp-

temp=temp->link;

绪队列作业优先权*/ default:

}

void input() /* 建立作业输入函数*/ {

int i,num;

SOURCE *q;

printf(\请输入作业数目:\ scanf(\

JCBnum+=num; /*总作业数*/

for(i=0;i

{

printf(\【作业号No.%d】\\n\ p=getpch(JCB); q=getpch(SOURCE); printf(\请输入作业名:\

scanf(\ getchar();

printf(\请输入作业需要运行时间:\

scanf(\ getchar();

printf(\请输入作业主存资源需求:\

scanf(\

getchar();

printf(\请输入作业磁带机数量需求:\

scanf(\

p->needsources=q; p->ptime=Systemtime; /*作业提交时间为系统默认时间*/

p->super=0; p->ftime=0; /*作业完成时间*/

first->link=p; p->state='W';

p->link=NULL; sort(); }

19

}

break;

>ptime+temp->ntime)/(double)(temp->ntime); }

/*响应比越大优先权越高*/

if((ready==NULL)||((p->super)>(ready->ntime))) /*队首*/ { p->link=ready; ready=p;

}

else /* 往后搜索适当的位置插入*/

{

first=ready;

second=first->link;

while(second!=NULL) /*插入队伍中间*/

{

if((p->super)>(second->super)) { p->link=second; first->link=p; second=NULL; insert=1; }

else {

first=first->link; second=second->link; }

}

if(insert==0) /* 插入队尾*/ } break;

}

void disp(JCB *pr) /*建立作业显示函数*/ {

printf(\作业名 提交时间 需求时间 响应比 即时状态 主存需求 磁带机数量 \\n\

printf(\ %s\\t\ printf(\ %d\\t\ printf(\ %d\\t\ printf(\ %.2f\\t\ printf(\ %c\\t\

printf(\ %s\\t\;

printf(\ %d\\t\hine);

printf(\ }

void check() /* 建立作业查看函数 */ {

JCB* pr;

printf(\*********************当前

{

JCB *pr=run; run->ftime=Systemtime;

printf(\【作业 [%s] 已完成...】\\n\

printf(\开始时间 完成时间 周转时间 带权周转时间 释放主存资源 释放磁带机数量\\n\

printf(\ %d\\t\ printf(\ %d\\t\

printf(\ %d\\t\

printf(\ %f\\t\ptime)/(double)run->ntime);

printf(\ %s\\t\y); printf(\ %d\\t\machine); printf(\

JCBtime += Systemtime-run->ptime; /*更新总周转时间*/

JCBtotaltime += JCBtime/(double)run->ntime; /*更新总带权周转时间*/ run=run->link;

正在运行的作业**********************\ free(pr); /*显示当前运行作业*/ pr=NULL; disp(run); } pr=ready;

printf(\**********************当前就绪队列状态************************\ /*显示就绪队列状态*/ while(pr!=NULL) {

disp(pr); pr=pr->link; } }

void destroy() /*建立作业撤销函数)*/

20

void running() /* 建立作业运行函数*/ { run->rtime=Systemtime; /*作业开始时间为系统当前时间*/ Systemtime += run->ntime; /*更新系统时间*/ destroy(); }

void main() /*主函数*/ {

int h=0;