进程调度优先数 c语言,操作系统实验 使用C++完成处理器调度实习题目本实习有两题,可任选一题。第一题:设计一个按优先数调度算法实现处理器调度的程序。[提示]:(1) 假定系统有5个进程,每个进程用一...

在C++环境下建立空进程,输入

#include "stdio.h"

#include

#include

#define getpch(type) (type*)malloc(sizeof(type))

#define NULL 0

struct pcb { /* 定义进程控制块PCB */

char name[10];

char state;

int super;

int ntime;

int rtime;

struct pcb* link;

}*ready=NULL,*p;

typedef struct pcb PCB;

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

{

PCB *first, *second;

int insert=0;

if(ready==NULL)

{

p->link=ready;

ready=p;

}

else /* 进程比较优先级,插入适当的位置中*/

{

first=ready;

second=first->link;

while(second!=NULL)

{

first=first->link;

second=second->link;/*插尾*/

}

if(insert==0) first->link=p;

}

}

input() /* 建立进程控制块函数*/

{'

int i,num;

system("cls"); /*清屏*/

printf("\n 请输入进程号?");

scanf("%d",&num);

for(i=0;iname);

printf("\n 输入进程运行时间:");

scanf("%d",&p->ntime);

printf("\n");

p->rtime=0;p->state='w';

p->link=NULL;

sort(); /* 调用sort函数*/

}

}

int space()

{

int l=0; PCB* pr=ready;

while(pr!=NULL)

{

l++;

pr=pr->link;

}

return(l);

}

disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/

{

printf("\n qname \t state \t super \t ndtime \t runtime \n");

printf("|%s\t",pr->name);

printf("|%c\t",pr->state);

printf("|%d\t",pr->super);

printf("|%d\t",pr->ntime);

printf("|%d\t",pr->rtime);

printf("\n");

}

check() /* 建立进程查看函数 */

{

PCB* pr;

printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/

disp(p);

pr=ready;

printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/

while(pr!=NULL)

{

disp(pr);

pr=pr->link;

}

}

destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/

{

printf("\n 进程 [%s] 已完成.\n",p->name);

free(p);

}

running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/

{

(p->rtime)++;

if(p->rtime==p->ntime)

destroy(); /* 调用destroy函数*/

else

{

p->state='w';

sort(); /*调用sort函数*/

}

}

main() /*主函数*/

{

int len,h=0;

char ch;

input();

len=space();

while((len!=0)&&(ready!=NULL))

{

ch=getchar();

h++;

printf("\n The execute number:%d \n",h);

p=ready;

ready=p->link;

p->link=NULL;

p->state='R';

check();

running();

printf("\n 按任一键继续......");

ch=getchar();

}

printf("\n\n 进程已经完成.\n");

ch=getchar();

}

解析看不懂?求助智能家教解答查看解答

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值