㈠ 求这道操作系统调度算法类题目的答案。
首先在一个程序进行IO操作的时候,CPU可以释放出来给另一个程序进行计算
然后系统可以同时进行两个计算,即双核CPU可同时工作用于程序的计算
设每个字母代表一个时间片长度20ms,C表示计算, I表示IO,D表示等待
则各程序的时间片分配如下:
P1:C I I I I CC
P2:CCCCCCC I CC
P3:DCCCCDDC I I I I CC
其中P1和P2的优先级最高,按所给操作用时直接分配即可
P3优先级最低,第一个时间片P1和P2在同时计算,所以P3要等待
随后P1进入IO,其CPU空闲,可以分配给P3进行计算
在计算了4个时间片80ms后,P1结束IO抢占了P3的CPU,所以P3又继续等待2个时间片
后面P1程序结束,不会再抢占CPU,P3按剩余操作用时直接分配即可
所以最短用时为最后P3完成所需的时间,共14个时间片,280ms
㈡ 动态优先级调度算法
给你两个例子,第一个:http://dev.csdn.net/article/53/53415.shtm
第二个:
#include "stdio.h"
#include <STDLIB.H>
#include <CONIO.H>
#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->super)>(ready->super))) /*优先级最大者,插入队首*/
{
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) first->link=p;
}
}
input() /* 建立进程控制块函数*/
{
int i,num;
//clrscr(); /*清屏*/
printf("\n 请输入进程号?");
scanf("%d",&num);
for(i=0;i<NUM;I++) scanf(?%s?,p- 输入进程名:?); printf(?\n p="getpch(PCB);" 进程号No.%d:\n?,i); {>name);
printf("\n 输入进程优先数:");
scanf("%d",&p->super);
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->super)--;
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();
}
㈢ 求一份儿C语言优先级调度算法要求如下
#include "string.h"
#define n 10 /*假定系统中可容纳的作业数量为n*/
typedef struct jcb
{char name[4]; /*作业名*/
int length; /*作业长度,所需主存大小*/
int printer; /*作业执行所需打印机的数量*/
int tape; /*作业执行所需磁带机的数量*/
int runtime; /*作业估计执行时间*/
int waittime; /*作业在系统中的等待时间*/
int next; /*指向下一个作业控制块的指针*/
}JCB; /*作业控制块类型定义*/
int head; /*作业队列头指针定义*/
int tape,printer;
long memory;
JCB jobtable[n]; /*作业表*/
int jobcount=0; /*系统内现有作业数量*/
shele( )
/*作业调度函数*/
{float xk,k;
int p,q,s,t;
do
{p=head;
q=s=-1;
k=0;
while(p!=-1)
{ if(jobtable[p].length<=memory&&jobtable[p].tape<=tape&&jobtable[p].printer<=printer)
{ /*系统可用资源是否满足作业需求*/
xk=(float)(jobtable[p].waittime)/jobtable[p].runtime;
if(q==0||xk>k) /*满足条件的第一个作业或者作业q的响应比小于作业p的响应比*/
{k=xk; /*记录响应比*/
q=p;
t=s;
}/*if*/
}/*if*/
s=p;
p=jobtable[p].next; /*指针p后移*/
}/*while*/
if(q!=-1)
{ if(t==-1) /*是作业队列的第一个*/
head=jobtable[head].next;
else
jobtable[t].next=jobtable[q].next;
/*为作业q分配资源:分配主存空间;分配磁带机;分配打印机*/
memory=memory-jobtable[q].length;
tape=tape-jobtable[q].tape;
printer=printer-jobtable[q].printer;
printf("选中作业的作业名:%s\n",jobtable[q].name);
}
}while(q!=-1);
}/*作业调度函数结束*/
main( )
{char name[4];
int size,tcount,pcount,wtime,rtime;
int p;
/*系统数据初始化*/
memory=65536;
tape=4;
printer=2;
head=-1;
printf("输入作业相关数据(以作业大小为负数停止输入):\n");
/*输入数据,建立作业队列*/
printf("输入作业名、作业大小、磁带机数、打印机数、等待时间、估计执行时间\n");
scanf("%s%d%d %d %d %d",name,&size,&tcount,&pcount,&wtime,&rtime);
while(size!=-1)
{/*创建JCB*/
if(jobcount<n)p=jobcount;
else { printf("无法再创建作业\n");
break;
}
jobcount++;
/*填写该作业相关内容*/
strcpy(jobtable[p].name,name);
jobtable[p].length=size;
jobtable[p].printer=pcount;
jobtable[p].tape=tcount;
jobtable[p].runtime=rtime;
jobtable[p].waittime=wtime;
/*挂入作业队列队首*/
jobtable[p].next=head;
head=p;
/* 输入一个作业数据*/
printf("输入作业名、作业大小、磁带机数、打印机数、等待时间、估计执行时间\n");
scanf("%s%d%d%d%d%d",name,&size,&tcount,&pcount,&wtime,&rtime);
}/*while*/
shele( ); /*进行作业调度*/
}/*main( )函数结束*/