Ⅰ 常見的調度演算法總結
一、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)
更改線程的優先順序。