FCFS算法和基于时间片轮转调度.doc
文本预览下载声明
Step 1、正确理解各调度算法的基本思想;
(1)、FCFS算法和基于时间片轮转调度算法,可设其PCB的格式为:
进程名(进程标识) ID 链接指针 NEXT指针 到达时间 A_time 估计运行时间 S_time 进程状态 进程名 链接指针 进程的优先权 估计运行时间 进程状态 (2)、高优先权调度算法可设PC为:
Step 2、在正确理解各调度算法的基础上编写出相应的程序。 下面以基于时间片轮转调度算法的源程序:
#include stdio.h
#include stdafx.h
#include malloc.h
typedef struct node
{
int ID; //进程名
int A_time; //到达时间
int S_time; //服务时间
struct node *next;
}PCB; //进程结构定义
//全局变量定义
PCB *ReadyQueue;
int C_time=0;
//自定义函数
PCB *Init_Queue() //创建就绪队列
{
PCB *p;
p=(PCB *)malloc(sizeof(PCB));
p-next=NULL;
return p;
}
PCB *Dequeue(PCB *Linklist) //进程执行结束、删除该进程
{
PCB *p;
p=Linklist-next;
Linklist-next=Linklist-next-next; //删除队首节点
p-next=NULL;
return p; //取出队首节点
}
void Enqueue(PCB *Linklist,PCB *p) //插入新的进程
{
PCB *q;
q=Linklist;
while ((q-next)!=NULL)
q=q-next;
q-next=p;
}
//主函数
int main(int argc, char* argv[])
{
char ch;
PCB *p;
ReadyQueue=Init_Queue(); //初始化就绪队列
while (1)
{
printf(\nGreate a new Process(y/n)?);
ch=getchar();
while (ch==y) //当输入y时表示要创建一个新的进程
{
p=(PCB *)malloc(sizeof(PCB));
printf(\nID:);
scanf(%d,(p-ID)); //输入新建进程的序号
printf(\nS_time:);
scanf(%d,(p-S_time)); //输入新建进程需要服务的时间
p-A_time=C_time; //取新建进程到达时间为到目前为止系统运行时间
p-next=NULL;
Enqueue(ReadyQueue,p); //将新建进程插入就绪队列
C_time++;
printf(\nGreate a new Process(y/n)?); //是否有新的进程进入就绪队列
getchar();
ch=getchar();
}
if (ReadyQueue-next==NULL) //若就绪队列为空,表示没有进程请求服务
{
printf(Not exists process need services!!!\n);
break;
}
else
{
p=Dequeue(ReadyQueue); //取出队首节点,即进程调度
printf(\nTime=%d Run Process is:%d,C_time,p-ID);
p-S_time--; //服务时间减1
C_time++;
if (p-S_time==0) //该进程执行完毕,退出就绪队列
{
printf(\nWhen time=%d Process P%d run over!,C_time,p-ID);
free(p);
}
显示全部