导航:首页 > 源码编译 > 优先级调度算法代码实现

优先级调度算法代码实现

发布时间:2022-07-31 06:21:54

⑴ 设计一个按优先数调度算法实现处理器调度的程序。

#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);
}
//这是运行过的,应该符合你的要求!

⑵ 操作系统优先级调度算法怎么样用c的实现谢谢了,大神帮忙啊

进程调度源程序如下: jingchendiao.cpp #include "stdio.h" #include <stdlib.h> #include <conio.h> #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 struct pcb { /* 定义进程控制块PCB */ char name[10]; char state; int super; int ntime; int rtime; struct pcb* link; }*ready=NULL,*p; typedef struct pcb PCB; sort() /* 建立对进程进行优先级排列函数*/ { PCB *first, *second; int insert=0; if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/ { p->link=ready; ready=p; } else /* 进程比较优先级,插入适当的位置中*/ { first=ready; second=first->link; while(second!=NULL) { if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/ { /*插入到当前进程前面*/ p->link=second; first->link=p; second=NULL; insert=1; } else /* 插入进程优先数最低,则插入到队尾*/ { first=first->link; second=second->link; } } if(insert==0) first->link=p; } } input() /* 建立进程控制块函数*/ { int i,num; clrscr(); /*清屏*/ printf("\n 请输入进程号?"); scanf("%d",&num); for(i=0;i<num;i++) { printf("\n 进程号No.%d:\n",i); p=getpch(PCB); printf("\n 输入进程名:"); scanf("%s",p->name); printf("\n 输入进程优先数:"); scanf("%d",&p->super); printf("\n 输入进程运行时间:"); scanf("%d",&p->ntime); printf("\n"); p->rtime=0;p->state='w'; p->link=NULL; sort(); /* 调用sort函数*/ } } int space() { int l=0; PCB* pr=ready; while(pr!=NULL) { l++; pr=pr->link; } return(l); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { printf("\n qname \t state \t super \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ { PCB* pr; printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { disp(pr); pr=pr->link; }

⑶ 优先级调度算法程序代码

FIFO的方法用下边的Queue改写一下。
///////////////////// Queue.h //////////////////////////////
#ifndef QUEUE_H
#define QUEUE_H

