OS模拟进程优先级调度
一条大三的舔狗,为了舔的老师满意,同时也为了自己的学分,自己写下了此实验作业。
当然自己学到才是最好的,其他的说实话不用太在意。一切为了自己
题目
- 设计一个有6个进程并发运行的模拟进程调度程序,每个进程由一个进程控制块(PCB)标志,每个进程处于运行(run)、就绪(ready)、阻塞(blocked)和完成(finish)状态之一。要求分别用优先数法、轮转法和短进程优先法进行调度和比较,并且显示打印进程状态参数变化情况。
- 自选设计一个进程调度算法,并加以实现。
假装分析
由于blocked阻塞队列我不是很懂,所以我就实现了run、ready、finish这三个队列
对于PCB这个进程标志块我们建立一个结构体,里面的内容有
- 进程标识号(name)
- 链指针(*next)
- 优先数/轮转时间片数(priority/roundTime)
- 占用CPU时间片数(cpuTime)
- 进程所需时间片数(needTime)
- 进程状态(state)
然后呢,我们用这个结构体定义三个指针
- run队列指针
- ready队列指针
- finish队列指针
最后我们就可以开始大闹天宫了
程序逻辑
首先我们用结构体创建链表,对于每个进程我们初始化都是以随机数作为她们的优先级 、占用CPU时间片数 、进程所需要时间片数 。进程标识号(name)我们用添加或者说创建顺序来表示,例如:1、2、3。进程状态和队列指针是一样多的,也是三个状态。
对于每个新建的节点(进程)我们都把她按照优先级(从大到小)插入ready队列里面,即如下图所示:

添加完之后我们就可以开始运行这个进程队列了,那么对于每次运行,我们需要使用run指针指向ready的队列的头节点(进程),然后ready指针指向她的下一个节点(进程),然后修改一下run队列节点(进程)的状态(state),这样run队列就可以运行了(记得run队列的next是NULL,得益于只能处理一个进程)。之后我们运行的时候,对其优先级自减3、needTime自减1、cpuTime自加1。
每次运行完后,run队列进程与ready队列头进程的优先级对比,如果ready头进程优先级大,则把run队列按优先级大小插入ready队列,取ready队列头进程就好,记得修改状态(state),就这样运行直到needTime等于0,那么就把她加入到finish的队列,这样就完成了这个实验的一部分吧。

实际运行
接下来的图片我相信你们也不会看的。
可以看,但没必要。






可以看见,needTime最后转移到了cpuTime。(废话!)
源代码
//
// Created by CongTsang on 2018/10/17. CopyRight
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define Ready 0
#define Run 1
#define Finish 2
typedef struct node
{
int name;
int cpuTime;
int needTime;
int priority;
int state;
struct node* next;
}PCB;
PCB *run, *ready, *finish;
int N; //Processes
string state_to_str(int _state)
{
switch(_state)
{
case Ready: return "Ready";
case Run: return "Run";
case Finish: return "Finish";
}
return "";
}
void display(char algo, PCB *head)
{
if (algo == 'P')
{
while (head != NULL)
{
printf(" %-14d%-14d%-18s%-17d%d\n",
head->name,head->priority,state_to_str(head->state).c_str(),
head->cpuTime,head->needTime);
head = head->next;
}
}else {
cout<<"R";
}
}
void showProcess(char algo)
{
cout<<"=============================================================================="<<endl;
PCB *tmp;
if (algo == 'P')
{
cout<<"| ProsId | Prio | ProsState | CPUTime | needTime |"<<endl;
if (run != NULL)
display(algo,run);
tmp = ready;
if (tmp != NULL)
display(algo,tmp);
tmp = finish;
if (tmp != NULL)
display(algo,tmp);
}else {
cout<<"1"<<endl;
}
}
void insert(PCB *p)
{
if (p->state == Ready)// 插入Ready队列
{
PCB *cur, *pre;
cur = ready, pre = NULL;
while (cur && cur->priority >= p->priority)
{
pre = cur;
cur = cur->next;
}
if (pre == NULL)
{
p->next = cur;
ready = p;
}else {
p->next = pre->next;
pre->next = p;
}
}else if (p->state == Finish) // 插入Finish队列
{
PCB *cur, *pre;
cur = finish, pre = NULL;
while (cur && cur->priority >= p->priority)
{
pre = cur;
cur = cur->next;
}
if (pre == NULL)
{
p->next = cur;
finish = p;
}else {
p->next = pre->next;
pre->next = p;
}
}
}
void Priority(char algo)
{
while (ready != NULL)
{
run = ready;
run->state = Run;
ready = ready->next;
run->next = NULL;
while (run != NULL)
{
run->priority -= 3;
run->cpuTime += 1;
run->needTime -= 1;
showProcess(algo);
if (run->needTime > 0)
{
if (ready == NULL)continue;
if (run->priority < ready->priority)
{
run->state = Ready;
insert(run);
run = NULL;
}
}else {
run->state = Finish;
insert(run);
run = NULL;
}
showProcess(algo);
}
}
}
void createP(char algo)
{
PCB *p;
int i;
int _needTime,_priority,_capacity;
run=NULL;
ready=NULL;
finish=NULL;
for(i=1; i <= N; i++)
{
_needTime = rand()%20+1;
_priority = rand()%10+1;
p=(PCB*)malloc(sizeof(PCB));
p->state = Ready;
p->name = i;
p->needTime = _needTime;
p->priority = _priority;
p->cpuTime = 0;
insert(p);
}
cout<<" Here they come!!!"<<endl;
showProcess(algo);
Priority(algo);
}
int main()
{
char i = 'p';
cout<<"How many Processes: ";
cin>>N;
i = (char)(toupper(i));
createP(i);
}
到此我们就结束了,其实很多功能我都没实现,作为计算机专业的我留下了没技术的眼泪,还有就是懒!
叨叨几句... 4 条评论
来看看
@演员
欢迎光临小店
这个小女巫好萌啊,怎么弄的
@天津网站建设
这是一个 看板娘 功能