⑴ 作业调度算法的优先级法
优先级算法(Priority Scheling)是多级队列算法的改进,平衡各进程对响应时间的要求。适用于作业调度和进程调度,可分成抢先式和非抢先式。 作业调度中的静态优先级大多按以下原则确定:
由用户自己根据作业的紧急程度输入一个适当的优先级。
由系统或操作员根据作业类型指定优先级。
系统根据作业要求资源情况确定优先级。
进程的静态优先级的确定原则:
按进程的类型给予不同的优先级。
将作业的情态优先级作为它所属进程的优先级。 进程的动态优先级一般根据以下原则确定:
根据进程占用有CPU时间的长短来决定。
根据就绪进程等待CPU的时间长短来决定。
⑵ 基于优先数的时间片轮转调度算法调度处理器
没有完全符合的,但是差不多的,你自己改改吧!
#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;
}
⑶ 作业调度算法的选择原则有哪几个
批处理作业的调度算法主要有以下几种:
①先来先服务算法。原则上按照作业进入输入井的次序调度,如果作业的资源得不到满足,将会推迟调度,它的资源得到满足的时候会优先被调度进来。
优点:具有一定的公平性。
缺点:系统的吞吐率低,平均周转时间长,有大作业到来的时,许多小作业推迟调度。
②计算时间短的作业优先.优先调度计算时间短的作业进行调度,资源不满足的情况下推迟调度。在这种调度算法下,要求用户要对作业的计算时间预先有一个估计,调度以此为依据。
优点:由于被选中的作业计算时间,所以不能尽快地完成并退出系统,降低了作业的平均等待时间,提高了系统的吞吐率。
缺点:大作业会不满意,而且极限情况下使得某些大作业始终得不到调度。
③响应比高者优先算法。该算法考虑了计算时间等待时间,既考虑了计算时间短的作业优先,又考虑了大作业长期等待的问题。所谓响应比是按照以下公式来定义的:
响应比R=等待时间/计算时间
这里的计算时间是估计的作业计算时间,从公式看,计算时间越短,响应比越高;而另一方面,大作业等待时间越长,响应比也会越大。一个作业完成以后,需要重新计算一下在输入井中的各个作业的响应比,最高的将优先调度。
④优先数调度算法。为每一个作业指定一个优先数,优先数高的作业先被调度。对于优先数相等的作业采用先来先服务的策略。优先数的制定原则是:作业的缓急程序,估计的计算时间,作业的等待时间,资源申请情况等因素综合考虑。
⑤均衡调度算法。使用不同资源的进程同时执行,减少作业等待同类设备而耗费的时间,加快作业的执行。
⑷ 动态高优先权优先调度算法
动态高优先权优先调度算法:
动态优先权是指,在创建进程时所赋予的优先权,是可以随进程的推进或随其等待时间的增加而改变的,以便获得更好的调度性能。例如,我们可以规定,在就绪队列中的进程,随其等待时间的增长,其优先权以速率a提高。若所有的进程都具有相同的优先权初值,则显然是最先进入就绪队列的进程,将因其动态优先权变得最高而优先获得处理机,此即FCFS算法。若所有的就绪进程具有各不相同的优先权初值,那么,对于优先权初值低的进程,在等待了足够的时间后,其优先权便可能升为最高,从而可以获得处理机。当采用抢占式优先权调度算法时,如果再规定当前进程的优先权以速率b下降,则可防止一个长作业长期地垄断处理机。
算法代码模拟实现:
#include<stdio.h>
#include<stdlib.h>
#defineN6
//待插入就绪队列的进程数据
intid[N]={0,1,
2,3,4,
5};
intpriority[N]={9,38,17,
2,7,18};
intcpuTime[N]={0,
0,0,0,
0,0};
intallTime[N]={3,
2,3,6,
1,3};
//********************************
//
//模拟进程/PCB数据结构
//
//********************************
//
枚举进程的状态:就绪、执行、阻塞、完成
enumSTATE{Ready,Run,Block,Finish
};
//建立PCB结构体
structPCB{
intid;//标志数
intpriority;//优先数
intcpuTime;//
已占CPU时间
intallTime;//
还需占CPU时间
intblockTime;//已被阻塞的时间
STATEstate;//
进程状态
PCB*pre;//
PCB的前指针
PCB*nxt;//
PCB的后指针
};
//********************************
//
//模拟进程队列
//
//********************************
//进程入列
voidqueQush(PCB*process,PCB
*queHead)
{
process->pre=NULL;
process->nxt=
queHead->nxt;
if(queHead->nxt!=NULL){
//非第一个入列
queHead->nxt->pre=
process;
}
queHead->nxt=process;
}
//进程出列
voidquePop(PCB*process,PCB
*queHead)
{
if(process->pre!=NULL){
//不是头节点
process->pre->nxt=
process->nxt;
}
else{
queHead->nxt=
process->nxt;
}
if(process->nxt!=NULL){
//不是尾节点
process->nxt->pre=
process->pre;
}
//
清空进程指针
process->pre=process->nxt=
NULL;
}
//查看队列里进程的信息
voidqueWalk(PCB*queHead)
{
PCB*pro=queHead->nxt;
if(pro==NULL){
printf("(无进程) ");
return;
}
while(pro!=NULL)
{
printf("id:%d,
pri:%d,alltime:%d ",
pro->id,
pro->priority,
pro->allTime);
pro=
pro->nxt;
}
}
//********************************
//
//模拟就绪队列
//
//********************************
intreadyQueNum;//就绪队列的进程数量
PCBreadyQueHead;//
就绪队列的头部
PCB*readyMaxProcess;//就绪队列中优先级最高的进程
//进程插入到就绪队列
voidreadyQueQush(PCB
*process)
{
readyQueNum++;
process->state=Ready;
queQush(process,&readyQueHead);
}
//优先级最高的进程出列
PCB*readyQuePop()
{
readyQueNum--;
quePop(readyMaxProcess,
&readyQueHead);
returnreadyMaxProcess;
}
//每个时间片,更新就绪队列里进程的信息
voidreadyQueUpdate()
{
intmaxPriority=-1;
PCB*pro=readyQueHead.nxt;
if(pro==NULL){
//就绪队列没有进程
readyMaxProcess=
NULL;
return;
}
while(pro!=NULL)
{
pro->priority
++;
if(pro->priority>maxPriority)
{
maxPriority=
pro->priority;
readyMaxProcess=pro;
}
pro=
pro->nxt;
}
}
//返回就绪队列最高优先级的值
intreadyMaxPriority()
{
returnreadyMaxProcess->priority;
}
//查看就绪队列里进程的信息
voidreadyQueWalk()
{
printf("就绪队列里的进程信息为: ");
queWalk(&readyQueHead);
}
//********************************
//
//模拟阻塞队列
//
//********************************
#defineEndBlockTime3
//进程最长被阻塞时间
intblockQueNum;//阻塞队列的进程数量
PCBblockQueHead;//
阻塞队列的头部
PCB*blockMaxProcess;//阻塞队列中优先级最高的进程
//进程插入到阻塞队列
voidblockQueQush(PCB
*process)
{
blockQueNum++;
process->blockTime=0;
process->state=Block;
queQush(process,&blockQueHead);
}
//优先级最高的进程出列
PCB*blockQuePop()
{
blockQueNum--;
quePop(blockMaxProcess,
&blockQueHead);
returnblockMaxProcess;
}
//每个时间片,更新阻塞队列里进程的信息
voidblockQueUpdate()
{
intmaxPriority=-1;
PCB*pro=blockQueHead.nxt;
while(pro!=NULL)
{
pro->blockTime
++;
if(pro->blockTime>=EndBlockTime)
{
PCB*process=pro;
pro=pro->nxt;
//阻塞时间到,调入就绪队列
blockQueNum--;
quePop(process,
&blockQueHead);
readyQueQush(process);
}else
if(pro->priority>maxPriority)
{
//更新阻塞队列里优先级最高的进程指针
maxPriority=
pro->priority;
blockMaxProcess=pro;
pro=pro->nxt;
}
}
}
//查看阻塞队列里进程的信息
voidblockQueWalk()
{
printf("阻塞队列里的进程信息为: ");
queWalk(&blockQueHead);
}
//********************************
//
//模拟动态优先权的进程调度
//
//********************************
//初始化数据
voidinitData()
{
//
初始化就绪队列和阻塞队列
readyQueNum=blockQueNum=0;
readyMaxProcess=blockMaxProcess=NULL;
readyQueHead.pre=readyQueHead.nxt=NULL;
blockQueHead.pre=blockQueHead.nxt=NULL;
//
初始化进程进入就绪队列
inti,maxPriority=-1;
for(i=0;i<N;i
++)
{
//分配一个PCB的内存空间
PCB*pro=(PCB
*)malloc(sizeof(PCB));
//给当前的PCB赋值
pro->id
=id[i];
pro->priority
=priority[i];
pro->cpuTime
=cpuTime[i];
pro->allTime
=allTime[i];
pro->blockTime
=0;
if(pro->allTime>0){
//插入到就绪队列中
readyQueQush(pro);
//更新就绪队列优先级最高的进程指针
if(pro->priority>
maxPriority){
maxPriority=pro->priority;
readyMaxProcess=pro;
}
}
}
}
//模拟cpu执行1个时间片的操作
voidcpuWord(PCB
*cpuProcess)
{
cpuProcess->priority-=3;
if(cpuProcess->priority<0)
{
cpuProcess->priority=0;
}
cpuProcess->cpuTime++;
cpuProcess->allTime--;
//
显示正执行进程的信息:
printf("CPU正执行的进程信息为: ");
printf("id:M,pri:M,
alltime:M ",
cpuProcess->id,
cpuProcess->priority,
cpuProcess->allTime);
}
intmain()
{
inttimeSlice=0;//
模拟时间片
intcpuBusy=0;
//模拟cpu状态
PCB*cpuProcess=NULL;//当前在cpu执行的进程
//
初始化数据
initData();
//
模拟进程调度
while(1)
{
if(readyQueNum==0
&&blockQueNum==0
&&cpuBusy==0){
//就绪队列、阻塞队列和cpu无进程,退出
break;
}
//printf(" %d%d",
readyQueNum,blockQueNum);
if(cpuBusy==0)
{
//cpu空闲,选择一个进程进入cpu
if(readyQueNum>0)
{
//
选择绪队列优先级最高的进程
cpuProcess
=readyQuePop();
}else{
//
就绪队列没有进程,改为选择阻塞队列优先级最高的进程
cpuProcess
=blockQuePop();
}
cpuProcess->cpuTime=
0;
cpuProcess->state=
Run;
cpuBusy=1;
}
timeSlice++;
printf(" 第%d个时间片后: ",
timeSlice);
//
模拟cpu执行1个时间片的操作
cpuWord(cpuProcess);
if(cpuProcess->allTime==0){
cpuProcess->state=
Finish;
//释放已完成进程的PCB
free(cpuProcess);
cpuBusy=0;
}
//
更新就绪队列和阻塞队列里的进程信息
blockQueUpdate();
readyQueUpdate();
//
查看就绪队列和阻塞队列的进程信息
readyQueWalk();
blockQueWalk();
if(cpuBusy==1
&&readyQueNum>0
&&
cpuProcess->priority
<readyMaxPriority()){
//需抢占cpu,当前执行的进程调入阻塞队列
blockQueQush(cpuProcess);
cpuProcess=readyQuePop();
}
}
printf(" 模拟进程调度算法结束 ");
return0;
}
⑸ 设计一个按优先数调度算法实现处理器调度的程序。
#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);
}
//这是运行过的,应该符合你的要求!
⑹ 多核CPU调度有哪几种算法 比如单核的有优先级、先来先服务。那多核的有哪几种呢
一般多核任务调度算法有全局队列调度和局部队列调度。前者是指操作系统维护一个全局的任务等待队列,当系统中有一个CPU核心空闲时,操作系统就从全局任务等待队列中选取就绪任务开始在此核心上执行。这种方法的优点是CPU核心利用率较高。后者是指操作系统为每个CPU内核维护一个局部的任务等待队列,当系统中有一个CPU内核空闲时,便从该核心的任务等待队列中选取恰当的任务执行,这种方法的优点是任务基本上无需在多个CPU核心间切换,有利于提高CPU核心局部Cache命中率。目前多数多核CPU操作系统采用的是基于全局队列的任务调度算法。
⑺ 设计一个按优先数调度算法实现处理器调度的程序。 高手帮忙!!
#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 。当新进程就绪时,系统检查其最终时限是否在当前运行的进程结束之前,如果是,则抢占当前进程。由于是动态算法,最早最终优先调度的优点就是灵活,当进程数不超过负载时,资源分配更优,但也同样由于它的动态属性,进程的优先级都是在不断变化中的,所以也没有哪个进程是一定可以保证满足调度的,当进程数超过负载时,资源分配合理度会急速下降,所以不太稳定。
⑼ 用c语言实现先到先处理和最短路径优先的cpu调度算法
#include <stdio.h>
#define n 20
struct fcfs
{
int id;
int atime;
int runtime;
int ftime;
}f[n];
zcx(){
int xz;
int amount;
printf("**************分割线*********************\n");
printf("1.先来先服务\n");
printf("2.优先级\n");
printf("请输入你的选择:");
scanf("%d",&xz);
printf("\n\n");
if(xz==1)
{
printf("你的选择是先来先服务\n");
printf("请输入进程数:");
scanf("%d",&amount);
yihao(amount);
}
else
{
printf("你的选择是优先级\n");
printf("请输入进程数:");
scanf("%d",&amount);
erhao(amount);
}
}
yihao(int amount)
{
int i,j,l,k;
struct fcfs f[n];
printf("\n\n");
for(i=0;i<amount;i++)
{
printf("请输入第 %d 个程序的信息\n",i+1);
printf("进程名 :");
scanf("%d",&f[i].id);
printf("到达时间:");
scanf("%d",&f[i].atime);
printf("运行时间:");
scanf("%d",&f[i].runtime);
}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;
k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}
printf("进程名 开始时间 运行时间 结束时间 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;
printf("%d %d %d %d\n",f[i].id,f[i].atime,f[i].runtime,f[i].ftime);
f[i+1].atime=f[i].ftime;
}
zcx();
}
erhao(int amount)
{
int i,j,l,k;
struct fcfs f[n];
printf("\n\n");
for(i=0;i<amount;i++)
{
printf("请输入第 %d 个程序的信息\n",i+1);
printf("进程名 :");
scanf("%d",&f[i].id);
printf("优先级 :");
scanf("%d",&f[i].atime);
printf("运行时间:");
scanf("%d",&f[i].runtime);
}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;
k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}
printf("进程名 优先级 工作时间 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;
printf("%d %d %d \n",f[i].id,f[i].atime,f[i].runtime);
f[i+1].ftime=f[i].ftime+f[i+1].atime;
}
zcx();
}
void main()
{
zcx();
}
这是操作系统的作业吧
⑽ CPU的调度算法:先来先服务、最短运行期、时间片轮转、优先权设置分别是什么意思
调度算法说的是现在有若干个进程(每个进程拥有自己的属性),算法根据它们的属性选择哪一个进程去执行。
先来先服务:按照进程来的时间早晚属性来判断,先来的先执行
最短:按照进程运行需要的时间长短属性来判断,最短的先执行
时间片轮转:和进程属性无关,每个进程都分配相同的时间去运行,轮着来
优先权设置:根据进程的优先级属性判断谁先执行,优先级是用户可以设定的
希望能够帮到你