作業系統程序排程模擬程式

2021-04-14 12:50:50 字數 3570 閱讀 7115

//time.h

#ifndef time_h

#define time_h

/*乙個模擬時間的類,被pcb和pattemper使用

*作為資料成員,或者作為引數進行傳遞,類

*只定義了幾個操作符過載的函式用來作為引數

*和成員在計算中的使用。類本身會保證構造的時間

*都是正確的格式,如果不爭取會置為零值或者丟擲異常

*預設的複製控制符合要求

*/#include

#include

using namespace std;

//當時間不正確的時候丟擲這也異常

class wrongtime: public exception

};class time

inline bool time::operator == (const time &t)

#endif

//timefun.cpp

#include "time.h"

time::time(int hour, int minute, int second):

m_hour(hour), m_minute(minute), m_second(second)

}time& time::operator +=(const time &t) 

if (m_minute >= 60)

return *this;

}throw wrongtime();

}//當左運算元小於右運算元的時,不會得到負值,而是零值

time& time::operator -=(const time &t)throw(wrongtime)

else

else

}m_second -= t.m_second;

if (m_minute < t.m_minute)

m_minute -= t.m_minute;

m_hour -= t.m_hour;

}return *this;

}bool time::righttime() const

//pcb.h

#ifndef pcb_h

#define pcb_h

/*模擬程序控制快,包括程序的名字,程序的優先順序,

*程序需要執行的時間,程序等待的時間等程序的資訊。

*同時類有幾個友元函式作為在幾個程序排程演算法中作

*為排序的準則進行呼叫。做為系統感知程序的唯一標識,

*程序控制塊從一開始就不能為空,所以不能有預設的

*建構函式,預設的複製操作符合要求

*/#include

#include

#include "time.h"

using namespace std;

//一些標識狀態的常量

const int waite = 1;

const int run = 2;

const int finish = 3;

class pcb

inline void pcb::setwaitetime(const time &t)

inline bool pcb::pcbfinish()

inline void pcb::setrtime()

inline time& pcb::returnrtime()

inline bool levelsortrule(const pcb &lp, const pcb &rp)

inline bool sjfsortrule(const pcb &lp, const pcb &rp)

inline bool hrnsortrule(const pcb &lp, const pcb &rp)

inline double pcb::returnm_r()

#endif

//pcbfun.cpp

#include "pcb.h"

pcb::pcb(const string &name, const time &runtime, int level):

m_name(name), m_runtime(runtime), m_level(level)

void pcb::setrtime(const time &t)

else

m_runtime -=  t;

m_endtime += t;

--m_level;

}bool pcb::operator ==(const pcb &p)

//pattemper.h

#ifndef pattemper_h

#define pattemper_h

/*程序排程過程的模擬,演算法包括:先來先服務,

*最高優先順序演算法,最短作業優先演算法,最高響應比演算法。

*程序開始執行的時間,程序平均等待的時間,時間片平均

*長度在建構函式中都進行了設定。在最高優先順序演算法,最

*高優先順序演算法和先來先服務演算法中,程序的等待時間對演算法

*本身的影響很小,所以不予以考慮,也降低了複雜度。但在

*最高響應比演算法中需要用到。預設的複製控制符合要求

*/#include

#include "pcb.h"

using namespace std;

class pattemper

;#endif

//patfun.cpp

#include

#include

#include

#include

#include "pattemper.h"

using namespace std;

//將預設的起始時間設定為零值,並從檔案data.txt中讀取程序

pattemper::pattemper():m_begintime(time()),m_evewtime(time(0,1,10)),

m_ertime(0,10,10)

}//如果要新增的程序已經存在什麼也不做

void pattemper::add()

//各種演算法的排程

void pattemper::operate()

}//先來先服務演算法,按先後順序提供給程序足夠的執行時間共執行

//列印出程序執行前後的情況,為了減少複雜度,由於等待時間

//對本演算法沒沒什麼太大的影響所以不予以考慮

void pattemper::fcfs()

}//最短作業優先演算法,處理器提供給足夠的時間執行,

//執行結束刪除,列印出執行前後程序的資訊,為了

//減少程式的複雜度,由於等待時間對本演算法的影響很小

//所以不予以考慮

void pattemper::sjf()

}//最高響應比優先演算法,這個和程序的等待時間有關係,不僅要設定平均等待

//時間,每個程序執行結束還要將它的執行時間做為等待時間賦值給它後面的

//程序,列印程序執行前後的資訊

void pattemper::hrn()

}//main.cpp

#include

#include "pattemper.h"

using namespace std;

int main()

作業系統 實驗三 程序排程模擬程式

1.1.實驗目的 用高階語言完成乙個程序排程程式,以加深對程序的概念及程序排程演算法的理解。1.2.實驗要求 1.2.1例題 設計乙個有 n個程序併發執行的程序排程模擬程式。程序排程演算法 採用最高優先順序優先的排程演算法 即把處理機分配給優先順序最高的程序 和先來先服務 若優先順序相同 演算法。1...

作業系統 實驗三 程序排程模擬程式

實驗三程序排程模擬程式 專業 商軟二班 姓名 江志彬 學號 201406114225 1.1.實驗目的 用高階語言完成乙個程序排程程式,以加深對程序的概念及程序排程演算法的理解。1.2.實驗要求 1.2.1例題 設計乙個有 n個程序併發執行的程序排程模擬程式。程序排程演算法 採用最高優先順序優先的排...

實驗三 程序排程模擬程式 作業系統

實驗三 程序排程模擬程式 作業系統 一.目的和要求 1.1.實驗目的 用高階語言完成乙個程序排程程式,以加深對程序的概念及程序排程演算法的理解。1.2.實驗要求 1.2.1例題 設計乙個有 n個程序併發執行的程序排程模擬程式。程序排程演算法 採用最高優先順序優先的排程演算法 即把處理機分配給優先順序...