多级反馈序列c语言模拟实现
多级反馈队列调度算法:
1、设置多个就绪队列,并给队列赋予不同的优先级数,第一个最高,依次递减。
2、赋予各个队列中进程执行时间片的大小,优先级越高的队列,时间片越小。
3、当一个新进程进入内存后,首先将其放入一个对列末尾,如果在一个时间片
结束时尚未完成,将其转入第二队列末尾。
4、当一个进程从一个对列移至第n个队列后,便在第n个队列中采用时间片轮转执行完。
5、仅当时间片空闲时,才调度第二个队列中的进程。
(1~i-1)空闲时,才调度i,如果处理机正在第i队列中运行,又有新进程进入优先权较高
队列,则新进程抢占处理机,将正在运行的进程放入第i队列队尾,将处理机分给新进程。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node /*进程节点信息*/
{
char name[20]; /*进程的名字*/
int prio; /*进程的优先级*/
int round; /*分配CPU的时间片*/
int cputime; /*CPU执行时间*/
int needtime; /*进程执行所需要的时间*/
char state; /*进程的状态,W——就绪态,R——执行态,F——完成态*/
int count; /*记录执行的次数*/
struct node *next; /*链表指针*/
}PCB;
typedef struct Queue /*多级就绪队列节点信息*/
{
PCB *LinkPCB; /*就绪队列中的进程队列指针*/
int prio; /*本就绪队列的优先级*/
int round; /*本就绪队列所分配的时间片*/
struct Queue *next; /*指向下一个就绪队列的链表指针*/
}ReadyQueue;
PCB *run=NULL,*finish=NULL; /*定义三个队列,就绪队列,执行队列和完成队列*/
ReadyQueue *Head = NULL; /*定义第一个就绪队列*/
int num; /*进程个数*/
int ReadyNum; /*就绪队列个数*/
void Output(); /*进程信息输出函数*/
void InsertFinish(PCB *in); /*将进程插入到完成队列尾部*/
void InsertPrio(ReadyQueue *in); /*创建就绪队列,规定优先数越小,优先级越低*/
void PrioCreate(); /*创建就绪队列输入函数*/
void GetFirst(ReadyQueue *queue); /*取得某一个就绪队列中的队头进程*/
void InsertLast(PCB *in,ReadyQueue *queue); /*将进程插入到就绪队列尾部*/
void ProcessCreate(); /*进程创建函数*/
void RoundRun(ReadyQueue *timechip); /*时间片轮转调度算法*/
void MultiDispatch(); /*多级调度算法,每次执行一个时间片*/
int main(void)
{
PrioCreate(); /*创建就绪队列*/
ProcessCreate();/*创建就绪进程队列*/
MultiDispatch();/*算法开始*/
Output(); /*输出最终的调度序列*/
return 0;
}
void Output() /*进程信息输出函数*/
{
ReadyQueue *print = Head;
PCB *p;
printf("进程名/t优先级/t轮数/tcpu时间/t需要时间/t进程状态/t计数器/n");
while(print)
{
if(print ->LinkPCB != NULL)
{
p=print ->LinkPCB;
while(p)
{
printf("%s/t%d/t%d/t%d/t%d/t/t%c/t/t%d/n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
}
print = print->next;
}
p = finish;
while(p!=NULL)
{
printf("%s/t%d/t%d/t%d/t%d/t/t%c/t/t%d/n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
p = run;
while(p!=NULL)
{
printf("%s/t%d/t%d/t%d/t%d/t/t%c/t/t%d/n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
}
void InsertFinish(PCB *in) /*将进程插入到完成队列尾部*/
{
PCB *fst;
fst = finish;
if(finish == NULL)
{
in->next = finish;
finish = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void InsertPrio(ReadyQueue *in) /*创建就绪队列,规定优先数越小,优先级越低*/
{
ReadyQueue *fst,*nxt;
fst = nxt = Head;
if(Head == NULL) /*如果没有队列,则为第一个元素*/
{
in->next = Head;
Head = in;
}
else /*查到合适的位置进行插入*/
{
if(in ->prio >= fst ->prio) /*比第一个还要大,则插入到队头*/
{
in->next = Head;
Head = in;
}
else
{
while(fst->next != NULL) /*移动指针查找第一个别它小的元素的位置进行插入*/
{
nxt = fst;
fst = fst->next;
}
if(fst ->next == NULL) /*已经搜索到队尾,则其优先级数最小,将其插入到队尾即可*/
{
in ->next = fst ->next;
fst ->next = in;
}
else /*插入到队列中*/
{
nxt = in;
in ->next = fst;
}
}
}
}
void PrioCreate() /*创建就绪队列输入函数*/
{
ReadyQueue *tmp;
int i;
printf("输入就绪队列的个数:/n");
scanf("%d",&ReadyNum);
printf("输入每个就绪队列的CPU时间片:/n");
for(i = 0;i < ReadyNum; i++)
{
if((tmp = (ReadyQueue *)malloc(sizeof(ReadyQueue)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%d",&(tmp->round)); /*输入此就绪队列中给每个进程所分配的CPU时间片*/
tmp ->prio = 50 - tmp->round; /*设置其优先级,时间片越高,其优先级越低*/
tmp ->LinkPCB = NULL; /*初始化其连接的进程队列为空*/
tmp ->next = NULL;
InsertPrio(tmp); /*按照优先级从高到低,建立多个就绪队列*/
}
}
void GetFirst(ReadyQueue *queue) /*取得某一个就绪队列中的队头进程*/
{
run = queue ->LinkPCB;
if(queue ->LinkPCB != NULL)
{
run ->state = 'R';
queue ->LinkPCB = queue ->LinkPCB ->next;
run ->next = NULL;
}
}
void InsertLast(PCB *in,ReadyQueue *queue) /*将进程插入到就绪队列尾部*/
{
PCB *fst;
fst = queue->LinkPCB;
if( queue->LinkPCB == NULL)
{
in->next = queue->LinkPCB;
queue->LinkPCB = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void ProcessCreate() /*进程创建函数*/
{
PCB *tmp;
int i;
printf("输入进程的个数:/n");
scanf("%d",&num);
printf("输入进程名字和进程所需时间:/n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar(); /*吸收回车符号*/
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 50 - tmp->needtime; /*设置其优先级,需要的时间越多,优先级越低*/
tmp ->round = Head ->round;
tmp ->count = 0;
InsertLast(tmp,Head); /*按照优先级从高到低,插入到就绪队列*/
}
}
void RoundRun(ReadyQueue *timechip) /*时间片轮转调度算法*/
{
int flag = 1;
GetFirst(timechip);
while(run != NULL)
{
while(flag)
{
run->count++;
run->cputime++;
run->needtime--;
if(run->needtime == 0) /*进程执行完毕*/
{
run ->state = 'F';
InsertFinish(run);
flag = 0;
}
else if(run->count == timechip ->round)/*时间片用完*/
{
run->state = 'W';
run->count = 0; /*计数器清零,为下次做准备*/
InsertLast(run,timechip);
flag = 0;
}
}
flag = 1;
GetFirst(timechip);
}
}
void MultiDispatch() /*多级调度算法,每次执行一个时间片*/
{
int flag = 1;
int k = 0;
ReadyQueue *point;
point = Head;
GetFirst(point);
while(run != NULL)
{
Output();
if(Head ->LinkPCB!=NULL)
point = Head;
while(flag)
{
run->count++;
run->cputime++;
run->needtime--;
if(run->needtime == 0) /*进程执行完毕*/
{
run ->state = 'F';
InsertFinish(run);
flag = 0;
}
else if(run->count == run->round)/*时间片用完*/
{
run->state = 'W';
run->count = 0; /*计数器清零,为下次做准备*/
if(point ->next!=NULL)
{
run ->round = point->next ->round;/*设置其时间片是下一个就绪队列的时间片*/
InsertLast(run,point->next); /*将进程插入到下一个就绪队列中*/
flag = 0;
}
else
{
RoundRun(point); /*如果为最后一个就绪队列就调用时间片轮转算法*/
break;
}
}
++k;
if(k == 3)
{
ProcessCreate();
}
}
flag = 1;
if(point ->LinkPCB == NULL)/*就绪队列指针下移*/
point =point->next;
if(point ->next ==NULL)
{
RoundRun(point);
break;
}
GetFirst(point);
}
}
多级反馈序列c语言模拟实现的更多相关文章
- 语言模拟ATM自动取款机系统
C语言实验报告 题目名称:C语言模拟ATM自动取款机系统 C语言模拟实现ATM自动取款机功能:输入密码,余额查询,取款,存款,转账,修改密码,退出功能: 代码实现的功能: 账号及密码输入: ...
- 关于c语言模拟c++的多态
关于c++多态,个人认为就是父类调用子类的方法,c++多态的实现主要通过虚函数实现,如果类中含有虚函数,就会出现虚函数表,具体c++多态可以参考<深度探索c++对象模型> c语言模拟多态主 ...
- c语言模拟c++的继承和多态
//C++中的继承与多态 struct A { virtual void fun() //C++中的多态:通过虚函数实现 { cout << "A:fun()" < ...
- 递归实现全排列序列C语言实现
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下鄙人用递归回溯的办法,采用C语言实现了全排列序列,用以某些优化方案的原始方案的给定 #incl ...
- C语言::模拟实现strlen函数
题目要求 编写一个C语言程序模拟实现strlen函数. 算法 strlen函数功能是计算字符串中字符的个数.(除\0外) 而字符串本身就是一个字符数组,只不过末尾以\0结束. 因此,我们只需遍历除\0 ...
- C语言:模拟密码输入显示星号
一个安全的程序在用户输入密码时不应该显示密码本身,而应该回显星号或者点号,例如······或******,这在网页.PC软件.ATM机.POS机上经常看到.但是C语言没有提供类似的功能,控制台上只能原 ...
- Linux套接子(c语言)模拟http请求、应答
有关套接子和http请求报文的博客在CSDN有很多比如,点这里查看,这里我就不再做过多赘述了,下面我们直接实战,模拟http请求. 要求:浏览器访问本地的localhost,在浏览器页面打印出 Hel ...
- c语言模拟实现oc引用计数
#include<stdio.h> #include<stdlib.h> //在c中引入 引用计数机制 // 要解决的问题: 1,指向某块动态内存的指针有几个? // ...
- 1048 图的宽度优先遍历序列 c语言
描述 图(graph)是数据结构 G=(V,E),其中V是G中结点的有限非空集合,结点的偶对称为边(edge):E是G中边的有限集合.设V={0,1,2,……,n-1},图中的结点又称为顶点(vert ...
随机推荐
- 深入了解Node模块原理
深入了解Node模块原理 当我们编写JavaScript代码时,我们可以申明全局变量: var s = 'global'; 在浏览器中,大量使用全局变量可不好.如果你在a.js中使用了全局变量s,那么 ...
- 中石油大学统考(大学英语B)押题笔记
二. 词汇与结构 1. I will.意为“我会的”,固定搭配. 2. get tired of 是词组“对…厌烦了”的意思. 3. — ________ is your girl friend li ...
- [转] linux下 /etc/profile、~/.bash_profile ~/.profile的执行过程
分类: linux 2015-03-13 16:24 1572人阅读 评论(0) 收藏 举报linuxprofile关于登录linux时,/etc/profile.~/.bash_profile等几个 ...
- Angular2 Pipe
AngularJs 1.x 中使用filters来帮助我们转换templates中的输出,但在Angular2中使用的是pipes,以下展示Angular 1.x and Angular 2中filt ...
- 面向对象的JavaScript --- 多态
面向对象的JavaScript --- 多态 多态 "多态"一词源于希腊文 polymorphism,拆开来看是poly(复数)+ morph(形态)+ism,从字面上我们可以理解 ...
- gluoncv voc_detection
https://github.com/zhreshold/gluon-cv/commit/73b3986aaa2e0d0e6f3f428c12072e3a9d29905e gluoncv可能版本还没更 ...
- 【node.js】回调函数
学习链接:http://www.runoob.com/nodejs/nodejs-callback.html Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调 ...
- 用ReentrantLock和Condition实现线程间通信
在Java多线程中,除了使用synchronize关键字来实现线程之间的同步互斥,还可以使用JDK1.5中新增的RetrantLock类来实现同样的效果.RetrantLock类的扩展功能也更加强大, ...
- eclipse主题皮肤设置
这里先声明,下面的方式适合最新版本的Eclipse Luna,旧的版本可以下载我提供的这个插件,并将其放在eclipse目录下的plugins目录下即可. 插件下载地址:http://download ...
- HttpURLConnection与HttpClient学习
转载HttpURLConnection与HttpClient浅析 一.HttpURLConnection的使用 import org.slf4j.Logger; import org.slf4j.Lo ...