namespace MyLibrary
{
#define MYLIBRARY_DEBUG
// MYLIBRARY_DEBUG 为测试而用
#ifdef MYLIBRARY_DEBUG
#include <iostream>
using std::ostream;
#endif

/// type def
#ifndef FALSE
#define FALSE false
#endif
#ifndef TRUE
#define TRUE true
#endif

typedef size_t size_type;
typedef bool BOOL;

/// 声明
template <typename _Ty> class Queue;

#ifdef MYLIBRARY_DEBUG
template <typename _Ty>
ostream & operator << ( ostream & , const Queue<_Ty> & );
#endif

//////////////////////// class ////////////////////////////
template <typename _Ty>
class Queue
{
//友元声明
#ifdef MYLIBRARY_DEBUG
friend ostream & operator << <> ( ostream &, const Queue<_Ty> & );
#endif

private:
//嵌套类定义
class QueueItem
{
public:
QueueItem( _Ty data ):_prior(0),_next(0),_data(data){}
public:
QueueItem * _prior; //前向指针
QueueItem * _next; //后向指针
_Ty _data; //数据
};
private:
//数据集
typename Queue<_Ty>::QueueItem * _head; //头结点指针
typename Queue<_Ty>::QueueItem * _tail; //尾结点指针

size_type _size; //长度

static const _Ty _temp; //只做空数据
public:
//构造析构集...
inline Queue():_head(0),_tail(0),_size(0){}
inline Queue( const Queue<_Ty> & );
inline ~Queue(){ while( !empty() )del(); }
public:
//外部操作接口集
inline const size_type size ( void ) const;
inline const BOOL empty ( void ) const;

inline const Queue & add ( const _Ty & );
inline const BOOL del ( void );
inline const BOOL del ( _Ty & );
inline const _Ty & get ( void )const;
public:
//重载操作符集
inline const Queue<_Ty> & operator = ( const Queue<_Ty> & );
inline const Queue<_Ty> & operator += ( const Queue<_Ty> & );
inline const Queue<_Ty> & operator += ( const _Ty & );
inline const Queue<_Ty> operator + ( const Queue<_Ty> & );
inline const Queue<_Ty> operator + ( const _Ty & );
inline const BOOL operator == ( const Queue<_Ty> & )const;
inline const BOOL operator != ( const Queue<_Ty> & )const;

};

template <typename _Ty>
inline const Queue<_Ty>
Queue<_Ty>::operator + ( const _Ty & value )
{
Queue<_Ty> temp = *this;
return temp+=value;
}

template <typename _Ty>
inline const Queue<_Ty> &
Queue<_Ty>::operator += ( const _Ty & value )
{
this->add( value );
return * this;
}

template <typename _Ty>
inline const Queue<_Ty>
Queue<_Ty>::operator + ( const Queue<_Ty> & queue )
{
// q=q1+q2;
if( queue.empty() )
{
return * this;
}
else
{
Queue<_Ty> temp = *this;
return temp += queue;
}
}

template <typename _Ty>
inline const Queue<_Ty> &
Queue<_Ty>::operator += ( const Queue<_Ty> & queue )
{
if( ! queue.empty() )
{
for( const typename Queue<_Ty>::QueueItem * it = queue._head;
it != queue._tail;
it = it->_prior )
{
this->add( it->_data );
}
this->add( queue._tail->_data );
}
return * this;
}

template <typename _Ty>
inline const BOOL
Queue<_Ty>::operator != ( const Queue<_Ty> & queue )const
{
if( queue.size() != this->size() )
{
return TRUE;
}
else
{
const typename Queue<_Ty>::QueueItem * youit = queue._tail;
const typename Queue<_Ty>::QueueItem * myit = this->_tail;
for(; myit != this->_head; myit=myit->_next,youit=youit->_next)
{
if( myit->_data != youit->_data )
{
return TRUE;
}
}
return (queue._head->_data != this->_head->_data)?TRUE:FALSE;
}
}

template <typename _Ty>
inline const BOOL
Queue<_Ty>::operator == ( const Queue<_Ty> & queue )const
{
if( queue.size() != this->size() )
{
return FALSE;
}
else
{
const typename Queue<_Ty>::QueueItem * youit = queue._tail;
const typename Queue<_Ty>::QueueItem * myit = this->_tail;
for(; myit != this->_head; myit=myit->_next,youit=youit->_next)
{
if( myit->_data != youit->_data )
{
return FALSE;
}
}
return (queue._head->_data != this->_head->_data)?FALSE:TRUE;
}
}

template <typename _Ty>
inline const Queue<_Ty> &
Queue<_Ty>::operator = ( const Queue<_Ty> & queue )
{
if( &queue == this )
{
return *this;
}
else
{
while( ! this->empty() )
{
this->del();
}
for( const typename Queue<_Ty>::QueueItem * it = queue._head;
it != queue._tail;
it = it->_prior )
{
this->add( it->_data );
}

this->add( queue._tail->_data );

return * this;
}
}

template <typename _Ty>
const _Ty Queue<_Ty>::_temp = _Ty();

template <typename _Ty>
inline const _Ty &
Queue<_Ty>::get( void )const
{
if( this->empty() )
return _temp; //返回表的空数据
else
{
return this->_head->_data;
}
}

template <typename _Ty>
inline const BOOL
Queue<_Ty>::del( void )
{
if( this->empty() )
return FALSE;
else
{
const typename Queue<_Ty>::QueueItem * temp = _head;
_head = _head->_prior;
--_size;
delete temp;

return TRUE;
}
}

template <typename _Ty>
inline const BOOL
Queue<_Ty>::del( _Ty & value )
{
if( this->empty() )
return FALSE;
else
{
const typename Queue<_Ty>::QueueItem * temp = _head;
value = temp->_data;
_head = _head->_prior;
--_size;
delete temp;

return TRUE;
}
}

template <typename _Ty>
inline const size_type
Queue<_Ty>::size(void)const
{
return _size;
}

template <typename _Ty>
inline const BOOL
Queue<_Ty>::empty( void )const
{
return (_size)?FALSE:TRUE;
}

template <typename _Ty>
inline const Queue<_Ty> &
Queue<_Ty>::add( const _Ty & value )
{
typename Queue<_Ty>::QueueItem * temp =
new typename Queue<_Ty>::QueueItem( value );
if( empty() )
{
_head = _tail = temp;
++_size;
}
else
{
temp->_next = _tail;
_tail->_prior = temp;
_tail = temp;
++_size;
}
return *this;
}

template <typename _Ty>
inline Queue<_Ty>::Queue( const Queue<_Ty> & queue )
:_head(0),_tail(0),_size(0)
{
if( this == &queue )
return;

for( const typename Queue<_Ty>::QueueItem * iter = queue._head;
iter != queue._tail;
iter = iter->_prior )
{
this->add( iter->_data );
}

this->add( queue._tail->_data );

return;
}

#ifdef MYLIBRARY_DEBUG
template <typename _Ty>
inline ostream & operator << ( ostream & os, const Queue<_Ty> & queue )
{
os<<"Queue("<<queue.size()<<"):\n";
os<<"head--->";
if( !queue.empty() )
{
for( const typename Queue<_Ty>::QueueItem * it = queue._head;
it != queue._tail;
it = it->_prior )
{
os<<it->_data<<" ";
}
os<<queue._tail->_data;
}
os<<"<---tail\n";
return os;
}
#endif

}////////// end namespace MyLibrary

