① 基于优先数的时间片轮转调度算法调度处理器
没有完全符合的,但是差不多的,你自己改改吧!
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
typedef struct PCBA
{
char name[30];
int round;
int prio;
int cputime;
int needtime;
char state;
struct PCBA *next;
} PCB,*CurrentRunPCB,*LinkQueue;
CurrentRunPCB SelMosHeiPrio(LinkQueue *Q);//从Q链表中选择一个优先级最大的进程
void InitQueue(LinkQueue *Q);//初始化链表Q,如果链表没有头指针,可以省略;
void InsertQueue(LinkQueue * Q,PCB* run);
{
}//将Run结点插入链表Q中,表示就绪队列里多了一个进程
void Priosch(LinkQueue Wait_Queue,LinkQueue Finish_Queue)//优先法运行,从就绪队列Wait_Queue中选择进程执行,(选择队列中优先级最高的进程),执行结束的进程放入完成队列中;
{ while (WaitQueue!=NULL)
{
CurrentRunPCB run=SelMosHeiPrio(&WaitQueue); //run为就绪队列中优先级最高的进程
run->cputime+=2;//时间片为2
run->needtime-=2;
run->prio=50-run->needtime;//动态优先级,但是优先级随着运行时间的增加,优先级增加,所以谁优先级高会一直占用CPU
run->state='r';
if(run->needtime>0)
{ run->state='w';
InsertQueue(&WaitQueue,run) ;
}
else
{cout<<"当前运行进称为:"<<run->name<<"运行结束后就绪队列情况:"<<'\n';
run->state='F';
run->needtime=0;
InsertQueue(&FinishQueue,run) ;
Print(&WaitQueue);
}
//当然可以采用不看进程运行过程,直接将优先级高的运行完,插入完成队列
/*
CurrentRunPCB run=SelMosHeiPrio(&WaitQueue);
cout<<"当前运行进称为:"<<run->name<<"运行结束后就绪队列情况:"<<'\n';
Print(&WaitQueue);
run->cputime=run->needtime;
run->needtime=0;
run->prio=50;//动态优先级,但是优先级随着运行时间的增加,优先级增加,所以谁优先级高会一直占用CPU
run->state='F';
InsertQueue(&FinishQueue,run) ;*/
}
void Print(LinkQueue*Q)//将队列的元素显示输出,输出包含进程名,进程CPUTIME,NEEDTIME,状态,优先级
{LinkQueue p=*Q;
cout<<"name cputime needtime state prio'"<<'\n';
while (p!=NULL)
{ cout<<p->name<<'\t'<<p->cputime<<'\t'<<p->needtime<<'\t'<<p->state<<'\t'<<p->prio<<'\n';
p=p->next;
}
}
CurrentRunPCB DeQueue(LinkQueue*Q)//从就绪队列中取出一个进程
{LinkQueue p=*Q;
*Q=(*Q)->next;
p->next=NULL;
return p;
}
void Roundsch(LinkQueue WaitQueue,LinkQueue FinishQueue)//轮转法运行,从就绪队列Wait_Queue中选择进程执行一个时间片,执行结束的进程放入完成队列中;若一个时间片未能执行完的进程再插入到就绪队列
{ while (WaitQueue!=NULL)
{
CurrentRunPCB run=DeQueue(&WaitQueue);
cout<<"当前运行进称为:"<<run->name<<"运行结束后就绪队列情况:"<<'\n';
run->cputime+=2;//时间片为2
run->needtime-=2;
run->prio=50- run->cputime;
run->state='r';
if(run->needtime>0)
{InsertQueue(&WaitQueue,run) ;
run->state='w';
}
else
{run->state='F';
run->needtime=0;
InsertQueue(&FinishQueue,run) ;
}
Print(&WaitQueue);
}
cout<<"完成队列情况:"<<'\n';
Print(&FinishQueue);
}
void SetAllpro(LinkQueue*Q)//设置优先级函数50-p->needtime
{int max=0;
LinkQueue p,t;
t=NULL;
p=*Q;
if (p!=NULL)
{
max=p->prio;
p=p->next;
while(p)
{
if (max<p->prio) max=p->prio;
p=p->next;
}
p=*Q;
t=*Q;
if (t==p&&max==t->prio)
{*Q=(*Q)->next;
t->next=NULL;
return t;}
else{
t=t->next;
while(t)
{
if (max==t->prio)
{
p->next=t->next;
t->next=NULL;
return t;
}
else{p=t;
t=t->next;
}
}
}
}
return t;
}
void main()
{
PCBA *pcb0,*pcb1,*pcb2,*pcb3,*pcb4; //five processes 五个进程
LinkQueue Wait_Queue,Finish_Queue; //两个队列 等待和完成
Wait_Queue=NULL; //给队列赋初值,如果带有头指针的链表,可以用函数;
Finish_Queue=NULL;
//InitQueue(&Wait_Queue);
//InitQueue(&Finish_Queue);
char ch;
//给各个进程设置初值
pcb0= new PCBA();
pcb1= new PCBA();
pcb2= new PCBA();
pcb3= new PCBA();
pcb4= new PCBA();
//example
strcpy(pcb0->name,"process1");
pcb0->round=2;
pcb0->prio=0;
pcb0->cputime=0;
pcb0->needtime=5;
pcb0->state='W';
pcb0->next=NULL;
strcpy(pcb1->name,"process2");
pcb1->round=2;
pcb1->prio=0;
pcb1->cputime=0;
pcb1->needtime=7;
pcb1->state='W';
pcb1->next=NULL;
strcpy(pcb2->name,"process3");
pcb2->round=2;
pcb2->prio=0;
pcb2->cputime=0;
pcb2->needtime=3;
pcb2->state='W';
pcb2->next=NULL;
strcpy(pcb3->name,"process4");
pcb3->round=2;
pcb3->prio=0;
pcb3->cputime=0;
pcb3->needtime=11;
pcb3->state='W';
pcb3->next=NULL;
strcpy(pcb4->name,"process5");
pcb4->round=2;
pcb4->prio=0;
pcb4->cputime=0;
pcb4->needtime=8;
pcb4->state='W';
pcb4->next=NULL;
//将各个进程插入就绪队列中
InsertQueue(&Wait_Queue,pcb0);
InsertQueue(&Wait_Queue,pcb1);
InsertQueue(&Wait_Queue,pcb2);
InsertQueue(&Wait_Queue,pcb3);
InsertQueue(&Wait_Queue,pcb4);
//利用此算法实现Wait_Queue中prio=50-needtime;
SetAllpro(&Wait_Queue);
cout<<"请输入选择的调度算法(1 or 2 or anykey exit!):"<<endl;
cin>>ch;
switch(ch)
{
case '1':
Print(&Wait_Queue);
Roundsch(Wait_Queue,Finish_Queue);
break;
case '2':
Print(&Wait_Queue);
Priosch(Wait_Queue,Finish_Queue);
break;
default :
cout<<"你选择了退出!"<<endl;
system("pause");
return;
}
return;
}
② 设计一个按响应比高者优先调度算法实现进程调度的程序。
只帮你写点开头,后面你应该会的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
typedef struct node
{
char name[10];
int prio;
int round;
int cputime;
int needtime;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run,; //队列指针
int N; //进程数
void zhunbei()
{
run=ready; //就绪队列头指针赋值给运行头指针
run->state='G'; //进程状态变为运行态]
ready=ready->next; //就绪队列头指针后移到下一进程
}
//输出标题函数
void output1(char a)
{
if(toupper(a)=='P') //优先级法
cout<<" "<<endl;
cout<<"进程名 占用CPU时间 已运行时间 还要的时间 轮转时间片 状态"<<endl;
}
③ 处理机调度模拟程序:选择一个调度算法,实现处理机调度.
处理机调度主要有:先到先服务、短作业优先、优先权调度、轮转法调度、多级队列调度、多级反馈队列。
在实现时:
1、创建三个状态(队列):运行(队长为1)、就绪、阻塞。
2、创建一个数据结构代表进程,里面有一些进程特征标记(根据我上面说的调度算法)。
3、写算法,读进程的数据结构进行入队、出队。
④ 设计一个按优先数调度算法实现处理器调度的程序。
#include "stdio.h"
#include "malloc.h"
#include "string.h"
typedef struct node{
int time;
int name;
char statement;
int num;
struct node *next;
}node,*L;
void createL(L &l,int n){
l=(L)malloc(sizeof(node));
if(!l)
printf("error!");
else
l->next=NULL;
L p,q;
q=l;
for(int i=0;i<n;i++){
p=(L)malloc(sizeof(node));
printf("请输入进程的名字name:\n");
scanf("%d",&p->name);
getchar();
printf("请输入该进程的运行时间time:\n");
scanf("%d",&p->time);
printf("请输入其优先级数num:\n");
scanf("%d",&p->num);
getchar();
printf("请输入其状态:\n");
p->statement=getchar();
p->next=q->next;
q->next=p;
q=p;
getchar();
}
}
void traL(L &l){
L p;
p=l->next;
printf("进程名\t运行时间\t优先数\t状态\n");
while(p){
printf(" %d\t%5d\t%11d\t %c",p->name,p->time,p->num,p->statement);
printf("\n");
p=p->next;
}
}
void Sort(L &l)
{
L tail=NULL;
while(tail!= l->next)
{
L pre = l;
L cur = pre->next;
while(cur != tail && cur->next != tail)
{
if( cur->num < cur->next->num )
{
pre->next = cur->next;
cur->next = cur->next->next;
pre->next->next = cur;
}
pre = pre->next;
cur = pre->next;
}
tail = cur;
}
}
void run(L &l){
Sort(l);
L p,r,q;
q=l;
if(l->next!=NULL)
p=l->next;
int j=0;
printf("第k次运行\t进程名\t运行时间\t优先数\t状态\n");
while(p!=NULL){
j++;
printf("%5d\t %d\t%5d\t%11d\t %c",j,p->name,p->time,p->num,p->statement);
printf("\n");
p->num--;
p->time--;
if(p->time==0){
p->statement='E';
r=p;
if(p->next!=NULL)
{
l->next=p->next;
free(r);
p=l->next;
}
else
break;
}
else{
Sort(l);
if(l->next!=NULL)
p=l->next;
else
break;
}
}
}
void main(){
L l;
int n;
printf("请输入进程的数目n:\n");
scanf("%d",&n);
createL(l,n);
traL(l);
run(l);
}
//这是运行过的,应该符合你的要求!
⑤ 处理机调度模拟程序:选择一个调度算法,实现处理机调度。
七、源程序:
#include <stdio.h>
int m1; //共享变量
int m2; //共享变量
struct{
int id; //进程标号
int waiter1; //指针,用于标识等待队列的下一个进程
int priority; //进程优先级
char status; //进程状态
}pcb[4];
struct{
int value; //信号量的值
int waiter2; //指针,用于标识等待此信号量队列的第一个进程
}sem[3];
char stack[11][4]; //现场保护堆栈
int i; //cpu中的通用寄存器
int ep; //当前运行进程指针
char addr; //程序运行时的地址
void init(); //初始化
int find(); //找出就绪进程
int w2(); //
int process1(); //进程1
int process2(); //进程2
int process3(); //进程3
int p(int,int ,char); //P原语
int v(int,int ,char); //V原语
main()
{
init();
printf("系统程序开始执行\n");
for(;;){
if(find()!=0) //找出就绪进程
w2(); //进程调度
else break;//退出程序
}
printf("系统程序结束\n");
}
void init() //初始化进程
{
int j,k;
pcb[0].status='w'; //进程状态设置为等待
pcb[0].priority=4; //进程的优先级别
for(j=1;j<=3;j++){
pcb[j].id=j; //进程编号
pcb[j].status='r'; //进程状态设置为就绪
pcb[j].waiter1=0; //进程指针初始化为0
pcb[j].priority=j; //设置进程优先级
}
for(j=1;j<=2;j++){
sem[j].value=1; //信号量赋值为1
sem[j].waiter2=0; //等待信号量队列的初始化
}
i=0; //CPU通用寄存器初始化
ep=0; //
addr='0'; //程序运行地址
m1=0; //共享变量初始化
m2=0; //共享变量初始化
for(j=1;j<=10;j++){
for(k=1;k<=3;k++)
stack[j][k]='0'; //现场保护堆栈初始化
}
}
int find(){ //查找初始化变量
int j;
for(j=1;j<=3;j++)
if(pcb[j].status=='r') return(j); //如果pcb队列中有就绪进程,返回进程编号
return(0);
}
int w2(){ //进程调度程序
int pd;
pd=find(); //找出就绪进程编号
if(pd==0) return(0); //如果没有找到就绪进程,退出程序
else if(ep==0){ //如果当前运行进程是否为0
pcb[pd].status='e'; //直接将当前进程设置为运行状态
ep=pd; //当前运行进程设置为pd
printf("进程%d正在执行\n",ep);
}
else
if(pcb[pd].priority<pcb[ep].priority)//如果当前进程比待调入进程优先级进行比较
{//调入进程优先级别高
pcb[ep].status='r'; //把CPU运行进程执行状态设置为就绪
printf("读取进程%d\n",pcb[pd].id);
pcb[pd].status='e'; //将待调入进程执行状态设置为执行
ep=pd; //将当前运行进程指针为待调入进程
}
printf("运行进程%d\n",ep);
i=stack[1][ep]; //恢复进程的通用寄存器中的值和运行地址。
addr=stack[2][ep];
switch(ep){ //根据当前运行的进程选择运行
case 1:process1();
break;
case 2:process2();
break;
case 3:process3();
break;
default:printf("当前进程出现错误%d\n",ep);
break;
}
}
int process1(){//进程1
if(addr=='m') goto m; //如果当前运行地址是m,跳到m运行
i=1;
a:
printf("进程1在信号量sem[1]上调用P操作\n");
if(p(1,1,'m')==0) return(0); //进行p操作,m表示程序执行到m
else goto m;
m:
printf("打印进程1...m1=%d\n",m1); //进程1的操作,打印,寄存器i+5
printf("打印进程1...i=%d\n",i);
i+=5;
goto a; //跳回a执行
}
int process2(){//进程分为3部分
if(addr=='m') goto m;
if(addr=='n') goto n;
i=1;
a:
printf("进程2在信号量sem[2]上调用P操作\n");
if(p(2,2,'m')==0) return(0);
m:
m1=2*m2;
printf("进程2在信号量sem[1]上调用V操作m1=%d\n",m1);
if(v(1,2,'n')==0) return(0);
else{
n:
printf("打印进程2...i=%d\n",i);
i+=10;
goto a;
}
}
int process3(){
if(addr=='m') goto m;
if(addr=='n') goto n;
i=1;
a:
if(i>4){
printf("进程3在信号量sem[2]上调用P操作\n");
if(p(2,3,'n')==0) return(0);
}
n:
m2=i;
printf("进程3在sem[2]信号量上调用V操作m=%d\n",m2);
if(v(2,3,'m')==0) return(0);
else{
m:
i+=1;
goto a;
}
}
int p(int se,int p,char ad){
int w;
sem[se].value--; //信号量减1
if(sem[se].value==0) return(1); //如果信号量=0,返回1,说明阻塞
printf("阻塞当前进程%d\n",p);
pcb[p].status='w'; //改变进程状态
ep=0; //运行进程为空
pcb[p].waiter1=0; //设为尾末
w=sem[se].waiter2; //找出等待队列的队尾
if(w==0) sem[se].waiter2=p; //插入等待队列
else{
while(pcb[w].waiter1!=0) w=pcb[w].waiter1;
pcb[w].waiter1=p;
}
stack[1][p]=i; //保存现场
stack[2][p]=ad;
return(0);
}
int v(int se,int p,char ad){
int w;
sem[se].value++; //信号量加1
if(sem[se].value>0) return(1); //信号量>0,无等待进程
w=sem[se].waiter2; //返回第一个等待进程
sem[se].waiter2=pcb[w].waiter1; //调整位置
pcb[w].status='r'; //进程改变状态
printf("唤醒进程%d\n",w);
stack[1][p]=i; //进程状态进栈
stack[2][p]=ad;
return(0);
}
⑥ 急~~超急~~请于2011·1·5十点前告诉我 操作系统课程设计 设计一个按优先数调度算法实现处理机调度的程序
这个怎么来告诉你,,课程设计
曾经搞过UCOS2系统,也是任务调度,时间轮换制的,
可以参考一下,毕竟是开源的,所以资料,改进的文章很多
⑦ 如何用C语言编写:设计一个时间片轮转调度算法实现处理机调度的程序
实验三 进程调度
一、实验目的
在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。当就绪进程个数大于处理机数时,就必须依照某种策略来决定那些进程优先占用处理机。本实验模拟在单处理机情况下的处理机调度,帮助学生加深了解处理机调度的工作。
二、实验内容
设计一个时间片轮转调度算法实现处理机调度的程序。
三、实验指导
1.实验中使用的数据结构:
1)PCB进程控制块
其中包括参数①进程名name;②要求运行时间runtime;③优先数prior;④状态state;⑤已运行时间runedtime。
2)为简单起见,只设运行队列,就绪链表两种数据结构,进程的调度在这两个队列中切换,如图3.1所示。
图3.1PCB链表
2.运行结果,包括各个进程的运行顺序,每次占用处理机的运行时间
3.每个进程运行时间随机产生,为1~20之间的整数。
4.时间片的大小由实验者自己定义,可为3或5。
四、实验要求
1.在上机前写出全部源程序;
2.能在机器上正确运行程序。
五、程序清单
六、运行结果
七、调试分析及实验心得
我的回答和这位老兄的差不多
⑧ 设计一个按优先数调度算法实现处理器调度的程序。 高手帮忙!!
#include <stdio.h>
#include <stdlib.h> //提供atoi()函数
#include <conio.c> //提供clrscr()函数
#define M 10 //字符串大小常量
#define N 3 //进程数常量
#define SLOT 2
typedef struct node{
char name[M];
int prio; //优先级
int round; //时间片长度
int cputime; //已经使用的cpu时间
int needtime;//需要多少cpu时间
int count; //计数器
char state; //进程的当前状态
struct node *next; //指向下一个进程
}PCB;
PCB *finish,*ready,*tail,*run;
void ShowHead(char *s1,char *s2);
int Menu_Select();
void Creat(int select);
void InsertPriority(PCB *q);
void InsertSlot(PCB *q);
void PrintOneProcess(int select,PCB *q);
void PrintAllProcess(int select);
void FirstIn();
void FIFODo(int select);
void PriorityDo(int select);
void SlotDo(int select);
void Quit();
main()
{
while(1)
{
clrscr();
switch(Menu_Select())
{
case 1:
Creat(1);
FIFODo(1);
printf("\n\n\t\t按回车键返回菜单...");
getchar();
getchar();
break;
case 2:
Creat(2);
PriorityDo(2);
printf("\n\n\t\t按回车键返回菜单...");
getchar();
getchar();
break;
case 3:
Creat(3);
SlotDo(3);
printf("\n\n\t\t按回车键返回菜单...");
getchar();
getchar();
break;
case 0:
Quit();
break;
}
}
}
/*打印每个界面的开头显示*/
void ShowHead(char *s1,char *s2)
{
printf("\t ==================%s========================\n\n",s1);
printf("\t\t\t\t%s\n\n",s2);
printf("\t ========================================================\n\n");
}
/*主菜单*/
int Menu_Select()
{
int choose;
char s[2];
clrscr();
printf("====================进程调度算法模拟程序=====================\n\n");
printf("\t\t 1.先进先出调度策略\n");
printf("\t\t 2.优先数调度策略\n");
printf("\t\t 3.时间片轮转调度策略\n");
printf("\t\t 0.退出系统\n\n");
printf("=============================================================\n\n");
do
{
printf("\n请输入你的选择(0-3):");
scanf("%s",s);
choose=atoi(s);
}while(choose<0 || choose>3);
return choose;
}
/*创建调度算法的链表*/
void Creat(int select)
{
PCB *p,*q; //q为就绪队列的最后一个结点
int i,time,rounds;
char na[M];
ready=NULL;
finish=NULL;
run=NULL;
if(select==1) //先进先出调度创建
{
printf("\nEnter name and time of process:\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='W';
p->next=NULL;
if(ready!=NULL) //就绪队列不为空
{
q->next=p;
q=p;
}
else //就绪队列为空
{
p->next=ready;
ready=p;
q=ready;
}
}
clrscr();
ShowHead("先进先出调度策略","FIFO dispatching algorithm ");
printf("\t\t name cputime needtime state\n");
PrintAllProcess(select); //打印出初始状态的信息
}
else if(select==2) //优先数调度创建
{
printf("\nEnter name and time of process:\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='W';
p->prio=50-time;
if(ready!=NULL) //就绪队列不为空的时候
InsertPriority(p);
else //就绪队列为空
{
p->next=ready;
ready=p;
}
}//end of for()
clrscr();
ShowHead("优先级调度策略","Priority dispatching algorithm ");
printf("\t\t name cputime needtime prio state\n");
PrintAllProcess(select); //打印出初始状态的信息
}//end of else if()
else if(select==3) //时间片轮转调度创建
{
printf("\nEnter name and the time of process:\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0; //计数器
p->round=SLOT;
p->state='W';
if(ready!=NULL)
InsertSlot(p);
else
{
p->next=ready;
ready=p;
}
}
clrscr();
ShowHead("时间片轮转调度策略","Time slot dispatching algorithm ");
printf("\n\t\t name cputime needtime count slot state\n");
PrintAllProcess(select); //打印出初始状态的信息
}
run=ready; //从就绪队列取一个进程,使其运行,同时就绪队列的头指针后移
run->state='R';
ready=ready->next;
}
/*优先调度:把进程插入到合适的位置,就绪队列按优先级由高到低的顺序排列*/
void InsertPriority(PCB *q)
{
PCB *pre,*p;
int flag=1;
pre=NULL;
p=ready;
while(p!=NULL && flag)
{
if(p->prio>q->prio)
{
pre=p;
p=p->next;
}
else
flag=0;
}
if(pre==NULL)
{
q->next=ready;
ready=q;
}
else
{
pre->next=q;
q->next=p;
}
}
/*时间片轮转:把结点插入到就绪队列的末尾*/
void InsertSlot(PCB *q)
{
PCB *pre,*p;
pre=NULL;
p=ready;
while(p!=NULL)
{
pre=p;
p=p->next;
}
pre->next=q;
q->next=NULL; /*由于插入到队列的末尾,所以必须要使其的下一个结点为空值*/
}
/*打印一个信息*/
void PrintOneProcess(int select,PCB *q)
{
if(select==1)
printf("\t\t %-10s%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->state);
else if(select==2)
printf("\t\t %-10s%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->prio,q->state);
else if(select==3)
printf("\t\t %-10s%-10d%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->count,q->round,q->state);
}
/*将所有的进程打印出来,按运行,就绪,完成顺序打印*/
void PrintAllProcess(int select)
{
PCB *p;
if(run!=NULL)
PrintOneProcess(select,run);
p=ready;
while(p!=NULL)
{
PrintOneProcess(select,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{
PrintOneProcess(select,p);
p=p->next;
}
}
/*把就绪队列的第一个进程调入运行*/
void FirstIn()
{
run=ready;
ready=ready->next;
run->state='R';
}
/*先进先出调度策略*/
void FIFODo(int select)
{
while(run!=NULL)
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
if(run->needtime==0) //进程执行结束
{
printf("\n\t\t name cputime needtime state\n");
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
FirstIn();
PrintAllProcess(1);
}
}
}
/*优先级算法*/
void PriorityDo(int select)
{
while(run!=NULL)
{
printf("\n\t\t name cputime needtime prio state\n");
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3;
if(run->needtime==0)
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
FirstIn();
}
else if((ready!=NULL) && (run->prio < ready->prio))
{
run->state='W';
InsertPriority(run);
FirstIn();
}
PrintAllProcess(select);
}
}
/*时间片轮转算法*/
void SlotDo(int select)
{
while(run!=NULL)
{
printf("\n\t\t name cputime needtime count slot state\n");
run->count=run->count+1;
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
if(run->needtime==0) /*运行完成时*/
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
FirstIn(); //就绪队列不为空,将就绪队列的第一个进程投入运行
}
else if(run->count==run->round) /*时间片已经到了,还未运行完成*/
{
run->count=0; //时间片
if(ready!=NULL)
{
run->state='W';
InsertSlot(run);
FirstIn();
}
}
PrintAllProcess(select); //打印本次的所有记录
}
}
void Quit()
{
char ch;
clrscr();
gotoxy(10,5);
printf("==========================================================\n\n");
printf(" Thank you for you using\n\n");
gotoxy(10,9);
printf("==========================================================\n\n");
gotoxy(13,15);
getchar();
printf("\n\t\tDo you really want to quit(y/Y or n/N):");
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
printf("\n\t\t按任意键退出系统...");
getchar();
exit(0);
}
}
⑨ 进程调度方案设计 实现一个基本动态优先级的调度算法
前两天做操作系统作业的时候学习了一下几种进程调度算法,在思考和讨论后,有了一些自己的想法,现在就写出来,跟大家讨论下。,或者说只有有限的CPU资源,当系统中有多个进程处于就绪状态,要竞争CPU资源时,操作系统就要负责完成如何分配资源的任务。在操作系统中,由调度程序来完成这一选择分配的工作,调度程序所使用的算法即是调度算法。调度算法需要考虑的指标主要有尽量保证CPU资源分配的公平性;按照一定策略强制执行算法调度;平衡整个计算机系统,尽量保持各个部分都处于忙碌状态。而根据系统各自不同的特点和要求,调度算法又有一些侧重点和目标不同,因此,算法按照系统差异主要分为三大类:批处理系统中的调度算法,代表调度算法有:先来先服务、最短作业优先、最短剩余时间优先。交互式系统中的调度算法,代表调度算法有:轮转调度、优先级调度、多级队列、最短进程优先、保证调度、彩票调度、公平分享调度。实时系统中的调度算法,代表调度算法有:速率单调调度、最早最终时限优先调度。下面就上述提到的调度算法中挑出几个进行重点分析:保证调度保证调度是指利用算法向用户做出明确的性能保证,然后尽力按照此保证实现CPU的资源分配。利用这种算法,就是定一个进程占用CPU的时间的标准,然后按照这个标准去比较实际占用CPU的时间,调度进程每次使离此标准最远的进程得到资源,不断满足离所保证的标准最远的进程,从而平衡资源分配满足这个标准的要求。保证调度算法的优点是:能很好的保证进程公平的CPU份额,当系统的特点是:进程的优先级没有太大悬殊,所制定的保证标准差异不大,各个进程对CPU的要求较为接近时,比如说系统要求n个进程中的每个进程都只占用1/n的CPU资源,利用保证调度可以很容易的实现稳定的CPU分配要求。但缺点是,这种情况太过理想,当系统的各个进程对CPU要求的紧急程度不同,所制定的保证较为复杂的时候,这个算法实现起来比较困难。彩票调度彩票调度这种算法的大意是指向进程提供各种系统资源如CPU资源的彩票,当系统需要做出调度决策时,随机抽出一张彩票,由此彩票的拥有者获得资源。在彩票调度系统中,如果有一个新的进程出现并得到一些彩票,那么在下一次的抽奖中,该进程会有同它持有彩票数量成正比例的机会赢得奖励。进程持有的彩票数量越多,则被抽中的可能性就越大。调度程序可以通过控制进程的彩票持有数量来进行调度。彩票调度有很多优点:首先,它很灵活,系统增加分给某个进程的彩票数量,就会大大增加它占用资源的可能性,可以说,彩票调度的反应是迅速的,而快速响应需求正是交互式系统的一个重要要求。其次,彩票调度算法中,进程可以交换彩票,这个特点可以更好的保证系统的平衡性,使其各个部分都尽可能的处于忙碌状态。而且利用彩票调度还可以解决许多别的算法很难解决的问题,例如可以根据特定的需要大致成比例的划分CPU的使用。速率单调调度速率单调调度算法是一种可适用于可抢占的周期性进程的经典静态实时调度算法。当实时系统中的进程满足:每个周期性进程必须在其周期内完成,且进程之间没有相互依赖的关系,每个进程在一次突发中需要相同的CPU时间量,非周期的进程都没有最终时限四个条件时,并且为了建模方便,我们假设进程抢占即刻发生没有系统开销,可以考虑利用速率单调算法。速率单调调度算法是将进程的速率(按照进程周期所算出的每秒响应的次数)赋为优先级,则保证了优先级与进程速率成线性关系,这即是我们所说的速率单调。调度程序每次运行优先级最高的,只要优先级较高的程序需要运行,则立即抢占优先级低的进程,而优先级较低的进程必须等所有优先级高于它的进程结束后才能运行。速率单调调度算法可以保证系统中最关键的任务总是得到调度,但是缺点是其作为一种静态算法,灵活性不够好,当进程数变多,系统调度变得复杂时,可能不能较好的保证进程在周期内运行。最早最终时限优先调度最早最终时限优先调度算法是一个动态算法,不要求进程是周期性的,只要一个进程需要CPU时间,它就宣布它的到来时间和最终时限。调度程序维持一个可运行的进程列表,按最终时限排序,每次调度一个最终时限最早的进程得到CPU 。当新进程就绪时,系统检查其最终时限是否在当前运行的进程结束之前,如果是,则抢占当前进程。由于是动态算法,最早最终优先调度的优点就是灵活,当进程数不超过负载时,资源分配更优,但也同样由于它的动态属性,进程的优先级都是在不断变化中的,所以也没有哪个进程是一定可以保证满足调度的,当进程数超过负载时,资源分配合理度会急速下降,所以不太稳定。