操作系统教程
——进程调度算法
院 系
班 级
学 号
姓 名
计算机与软件学院
08 软件工程 2 班
20081344066
何丽茗
进程调度算法的模拟实现
实验目的
1.本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。
2.利用程序设计语言编写算法,模拟实现先到先服务算法 FCFS、轮转调度算法 RR、最
短作业优先算法 SJF、优先级调度算法 PRIOR、最短剩余时间优先算法 SRTF。
3.进行算法评价,计算平均等待时间和平均周转时间。
实验内容及结果
1.先来先服务算法
2.轮转调度算法
3. 优先级调度算法
4. 最短时间优先算法
5. 最短剩余时间优先算法
实验总结
在此次模拟过程中,将 SRTF 单独拿了出来用指针表示,而其余均用数组表示。
完整代码
【Srtf.cpp代码如下:】
//最短剩余时间优先算法的实现
#include
#include
#include
typedef struct
{
int remain_time;
int arrive_time;
int Tp;
int Tc;
int To;
int number;
}Process_Block;
typedef struct _Queue
{
Process_Block PB;
struct _Queue *next;
}_Block,*Process;
typedef struct
{
Process head;
Process end;
}Process_Queue;
Process_Queue PQ;
int
Process
t;
Run_Now;
void InitQueue(Process_Queue PQ)
{
PQ.head ->next = NULL;
PQ.end
->next = PQ.head;
}/*初始化队列*/
int IsEmpty(Process_Queue PQ)
{
if(PQ.end->next == PQ.head)
//进程剩余执行时间
//进程到达时间
//进入就绪队列的时间
//进入执行队列的时间
//进程执行结束的时间
//进程编号
//定义进程模块
//定义一个进程模块队列中结点
//队列头指针
//队列尾指针
//进程队列
//定义一个全局队列变量
//全局时间
//当前正在运行的进程,作为全局变量
return 1;
//队列空的条件为头指针指向尾指针并且尾指针指向头指针
else
return 0;
}/*判定队列是否为空队列*/
void EnQueue(Process_Queue PQ,Process P)
{
Process temp =(Process)malloc(sizeof(_Block));
temp = PQ.end;
temp->next->next = P;
PQ.end->next = P;
}/*插入队列操作*/
Process DeQueue(Process_Queue PQ)
{
if(IsEmpty(PQ))
return NULL;
Process temp = PQ.head->next;
PQ.head->next= temp ->next;
if(PQ.end->next == temp)
PQ.end->next = PQ.head;
return temp;
}/*出列操作*/
Process ShortestProcess(Process_Queue PQ)
{
if(IsEmpty(PQ))
//如果队列为空,返回
{
}
if(!Run_Now)
return NULL;
else
return Run_Now;
Process temp,shortest,prev;
int min_time;
if(Run_Now)
//如果当前有进程正在执行,
{
}
else
{
}
shortest = Run_Now;
//那么最短进程初始化为当前正在执行的进程,
min_time = Run_Now->PB.remain_time;
//如果当前没有进程执行,
shortest = PQ.head->next;
//则最短进程初始化为队列中第一个进程
min_time = PQ.head->next->PB.remain_time;
temp = PQ.head;
prev = temp;
while(temp->next)
{
}
if(temp->next->PB.remain_time next;
//则保存当前进程,
min_time = shortest->PB.remain_time;
prev=temp;
//及其前驱
temp=temp->next;
if(shortest == PQ.end->next)
//如果最短剩余时间进程是队列中最后一个进程,
PQ.end->next = prev;
//则需要修改尾指针指向其前驱
prev->next = shortest->next;
//修改指针将最短剩余时间进程插入到队头
return shortest;
}/*调度最短剩余时间的进程至队头*/
void Run()
{
Run_Now->PB.remain_time--;
//某一时间运行它的剩余时间减
return;
}/*运行函数*/
void Wait()
{
}
return ;
int sum(int array[],int n)
{
}
int i,sum=0;
for(i=0;i