#endif

⑷ 进程调度方案设计 实现一个基本动态优先级的调度算法

前两天做操作系统作业的时候学习了一下几种进程调度算法,在思考和讨论后,有了一些自己的想法,现在就写出来,跟大家讨论下。,或者说只有有限的CPU资源,当系统中有多个进程处于就绪状态,要竞争CPU资源时,操作系统就要负责完成如何分配资源的任务。在操作系统中,由调度程序来完成这一选择分配的工作,调度程序所使用的算法即是调度算法。调度算法需要考虑的指标主要有尽量保证CPU资源分配的公平性;按照一定策略强制执行算法调度;平衡整个计算机系统,尽量保持各个部分都处于忙碌状态。而根据系统各自不同的特点和要求,调度算法又有一些侧重点和目标不同,因此,算法按照系统差异主要分为三大类:批处理系统中的调度算法,代表调度算法有:先来先服务、最短作业优先、最短剩余时间优先。交互式系统中的调度算法,代表调度算法有:轮转调度、优先级调度、多级队列、最短进程优先、保证调度、彩票调度、公平分享调度。实时系统中的调度算法,代表调度算法有:速率单调调度、最早最终时限优先调度。下面就上述提到的调度算法中挑出几个进行重点分析:保证调度保证调度是指利用算法向用户做出明确的性能保证,然后尽力按照此保证实现CPU的资源分配。利用这种算法,就是定一个进程占用CPU的时间的标准,然后按照这个标准去比较实际占用CPU的时间,调度进程每次使离此标准最远的进程得到资源,不断满足离所保证的标准最远的进程,从而平衡资源分配满足这个标准的要求。保证调度算法的优点是:能很好的保证进程公平的CPU份额,当系统的特点是:进程的优先级没有太大悬殊,所制定的保证标准差异不大,各个进程对CPU的要求较为接近时,比如说系统要求n个进程中的每个进程都只占用1/n的CPU资源,利用保证调度可以很容易的实现稳定的CPU分配要求。但缺点是,这种情况太过理想,当系统的各个进程对CPU要求的紧急程度不同,所制定的保证较为复杂的时候,这个算法实现起来比较困难。彩票调度彩票调度这种算法的大意是指向进程提供各种系统资源如CPU资源的彩票,当系统需要做出调度决策时,随机抽出一张彩票,由此彩票的拥有者获得资源。在彩票调度系统中,如果有一个新的进程出现并得到一些彩票,那么在下一次的抽奖中,该进程会有同它持有彩票数量成正比例的机会赢得奖励。进程持有的彩票数量越多,则被抽中的可能性就越大。调度程序可以通过控制进程的彩票持有数量来进行调度。彩票调度有很多优点:首先,它很灵活,系统增加分给某个进程的彩票数量,就会大大增加它占用资源的可能性,可以说,彩票调度的反应是迅速的,而快速响应需求正是交互式系统的一个重要要求。其次,彩票调度算法中,进程可以交换彩票,这个特点可以更好的保证系统的平衡性,使其各个部分都尽可能的处于忙碌状态。而且利用彩票调度还可以解决许多别的算法很难解决的问题,例如可以根据特定的需要大致成比例的划分CPU的使用。速率单调调度速率单调调度算法是一种可适用于可抢占的周期性进程的经典静态实时调度算法。当实时系统中的进程满足:每个周期性进程必须在其周期内完成,且进程之间没有相互依赖的关系,每个进程在一次突发中需要相同的CPU时间量,非周期的进程都没有最终时限四个条件时,并且为了建模方便,我们假设进程抢占即刻发生没有系统开销,可以考虑利用速率单调算法。速率单调调度算法是将进程的速率(按照进程周期所算出的每秒响应的次数)赋为优先级,则保证了优先级与进程速率成线性关系,这即是我们所说的速率单调。调度程序每次运行优先级最高的,只要优先级较高的程序需要运行,则立即抢占优先级低的进程,而优先级较低的进程必须等所有优先级高于它的进程结束后才能运行。速率单调调度算法可以保证系统中最关键的任务总是得到调度,但是缺点是其作为一种静态算法,灵活性不够好,当进程数变多,系统调度变得复杂时,可能不能较好的保证进程在周期内运行。最早最终时限优先调度最早最终时限优先调度算法是一个动态算法,不要求进程是周期性的,只要一个进程需要CPU时间,它就宣布它的到来时间和最终时限。调度程序维持一个可运行的进程列表,按最终时限排序,每次调度一个最终时限最早的进程得到CPU 。当新进程就绪时,系统检查其最终时限是否在当前运行的进程结束之前,如果是,则抢占当前进程。由于是动态算法,最早最终优先调度的优点就是灵活,当进程数不超过负载时,资源分配更优,但也同样由于它的动态属性,进程的优先级都是在不断变化中的,所以也没有哪个进程是一定可以保证满足调度的,当进程数超过负载时,资源分配合理度会急速下降,所以不太稳定。

