實驗8 4 1 Linux的程序排程模擬程式設計

2021-09-19 04:52:56 字數 4629 閱讀 3740

給出程序排程的演算法描述(如基於動態優先順序和時間片輪轉排程演算法的描述)。用c語言設計乙個對n個併發程序進行排程的程式,每個程序由乙個程序控制塊(pcb)結構表示,該程序控制塊應包括下述資訊:程序標識id、程序優先數priority(並規定優先數與優先權成正比)、時間片數chip、程序已經占用cpu的時間cputime,程序還需要執行的時間alltime(當程序執行完畢時,其值為0)、程序的狀態state(為簡化起見。設每個程序處於執行e(excecuting)、就緒r(ready)和完成f(finish)三種狀態之一,並假設起始狀態都是就緒狀態r。),以及程序佇列指標next(用來將pcb排成佇列)等,可按照排程演算法的不同而增刪。

排程程式應當包含2種不同的排程演算法,執行時可以任選一種,以利於各種方法的分析和比較。

程式應能顯示或列印各種程序狀態和引數變化情況,便於觀察。即要顯示每個時間片內各程序的情況,並且指出執行程序及就緒和阻塞佇列中的內容。

程序控制塊(pcb)的定義如下:

typedef struct process_pcb

//排序比較函式:按照程序到達時間公升序排列

bool cmp_arrive_time(const pcb a, const pcb b);

//排序比較函式:按照程序優先數降序排序

bool cmp_priority(const pcb a, const pcb b);

//輸入程序資訊

void input_process();

//選擇程序排程策略

int select_policy();

//列印所有程序資訊

void print_all(int current);

//先來先服務演算法

void fcfs();

//時間片輪轉演算法

void round_robin();

//動態優先順序演算法

void dynamic_prio();

#include #include using namespace std;

//程序三種狀態,這裡增加一種,表示雖然輸入,但是還沒有到達進入系統時刻

typedef enum processstatestate;

//用於列印程序三種狀態

char* statestring = ;

typedef struct process_pcb

pcb;

//排序比較函式:按照程序到達時間公升序排列

bool cmp_arrive_time(const pcb a, const pcb b);

//排序比較函式:按照程序優先數降序排序

bool cmp_priority(const pcb a, const pcb b);

//輸入程序資訊

void input_process();

//選擇程序排程策略

int select_policy();

//列印所有程序資訊

void print_all(int current);

//先來先服務演算法

void fcfs();

//時間片輪轉演算法

void round_robin();

//動態優先順序演算法

void dynamic_prio();

pcb *running_process = null;//當前執行任務

vectorinput_queue;//程序輸入佇列,如當前時刻小於程序到達時間,則該程序仍然在輸入佇列中

vectorready_queue;//就緒佇列

vectorfinish_queue;//完成佇列

int main()

}//按程序到達時間公升序排列,先到達的排在隊首

bool cmp_arrive_time(const pcb a, const pcb b)

//按程序優先順序降序排列,優先順序高的排在隊首

//如優先順序相同,先到的程序排在前面

bool cmp_priority(const pcb a, const pcb b)

else

}//選擇程序排程策略

int select_policy()

else

} return n;

}//輸入程序資訊

void input_process()

//按照到達時間公升序排隊

sort(input_queue.begin(), input_queue.end(), cmp_arrive_time);

}//列印單個程序的資訊

void print_process(pcb* pro)

printf("%4d%10d%10d%8d%10s", pro->id, pro->arrive_time, pro->service_time, pro->priority, statestring[pro->state]);

//如程序尚未開始,則開始時間、結束時間以及剩餘時間以--表示

//如程序已經開始,但未結束,則其結束時間以--表示

if(pro->start_time == -1)elseelse

} //僅程序結束後,才統計其周轉時間及加權周轉時間

if(pro->state == finish)

else

}//列印所有程序的資訊,-1為列印程序初始輸入狀態

void print_all(int current)

else

printf("程序號 到達時間 服務時間 優先順序 狀態 開始時間 結束時間 剩餘時間 周轉時間 帶權周轉時間\n");

//列印正在執行的程序

if(running_process != null)

vector::iterator it;

//列印就緒佇列中的程序

for(it = ready_queue.begin(); it != ready_queue.end(); it ++)

//列印完成佇列中的程序

for(it = finish_queue.begin(); it != finish_queue.end(); it ++)

//列印仍在輸入佇列中的程序

for(it = input_queue.begin(); it != input_queue.end(); it ++)

}//先來先服務演算法

void fcfs()

//將輸入佇列中,到達時間小於等於當前時間片的程序放入就緒佇列中,並從輸入佇列中刪除

while(!input_queue.empty())else

} //判斷是否需要排程,如需要則從取出就緒佇列隊首程序進行排程

if(need_schedule && !ready_queue.empty())

print_all(chip);//列印當前時刻所有程序的資訊

//當前執行任務完成1個時間片,更改其資訊

if(running_process)else

} chip += 1;

} //所有任務全部完成後,列印一次

print_all(chip);

}//時間片輪轉演算法

void round_robin()

//將輸入佇列中,到達時間小於等於當前時間片的程序放入就緒佇列中,並從輸入佇列中刪除

while(!input_queue.empty())else

} //判斷是否需要排程,如需要則取出就緒佇列隊首程序進行排程

if(need_schedule && !ready_queue.empty())

running_process->state = executing;

need_schedule = false;

} print_all(chip);//列印當前時刻所有程序的資訊

//當前執行任務完成1個時間片,判斷該任務是否已經完成

if(running_process)elseelse

}} chip += 1;

} //所有任務全部完成後,列印一次

print_all(chip);

}//動態優先順序演算法

void dynamic_prio()

//將輸入佇列中,到達時間小於等於當前時間片的程序放入就緒佇列中,並從輸入佇列中刪除

while(!input_queue.empty())else

} if(!ready_queue.empty())

//判斷是否需要排程,如需要則取出就緒佇列隊首程序進行排程

if(need_schedule && !ready_queue.empty())

running_process->state = executing;

need_schedule = false;

} print_all(chip);//列印當前時刻,所有程序的資訊

//當前執行任務完成1個時間片,判斷該任務是否已經完成

if(running_process)else

if(!ready_queue.empty() && ready_queue[0].priority > running_process->priority)else

}} chip += 1;

} //所有任務全部完成後,列印一次

print_all(chip);

}

程序排程實驗 程序執行及其排程

程序概念從空間的維度上來看,程序是乙個由多種資訊構成的綜合體,它包括 段 資料段 堆 堆疊等,圖示如下 綜合程序關聯的各種資訊而構成了的乙個資料結構,我們稱為程序控制塊 process control block,pcb 也稱為任務控制塊 task control block 這些相關的資訊包括 從...

實驗三程序排程

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

linux程序排程

排程 從就緒的程序選出最適合的乙個來執行。知識點 1 排程策略 2 排程時機 3 排程步驟 排程策略 sched normal sched other 普通的分時程序 sched fifo 先入先出的實時程序 sched rr 時間片輪轉的實時程序 sched batch 批處理程序 sched i...