Ⅰ 常见的调度算法总结
一、FCFS——先来先服务和短作业(进程)优先调度算法
1. 先来先服务调度算法。
先来先服务(FCFS)调度算法是一种最简单的调度算法,该算法既可用于作业调度, 也可用于进程调度。FCFS算法比较有利于长作业(进程),而不利于短作业(进程)。由此可知,本算法适合于CPU繁忙型作业, 而不利于I/O繁忙型的作业(进程)。
2. 短作业(进程)优先调度算法。
短作业(进程)优先调度算法(SJ/PF)是指对短作业或短进程优先调度的算法,该算法既可用于作业调度, 也可用于进程调度。但其对长作业不利;不能保证紧迫性作业(进程)被及时处理;作业的长短只是被估算出来的。
二、FPF高优先权优先调度算法
1. 优先权调度算法的类型。
为了照顾紧迫性作业,使之进入系统后便获得优先处理,引入了最高优先权优先(FPF)调度算法。 此算法常被用在批处理系统中,作为作业调度算法,也作为多种操作系统中的进程调度,还可以用于实时系统中。当其用于作业调度, 将后备队列中若干个优先权最高的作业装入内存。当其用于进程调度时,把处理机分配给就绪队列中优先权最高的进程,此时, 又可以进一步把该算法分成以下两种:
1)非抢占式优先权算法
2)抢占式优先权调度算法(高性能计算机操作系统)
2. 优先权类型 。
对于最高优先权优先调度算法,其核心在于:它是使用静态优先权还是动态优先权, 以及如何确定进程的优先权。
3.动态优先权
高响应比优先调度算法为了弥补短作业优先算法的不足,我们引入动态优先权,使作业的优先等级随着等待时间的增加而以速率a提高。 该优先权变化规律可描述为:优先权=(等待时间+要求服务时间)/要求服务时间;即 =(响应时间)/要求服务时间
三、基于时间片的轮转调度算法
1.时间片轮转法。
时间片轮转法一般用于进程调度,每次调度,把CPU分配队首进程,并令其执行一个时间片。 当执行的时间片用完时,由一个记时器发出一个时钟中断请求,该进程被停止,并被送往就绪队列末尾;依次循环。
2. 多级反馈队列调度算法
多级反馈队列调度算法多级反馈队列调度算法,不必事先知道各种进程所需要执行的时间,它是目前被公认的一种较好的进程调度算法。 其实施过程如下:
1) 设置多个就绪队列,并为各个队列赋予不同的优先级。在优先权越高的队列中, 为每个进程所规定的执行时间片就越小。
2) 当一个新进程进入内存后,首先放入第一队列的末尾,按FCFS原则排队等候调度。 如果他能在一个时间片中完成,便可撤离;如果未完成,就转入第二队列的末尾,在同样等待调度…… 如此下去,当一个长作业(进程)从第一队列依次将到第n队列(最后队列)后,便按第n队列时间片轮转运行。
3) 仅当第一队列空闲时,调度程序才调度第二队列中的进程运行;
仅当第1到第( i-1 )队列空时, 才会调度第i队列中的进程运行,并执行相应的时间片轮转。
4) 如果处理机正在处理第i队列中某进程,又有新进程进入优先权较高的队列, 则此新队列抢占正在运行的处理机,并把正在运行的进程放在第i队列的队尾。
Ⅱ 怎么用C语言实现多级反馈队列调度算法
调度算法的实施过程如下所述:(1)应设置多个就绪队列,并为各个队列赋予不同的优先级。(2)当一个新进程进入内存后,首先将它放入第一队列的末尾,按FCFS的原则排队等待调度。当轮到该进程执行时,如他能在该时间片内完成,便可准备撤离系统;如果它在一个时间片结束时尚未完成,调度程序便将该进程转入第二队列的末尾,再同样地按FCFS原则等待调度执行;如果它在第二队列中运行一个时间片后仍未完成,再依次将它放入第三队列……,如此下去,当一个长作业进程从第一队列依次降到第N队列后,在第N队列中便采取时间片轮转的方式运行
Ⅲ 求一个操作系统处理机调度先来先服务算法的代码,急,要C++的
没财富回答个吊
Ⅳ 操作系统进程调度算法(数组)c++
1.程序算法
struct PCB
{
int pname;
int pri;
int runtime;
int waitting;
struct PCB*next;
}
pcb[7];
struct PCB*running,ready,wait;
int sin=0;
main()
{ 创建PCB[3]--PCB[9]并插入ready队列;/*pname分别为3--9,
pri=0,runtime=10,waittime=0 */
for(;;)/*系统程序,完成初始化和处理机分派功能*/
{cast{sig=0:swtch;
sig=1:waiter;
sig=3:proc3;
sig=4:proc4;
sig=5:proc5;
sig=6:proc6;
sig=7:proc7;
sig=8:proc8;
sig=9:proc9;}
}
}
2.进程调度程序
swtch()
{
while(ready==NULL)wakeup();
移出就绪队列第一个PCB;
送running指针;
若pri=1,则runntime=4,否则runtime=10;
将running→pname送sig
}
3。 将进程等待函数
wait()
{将运行进程插入wait队列,优先数置1;
sig=0;
}
4。进程唤醒函数
wakeup()
{
将wait队列中所有的PCB中waittime减1;
将wait队列中的所有的waittime=0的PCB揭除;
插入到ready队列中第一个优先级为0的PCB前面
}
Ⅳ 进程调度算法1——FCFS、SJF、HNNR
进程的调度方式有两种: 非剥夺调度方式(非抢占式)和剥夺调度方式(抢占方式)。
非抢占式:只允许进程主动放弃处理机。如进程运行结束、异常结束或主动请求I/O阻塞。在运行的过程中即使有更紧迫的任务到达,当前进程依然会继续使用处理机,直到该进程终止或主动要求进入阻塞态。
抢占式:当一个进程正在处理机上执行时,如果有一个更重要更紧迫的进程需要处理机,则立即暂停正在执行的进程,将处理机分配给更重要更紧迫的那个进程。
下面介绍适用于早期操作系统几种进程调度的算法
先来先服务(FCFS):按照到达的先后顺序调度,事实上就是等待时间越久的越优先得到服务。
下面表示按照先来先服务算法的执行顺序
计算进程的几个衡量指标:
短作业优先算法是非抢占式的算法,但是也有抢占式的版本—— 最短剩余时间优先算法(STRN,Shortest Remaining Time Next) 。
用于进程的调度算法称为短进程优先调度算法(SPF,Shortest Process First)。
短作业/进程优先调度算法:每次调度时选择当前已到达且运行时间最短的作业/进程.。
因为进程1最先达到,此时没有其他线程,所以进程1先被服务。当进程1运行完后,进程2和3已经到达,此时进程3需要的运行时间比进程2少,所以进程3先被服务…
计算进程的几个衡量指标:
最短剩余时间优先算法:每当有进程 加入就绪队列改变时就需要调度 ,如果新到达的进程的所需的运行时间比当前运行的进程剩余时间更短,则由新进程抢占处理机,当前运行进程重新回到就绪队列。此外,当一个 进程完成时也需要调度 。
通过比较上面三组的平均周转时间、平均带权周转时间和平均等待时间可以看出,短作业优先算法可以减少进程的等待时间,对短作业有利。
高响应比优先算法: 非抢占式的调度算法 ,只有当前运行的进程主动放弃CPU时(正常/异常完成、或主动阻塞),才需要进行调度,调度时计算所有就绪进程的相应比,选响应比最高的进程上处理机。
响应比 = (等待时间 + 运行时间)/ 运行时间
上面的三种调度算法一般适用于 早期的批处理系统 ,没有考虑响应时间也不区分任务的紧急程度。因此对用户来说交互性差。
如发现错误,请指正!!!
Ⅵ 急求c++处理机调度算法的实现
#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;
void
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;
}
}
void
input()
/*
建立进程控制块函数*/
{
int
i,num;
system("cls");
/*清屏*/
printf("\n
请输入进程数:
");
scanf("%d",&num);
for(i=1;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);
}
void
disp(PCB
*
pr)
/*建立进程显示函数,用于显示当前进程*/
{
printf("\n
进程名\t
状态\t
优先数\t
需要运行时间\t
已经运行时间\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
void
check()
/*
建立进程查看函数
*/
{
PCB*
pr;
printf("\n
****
当前正在运行的进程是:\n");
/*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n
****
当前就绪队列状态为:\n");
/*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void
destroy()
/*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n
进程
[%s]
已完成.\n",p->name);
free(p);
}
void
running()
/*
建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();
/*
调用destroy函数*/
else
{
(p->super)--;
p->state='W';
sort();
/*调用sort函数*/
}
}
void
main()
/*主函数*/
{
int
len,h=0;
char
ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("-----------------------------------------------------");
printf("\n
现在是第%d次运行:
\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n
按任意键继续......\n");
}
printf("\n\n
进程已经完成.\n");
}
Ⅶ 急求 程序代码 c/c++ 操作系统中的 处理机调度算法
#include <iostream>
#include <stdio.h>
#include <string>
//#include <windows.h>
using namespace std;
//hyugtyftydrtdtrdrrtrdrt
struct Node
{
string name;//进程(作业)名称
int arriveTime;//到达时间
int ServerTime;//服务时间
int leftTime;//the left time
Node *link;//指向下一个节点的指针
};
class CProcess
{
public:
CProcess();//构造函数
~CProcess();//析构函数
const CProcess &operator =(const CProcess& p);//重载赋值操作符
void insertNode(string &na,int& at,int& st);//插入新元素(at由小到大)到链表合适的位置
void sort();//按照服务时间由大到小排序
bool isEmpty();//判断是否为空
void destroy();//销毁
int length();//求出链表长度
void print();//打印出元素
void FCFS();//先到先服务
void SJF();//短进程(作业)优先
void RR(int& q);//时间片轮转
void priority();//优先权调度
protected:
Node *first;
Node *last;
};
const CProcess& CProcess::operator=(const CProcess& p)
{
Node *newNode;
Node *Current;
if(this!=&p)//避免自己给自己赋值
{
if(first!=NULL)//如果链表不为空
destroy();
if(p.first==NULL)
{//如果要拷贝的对象为空
this->first = NULL;
this->last = NULL;
}
else
{
Current = p.first;
first= new Node;
first->name=Current->name;//
first->arriveTime=Current->arriveTime;
first->ServerTime=Current->ServerTime;
first->link =NULL;
last =first;
Current = Current->link;
while(Current!=NULL)
{
newNode = new Node;
newNode->name=Current->name;
newNode->arriveTime=Current->arriveTime;
newNode->ServerTime=Current->ServerTime;
newNode->link=NULL;
last->link=newNode;
last=newNode;
Current = Current->link;
}
}
}
return *this;
}
CProcess::CProcess()
{//构造函数
first=NULL;
last=NULL;
}
CProcess::~CProcess()
{
Node *temp;
while(first!=NULL)
{
temp=first;
first=first->link;
delete temp;
}
last=NULL;
}
void CProcess::insertNode(string &na,int& at,int& st)
{//按照到达时间升序排序
Node *Current;
Node *trailCurrent;//指向Current的前一个节点
Node *newNode;
bool found;
newNode = new Node;//建立一个新节点
newNode->name=na;
newNode->arriveTime=at;
newNode->ServerTime=st;
newNode->link=NULL;//
if(first==NULL)//如果第一个节点为空(如果是第一次插入元素)
first=newNode;//将新节点赋给第一个节点
else
{//如果不是第一次
Current =first;
found = false;
while(Current!=NULL && !found)
{
if(Current->arriveTime >= at)
found = true;
else
{
trailCurrent = Current;
Current = Current->link;
}
}
if(Current==first)
{
newNode->link = first;
first = newNode;
}
else
{
trailCurrent->link = newNode;
newNode->link = Current;
}
}
}
int CProcess::length()
{
int count =0;//声明变量,并初始化为0(用来记录长度)
Node *Current;
Current = first;
while(Current!=NULL)//当前节点不为空,记录值自加,一直向后遍历,
{
count++;
Current = Current->link;
}
return count;//返回长度
}
void CProcess::sort()//按照服务时间,升序排列
{//冒泡排序
string sname;
int at;
int st;
Node *Current;//指向当前节点
Node *trailCurrent;//指向当前节点的前一个节点
for(trailCurrent=first->link;trailCurrent!=NULL;trailCurrent=trailCurrent->link)//控制条件有问题
{
for(Current=trailCurrent->link;Current!=NULL;Current=Current->link)//控制条件有问题
{
if(trailCurrent->ServerTime > Current->ServerTime)
{
sname=trailCurrent->name;
at=trailCurrent->arriveTime;
st=trailCurrent->ServerTime;
trailCurrent->name=Current->name;
trailCurrent->arriveTime=Current->arriveTime;
trailCurrent->ServerTime=Current->ServerTime;
Current->name=sname;
Current->arriveTime=at;
Current->ServerTime=st;
}
}
}
}
bool CProcess::isEmpty()//判断是否为空
{
return (first==NULL);//如果第一个节点为空,返回值
}
void CProcess::print()
{
Node *Current;
Current = first->link;//头节点赋给当前节点
while(Current!=NULL)//当前节点不为空,一直向后遍历打印
{
cout<<Current->name<<" ";
cout<<Current->arriveTime<<" ";
cout<<Current->ServerTime<<"\n";
Current = Current->link;
}
}
void CProcess::destroy()
{
Node *temp;//定义一个临时指针变量
while(first!=NULL)
{
temp=first;
first=first->link;
delete temp;
}
last=NULL;
}
void CProcess::FCFS()//先到先服务
{
Node *Current;
int T0=0;//完成时间
int T1=0;//周转时间
Current = first->link;//头节点赋给当前节点
while(Current!=NULL)
{
if(T0 < Current->arriveTime)
{
T0=Current->arriveTime+Current->ServerTime;
T1=T0-Current->arriveTime;
cout<<Current->name<<"\t";//打印出进程名
cout<<T0<<"\t";//打印出完成时间
cout<<T1<<"\n";//打印出周转时间
Current = Current->link;
}
else
{
T0=Current->ServerTime+T0;
T1=T0-Current->arriveTime;//周转时间等于,完成时间 - 到达时间
cout<<Current->name<<"\t";//打印出进程名
cout<<T0<<"\t";//打印出完成时间
cout<<T1<<"\n";//打印出周转时间
Current = Current->link;
}
}
}
void CProcess::SJF()//短进程(作业)优先
{
//首先执行第一个到达的作业
Node *Current;
int T0=0;//完成时间
int T1=0;//周转时间
T0=first->link->ServerTime+T0;
T1=T0-first->link->arriveTime;
cout<<first->link->name<<"\t";
cout<<T0<<"\t";//打印出完成时间
cout<<T1<<"\n";//打印出周转时间
first->link=first->link->link;//删除
//执行剩下的
sort();//对剩下的排序
Current = first->link;//头节点赋给当前节点
while(Current!=NULL)
{
if(T0 < Current->arriveTime)
{
T0=Current->arriveTime+Current->ServerTime;
T1=T0-Current->arriveTime;
cout<<Current->name<<"\t";//打印出进程名
cout<<T0<<"\t";//打印出完成时间
cout<<T1<<"\n";//打印出周转时间
Current = Current->link;
}
else
{
T0=Current->ServerTime+T0;
T1=T0-Current->arriveTime;//周转时间等于,完成时间 - 到达时间
cout<<Current->name<<"\t";//打印出进程名
cout<<T0<<"\t";//打印出完成时间
cout<<T1<<"\n";//打印出周转时间
Current = Current->link;
}
}
}
void CProcess::RR(int& q)//时间片轮转
{
cout<<"时间片轮转操作完成!\n";
}
void CProcess::priority()//优先权调度
{
cout<<"优先权操作完成!\n";
}
void main()
{
CProcess p0,p1,p2,p3,p4;
int at,st;
string na;
int judge=1;//控制退出程序
int choice;//控制选择操作
while(judge)
{
cout<<"********************************************************\n";
cout<<"****** 说明:本程序适用于单道进程(作业) ******\n";
cout<<"******** 请选择您的操作 ***************\n";
cout<<"*********输入相应的数字,按下(Enter)键!**************\n";
cout<<"************* 5.录入信息 ************\n";
cout<<"************* 1.先到先服务 ************\n";
cout<<"************* 2.短进程(作业)优先 ************\n";
cout<<"************* 3.时间片轮转 ************\n";
cout<<"************* 4.优先权(静态)调度 ************\n";
cout<<"************* 0.退出程序 ************\n";
cout<<"********************************************************\n";
cin>>choice;
switch(choice)
{
case 0:
judge=0;
break;
case 5:
cout<<"请输入信息以“end”结束输入!\n";
cout<<"进程名 到达时间 服务时间"<<endl;
while(na.compare("end"))//如果相等则会返回0
{
p0.insertNode(na,at,st);
cin>>na>>at>>st;
}
cout<<"录入成功,目前的信息为:\n";
cout<<"进程名 到达时间 服务时间"<<endl;
p0.print();
break;
case 1://先到先服务
p1=p0;//拷贝一份
if(p1.isEmpty())
{
cout<<"请先录入信息\n";
break;
}
else
{
cout<<"先到先服务\n";
cout<<"进程名 完成时间 周转时间\n";
p1.FCFS();
break;
}
case 2://短作业优先
p2=p0;//拷贝一份
//p2.sort();
//p2.print();
if(p2.isEmpty())
{
cout<<"请先录入信息\n";
break;
}
else
{
cout<<"短作业优先\n";
cout<<"进程名 完成时间 周转时间\n";
p2.SJF();
break;
}
case 3://时间片轮转
p3=p0;//拷贝一份
int q;
if(p3.isEmpty())
{
cout<<"请先录入信息\n";
break;
}
else
{
cout<<"请输入时间片大小";
cin>>q;
cout<<"时间片轮转\n";
cout<<"进程名 完成时间 周转时间\n";
p3.RR(q);
break;
}
case 4://优先权
p4=p0;//拷贝一份
if(p4.isEmpty())
{
cout<<"请先录入信息\n";
break;
}
else
{
cout<<"时间片轮转\n";
cout<<"进程名 完成时间 周转时间\n";
p4.priority();
break;
}
default:
cout<<"请选择目录中的选项!\n";
break;
}
}
return;
}
Ⅷ 优先级调度算法程序代码
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
Ⅸ 求磁盘调度算法scan算法的java代码
1、先来先服务算法(FCFS)First Come First Service
这是一种比较简单的磁盘调度算法。它根据进程请求访问磁盘的先后次序进行调度。此算法的优点是公平、简单,且每个进程的请求都能依次得到处理,不会出现某一进程的请求长期得不到满足的情况。此算法由于未对寻道进行优化,在对磁盘的访问请求比较多的情况下,此算法将降低设备服务的吞吐量,致使平均寻道时间可能较长,但各进程得到服务的响应时间的变化幅度较小。
先来先服务 (125)86.147.91.177.94.150.102.175.130
[java] view plain print?
Ⅹ 优先级调度算法如何用JAVA实现
在多线程时,可以手动去设置每个线程的优先级setPriority(int newPriority)
更改线程的优先级。