⑸ 求进程调度先来先服务算法,短进程优先算法完整c语言代码

/*(一)进程调度

进程调度算法有FIFO,优先数调度算法,时间片轮转调度算法,分级调度算法,

输入:进程流文件,其中存储的是一系列要执行的进程,
每个作业包括三个数据项:
进程名 所需时间 优先数(0级最高)
输出:
进程执行流 等待时间 平均等待时间

本程序包括:FIFO,优先数调度算法,时间片轮转调度算法

进程流文件process_stream.txt
测试数据:
p0 16 2
p1 5 1
p2 4 3
p3 8 0
p4 9 4
p5 7 6

VC++调试通过
*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>

const int Quatum=2;//定义时间片的长度为2秒
const int MAXPCB=100;//定义最大进程数

//定义进程结构体
typedef struct node
{
char name[20];//进程名
int time; //进程运行时间
int privilege;//进程优先级(静态)
int finished;//进程完成标志,0-未完成,1-已完成
int wait_time;//进程等待时间
}pcb;

pcb pcbs[MAXPCB];
int quantiry;//进程流文件中的进程总数

void initial()
{
int i;
for (i=0;i<MAXPCB;i++)
{
strcpy(pcbs[i].name,"");
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantiry=0;
}

int readData()
{
FILE *fp;
char fname[20];
int i;
cout<<"请输入进程流文件名:"<<endl;
cin>>fname;
if ((fp=fopen(fname,"r"))==NULL)
{
cout<<"错误,文件打不开,请检查文件名"<<endl;
}
else
{
while (!feof(fp))
{
fscanf(fp,"%s %d %d %d",pcbs[quantiry].name,
&pcbs[quantiry].time,&pcbs[quantiry].privilege);
quantiry++;
}
//输出所读入得数据
cout<<"输出所读入的数据"<<endl;
cout<<"进程流文件中的进程总数="<<quantiry<<endl;
cout<<"进程名 所需时间 优先数"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
}

return 1;
}

return 0;
}

//重置数据,以供另一个算法使用
void init()
{
int i;
for (i=0;i<MAXPCB;i++)
{
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
}

void FIFO()
{
int i,j;

int total;
//输出FIFO算法执行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"FIFO算法执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
for (j=i+1;j<quantiry;j++)
{
pcbs[j].wait_time+=pcbs[i].time;
}
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<" "<<"平均等待时间:"<<total/quantiry<<endl;
}

//优先度调度算法
void privilege()
{
int i,j,p;
int passed_time=0;
int total;

int queue[MAXPCB];
int current_privielege=1000;

for (i=0;i<quantiry;i++)
{
current_privielege=1000;
for (j=0;j<quantiry;j++)
{
if ((pcbs[j].finished==0)&&(pcbs[j].privilege<current_privielege))
{
p=j;
current_privielege=pcbs[j].privilege;
}
}
queue[i]=p;
pcbs[p].finished=1;
pcbs[p].wait_time+=passed_time;
passed_time+=pcbs[p].time;

}
//输出优先数调度执行流
cout<<endl<<"-----------------------------------------"<<endl;
cout<<"优先数调度执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[queue[i]].name<<" "<<pcbs[queue[i]].wait_time<<"--"<<queue[i]<<endl;
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}

cout<<"总等待时间:"<<total<<" 平均等待时间:"<<total/quantiry<<endl;
}

//时间片轮转调度算法
void timer()
{
int i,j,sum,flag=1;
int passed_time=0;
int max_time=0;
int round=0;

int queue[1000];
int total=0;

while(flag==1)
{
flag=0;
for (i=0;i<quantiry;i++)
{
if (pcbs[i].finished==0)
{
flag=1;
queue[total]=i;
total++;
if (pcbs[i].time<=Quatum*(round+1))
pcbs[i].finished=1;
}
}
round++;
}

cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"时间片轮转调度执行流:";
for(i=0;i<total;i++)
{
cout<<pcbs[queue[i]].name<<" ";
}
cout<<endl;
cout<<"进程名 结束时间 运行时间 等待时间"<<endl;

sum=0;

for (i=0;i<quantiry;i++)
{
for(j=total-1;j>=0;j--)//从轮转调度执行流序列由后往前比较,找到同名进程即可计算其完成时间
{
if (strcmp(pcbs[queue[j]].name,pcbs[i].name)==0)
{
cout<<" "<<pcbs[i].name<<" "<<(j+1)*Quatum<<" ";
cout<<pcbs[i].time<<" "<<(j+1)*Quatum-pcbs[i].time<<endl;
sum+=(j+1)*Quatum-pcbs[i].time;
break;
}
}
}

cout<<"总等待时间:"<<sum<<" "<<"平均等待时间:"<<sum/quantiry<<endl;
}

//显示版权信息函数
void version()
{
cout<<endl<<endl;

cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 进程调度模拟系统 ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ version 2011 ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl<<endl;
}
//主函数

int main()
{
int flag;
version();
initial();
flag=readData();
if(flag==1){
FIFO();
init();
privilege();
init();
timer();
}
cout<<endl;
system("pause");
return 0;
}

⑹ 优先级调度算法如何用JAVA实现

在多线程时,可以手动去设置每个线程的优先级setPriority(int newPriority)
更改线程的优先级。

⑺ 用C语言编写一段简单的程序,作业调度和低级调度算法

真不容易啊,怕是没人弄了!
优先级调度算法程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node
{
char name[10]; /*进程标识符*/
int prio; /*进程优先数*/
int round; /*进程时间轮转时间片*/
int cputime; /*进程占用CPU时间*/
int needtime; /*进程到完成还要的时间*/
int count; /*计数器*/
char state; /*进程的状态*/
struct node *next; /*链指针*/
}PCB;
PCB *finish,*ready,*tail,*run; /*队列指针*/
int N; /*进程数*/
/*将就绪队列中的第一个进程投入运行*/
firstin()
{
run=ready; /*就绪队列头指针赋值给运行头指针*/
run->state='R'; /*进程状态变为运行态*/
ready=ready->next; /*就绪对列头指针后移到下一进程*/
}
/*标题输出函数*/
void prt1(char a)
{
if(toupper(a)=='P') /*优先数法*/
printf(" name cputime needtime priority state\n");
else
printf(" name cputime needtime count round state\n");
}
/*进程PCB输出*/
void prt2(char a,PCB *q)
{
if(toupper(a)=='P') /*优先数法的输出*/
printf(" %-10s%-10d%-10d%-10d %c\n",q->name,
q->cputime,q->needtime,q->prio,q->state);
else/*轮转法的输出*/
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
q->cputime,q->needtime,q->count,q->round,q->state);
}
/*输出函数*/
void prt(char algo)
{
PCB *p;
prt1(algo); /*输出标题*/
if(run!=NULL) /*如果运行指针不空*/
prt2(algo,run); /*输出当前正在运行的PCB*/
p=ready; /*输出就绪队列PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
getch(); /*压任意键继续*/
}
/*优先数的插入算法*/
insert1(PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q; /*待插入的PCB指针*/
p1=ready; /*就绪队列头指针*/
r=p1; /*r做p1的前驱指针*/
b=1;
while((p1!=NULL)&&b) /*根据优先数确定插入位置*/
if(p1->prio>=s->prio)
{
r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1) /*如果条件成立说明插入在r与p1之间*/
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1; /*否则插入在就绪队列的头*/
ready=s;
}
}
/*轮转法插入函数*/
insert2(PCB *p2)
{
tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/
tail=p2;
p2->next=NULL;
}
/*优先数创建初始PCB信息*/
void create1(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL; /*就绪队列头指针*/
finish=NULL; /*完成队列头指针*/
run=NULL; /*运行队列指针*/
printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
for(i=1;i<=N;i++)
{
p=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) /*就绪队列不空调用插入函数插入*/
insert1(p);
else
{
p->next=ready; /*创建就绪队列的第一个PCB*/
ready=p;
}
}
clrscr();
printf(" output of priority:\n");
printf("************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready->next;
run->state='R';
}
/*轮转法创建进程PCB*/
void create2(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("Enter name and time of round process\n");
for(i=1;i<=N;i++)
{
p=malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0; /*计数器*/
p->state='w';
p->round=2; /*时间片*/
if(ready!=NULL)
insert2(p);
else
{
p->next=ready;
ready=p;
tail=p;
}
}
clrscr();
printf(" output of round\n");
printf("************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready->next;
run->state='R';
}
/*优先数调度算法*/
priority(char alg)
{
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
if(run->needtime==0) /*如所需时间为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';
insert1(run);
firstin(); /*将就绪队列的第一个进程投入运行*/
}
prt(alg); /*输出进程PCB信息*/
}
}
/*时间片轮转法*/
roundrun(char alg)
{
while(run!=NULL)
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->count=run->count+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; /*计数器置0*/
if(ready!=NULL) /*如就绪队列不空*/
{
run->state='W'; /*将进程插入到就绪队列中等待轮转*/
insert2(run);
firstin(); /*将就绪对列的第一个进程投入运行*/
}
}
prt(alg); /*输出进程信息*/
}
}
/*主函数*/
main()
{
char algo; /*算法标记*/
clrscr();
printf("type the algorithm:P/R(priority/roundrobin)\n");
scanf("%c",&algo); /*输入字符确定算法*/
printf("Enter process number\n");
scanf("%d",&N); /*输入进程数*/
if(algo=='P'||algo=='p')
{
create1(algo); /*优先数法*/
priority(algo);
}
else
if(algo=='R'||algo=='r')
{
create2(algo); /*轮转法*/
roundrun(algo);
}
}

