实验四 模拟进程调度
一、实验目的:
1、理解PCB
2、理解进程的并发执行
3、理解进程的FCFS、动态优先权和时间片轮转三种调度算法,并模拟实现这三种算法
二、实验仪器及材料:
微型计算机、Linux系统
三、实验内容:
1、动态优先权调度算法模拟:
优先权= 1 + 等待时间/需要服务时间----最高响应比优先
(1) 输入一批作业的进程标识和估计运行时间,创建进程(初始化进程的PCB)、将其PCB插入就绪队列
(2) 当就绪队列非空、CPU空闲时,计算出每个就绪进程的优先权
(3) 将优先权最高的进程从就绪队列移入运行队列,状态改为运行
(4) 在屏幕上输出当前所有进程的状态和优先权信息(已完成进程不需此项)
(5) 让运行进程执行足够的服务时间,并追加所有就绪进程的等待时间
(6) 将运行进程移入完成队列
(7) 提示可以输入新的作业,如果有新作业输入,则转向(1) ,否则转向(2)
如果就绪队列为空,则结束
源代码:
#include
#include
typedef struct node
{
char name[10]; //进程标识符
float prin; //进程的优先级
int round; //进程轮转的时间片
int needtime; //进程还需要当CPU时间
int waittime; //进程进入系统后等待当CPU的时间
char state[10]; //进程当前状态
struct node *next; //链接指针
} PCB;
PCB *readyQueue = NULL, *runningQueue = NULL, *finishedQueue = NULL, *finishedQueueTail = NULL, *readyQueueTail = NULL, pcbPool[10];
int poolSize = 0;
char Ready[10] = "Ready"; char Running[10] = "Running"; char Finished[10] = "Finished";
void createPro(); //根据进程标识和估计运行时间,将其PCB插入就绪队列
void readyQueueNotNull(); //当就绪队列非空、CPU空闲时,计算每个就绪进程的优先权
void insertRunningQueue(); //将优先权最高当进程从就绪队列移入运行队列,状态改为运行
void displayQueue(); //将所有非结束进程的状态和优先权信息显示在屏幕
void runPro(); //让运行进程执行足够的服务时间
void addNewPro(); //提示可以输入新的进程
int main(int argc, char const *argv[])
{
createPro();
while (readyQueue != NULL)
{
readyQueueNotNull();
insertRunningQueue();
displayQueue();
runPro();
puts("");
addNewPro();
}
puts("All processes have run over");
return 0;
}
void createPro()
{
char name[10];
char stop = 'y';
int needtime, i = 0;
while (stop == 'y')
{
//获取用户输入进程名
puts("Please input the process name: ");
scanf("%s", name);
strcpy(pcbPool[i].name, name);
//获取用户输入进程需要时间
puts("Please input the need of time: ");
scanf("%d", &needtime);
pcbPool[i].needtime = needtime;
strcpy(pcbPool[i].state, Ready);
pcbPool[i].next = NULL;
pcbPool[i].prin = 0.0f;
pcbPool[i].waittime = 0;
if (i == 0)
{
readyQueue = &pcbPool[0];
readyQueueTail = &pcbPool[0];
}
else
{
readyQueueTail->next = &pcbPool[i];
readyQueueTail = &pcbPool[i];
}
if (++i > 9) break;
puts("Do you want to input new process(y or n): ");
scanf("%s", &stop);
}
poolSize = i + 1;
}
void readyQueueNotNull()
{
PCB *pcb = readyQueue;
while (1)
{
pcb->prin = 1 + (float)pcb->waittime / pcb->needtime;
if (pcb->next == NULL)
break;
else
pcb = pcb->next;
}
}
void insertRunningQueue()
{
PCB *currentPCB = readyQueue, *lastPCB = readyQueue, *lastOfChosePCB = readyQueue, *chosePCB = readyQueue;
while (1)
{
//选择优先级最高的进程
if (chosePCB->prin < currentPCB->prin)
{
lastOfChosePCB = lastPCB;
chosePCB = currentPCB;
}
//判断就绪队列是否结束
if (currentPCB->next != NULL)
{
lastPCB = currentPCB;
currentPCB = currentPCB->next;
}
else
{
break;
}
}
strcpy(chosePCB->state, Running); //将优先级最高的进程状态标识为运行时
runningQueue = chosePCB; //将优先级最高的进程从就绪队列移入运行队列
//将优先级最高的进程从就绪队列中移出
if (readyQueue == chosePCB)
readyQueue = readyQueue->next;
else
lastOfChosePCB->next = chosePCB->next;
}
void displayQueue()
{
if (readyQueue != NULL)
{
PCB pcb = *readyQueue;
while (1)
{
printf("Process: %s State: %s prin: %f\n", pcb.name, pcb.state, pcb.prin);
if (pcb.next == NULL)
break;
else
pcb = *(pcb.next);
}
}
if (runningQueue != NULL)
printf("Process: %s State: %s prin: %f\n", runningQueue->name, runningQueue->state, runningQueue->prin);
printf("Process %s is running,please wait......\n", runningQueue->name);
}
void runPro()
{
sleep(runningQueue->needtime);
if (readyQueue != NULL)
{
PCB *pcb = readyQueue;
while (1)
本文档为【惠州学院操作系统模拟进程调度实验】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。