⑻ 动态高优先权优先调度算法

动态高优先权优先调度算法:

动态优先权是指,在创建进程时所赋予的优先权,是可以随进程的推进或随其等待时间的增加而改变的,以便获得更好的调度性能。例如,我们可以规定,在就绪队列中的进程,随其等待时间的增长,其优先权以速率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 <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);
}

}

阅读全文

与优先级调度算法代码实现相关的资料

热点内容
怎么恢复手机app的安装包 浏览:300
idea重启项目不编译 浏览:495
程序员那么可爱演员表陆漓妈妈 浏览:127
linuxgadget驱动 浏览:592
华三调用acl的命令 浏览:7
资金流pdf 浏览:929
金融结算法补充条款 浏览:289
什么叫服务器怎么连接 浏览:519
空调压缩机有制冷但室内不是很冷 浏览:837
如何查解压成功 浏览:648
代码编译服务器硬件配置2021 浏览:413
php如何选中相同的进行修改 浏览:623
工行app个人怎么给企业账户转账 浏览:149
汇编与程序员 浏览:666
压缩包解码器下载 浏览:130
爱旅行的预备程序员 浏览:111
安卓qq浏览器怎么转换到ios 浏览:292
不同编译器的库可以调用吗 浏览:455
灰度信托基金加密 浏览:423
宿迁程序员兼职网上接单 浏览:929