日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

经典时间片轮转RR算法C语言实现

發布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 经典时间片轮转RR算法C语言实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

**RR算法主要體現在兩個時機的調度:
1.進程的服務時間用完時,無論時間片到沒到,時間片都需要置0。
2.進程的服務時間沒用完,而且時間片到了,需要把此進程添加到隊尾,時間片置0。
進程都運行結束時,調出循環的條件需要注意。
具體可以看注釋:

#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std;typedef struct PCB {char name;int arrivaltime; //到達時間int Servicetime; //服務時間int Finishtime; //完成時間int Wholetime; //周轉時間double WeightWholetime; //帶權周轉時間}RR; struct QueueNode { //鏈表結構RR node;struct QueueNode* next; };typedef struct { //隊列結構QueueNode* front; //隊頭QueueNode* rear; //隊尾 }LinkQueue;void start_state(RR* ResultPCB,int n); //讀入假設的數據,設置系統初始狀態 void dispath(LinkQueue* q_ready,RR* ResultPCB,int n); // 模擬調度 void frontNodeTorear(LinkQueue* q); //隊首進程添到隊尾 bool IsEmptyQueue(LinkQueue* q); //隊列判空 void InitQueue(LinkQueue* q); //分配內存 void InsertQueueNode(LinkQueue* q, RR TempPCB); //入隊 bool DeleteQueueNode(LinkQueue* q); //出隊 bool cmp(RR a,RR b); //排序輔助函數int main() {LinkQueue* q_ready = (LinkQueue*)malloc(sizeof(LinkQueue)); //為就緒隊列分配內存空間InitQueue(q_ready);int n; //進程個數printf("Enter n:");scanf("%d", &n);RR* ResultPCB = (RR*)malloc(sizeof(RR)); //存儲進程信息的結構體數組start_state(ResultPCB, n); //進程初始化dispath(q_ready, ResultPCB, n); //調度//RR算法基本實現,下面進行數據匯總處理int SumWT = 0;double SumWWT = 0;printf("\nID\tArrivalTime\tServiceTime\tFinishTime\tWholeTime\tWeightWholeTime\n");for (int i = 0;i < n;i++) {ResultPCB[i].Wholetime = ResultPCB[i].Finishtime - ResultPCB[i].arrivaltime;ResultPCB[i].WeightWholetime = (1.0)*ResultPCB[i].Wholetime /ResultPCB[i].Servicetime;SumWT += ResultPCB[i].Wholetime; //累計總周轉時間SumWWT += ResultPCB[i].WeightWholetime; //累計總帶權周轉時間printf("%c\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2lf\n",ResultPCB[i].name, ResultPCB[i].arrivaltime, ResultPCB[i].Servicetime, ResultPCB[i].Finishtime, ResultPCB[i].Wholetime, ResultPCB[i].WeightWholetime);}double AverageWT = (1.0*SumWT) / n; //平均周轉時間double AverageWWT= (1.0 * SumWWT) / n;//平均帶權周轉時間printf("SumWT=%d\n", SumWT);printf("SumWWT=%.2lf\n", SumWWT);printf("AverageWT=%.2lf\n", AverageWT);printf("AverageWWT=%.2lf\n", AverageWWT);return 0; }void start_state(RR* ResultPCB, int n) //初始化 { for (int i = 0;i < n;i++){ResultPCB[i].name = 'A' + i;}printf("Enter ArrivalTime:");for (int i = 0;i < n;i++) {scanf("%d", &ResultPCB[i].arrivaltime);}printf("Enter ServiceTime:");for (int i = 0;i < n;i++) {scanf("%d", &ResultPCB[i].Servicetime);}sort(ResultPCB, ResultPCB + n, cmp); //排序 } void dispath(LinkQueue* q_ready, RR* ResultPCB, int n) //主要輪轉算法實現 {int j = 0; //累計入隊的進程個數int SumoperateTime = 0; //總時間int x = 0; //累計時間片int t; //時間片大小printf("Enter TimeSlice:");scanf("%d", &t);while(1){if (j >= n && !q_ready->front->next) {//當進入隊列的進程個數為總進程數n時且隊列為空時退出循環//表示所有進程全部運行結束break; }printf("Time%d:", SumoperateTime); for (int i = 0;i < n;i++){if (ResultPCB[i].arrivaltime == SumoperateTime) {//每次需要遍歷所有進程到達時間,可能有些進程到達時間一樣,依次入隊printf(" %c arrived ", ResultPCB[i].name);InsertQueueNode(q_ready, ResultPCB[i]); //添加新進程進入隊列++j; //入隊的進程個數}}if (0==q_ready->front->next->node.Servicetime) //隊首進程服務時間用完時{printf(" %c finished.", q_ready->front->next->node.name); //finish表示此進程運行結束for (int i = 0;i < n;i++) {if (q_ready->front->next->node.name == ResultPCB[i].name) //遍歷數組,找到與隊首進程對應的項{ResultPCB[i].Finishtime = SumoperateTime; //把總時間作為完成時間賦值給對應的進程}}DeleteQueueNode(q_ready); //注銷隊首進程x = 0; //此時進程切換,時間片需要置0}else if (0<q_ready->front->next->node.Servicetime) //若隊首進程還有服務時間{if (x < t) //如果不到時間片,則什么也不做,下次還是執行此隊首進程{;}else //如果到達時間片,此時需要進行進程切換{x = 0; //無疑時間片需要置0if (!q_ready->front->next->next) //如果隊里只有一個進程,沒必要放到隊尾{;}else frontNodeTorear(q_ready); //如果有多個進程,則把隊首進程移到隊尾}}if (!IsEmptyQueue(q_ready)) //隊列不空{printf(" %c executing.\n",q_ready->front->next->node.name); //執行隊首進程--q_ready->front->next->node.Servicetime; //執行一次后服務時間減一}else //隊列為空{printf("***沒有進程運行\n");}++x; //累計時間片++SumoperateTime; //累計總時間,用于明確各個進程完成時間}}void InitQueue(LinkQueue* q) { //初始化頭結點q->front = q->rear = (QueueNode*)malloc(sizeof(QueueNode));q->front->next = NULL; }bool IsEmptyQueue(LinkQueue* q) { //判空if (q->front == q->rear)return true;return false; }void InsertQueueNode(LinkQueue* q, RR TempPCB) { //插入結點QueueNode* P = (QueueNode*)malloc(sizeof(QueueNode));P->node = TempPCB;P->next = NULL;q->rear->next = P;q->rear = P; }bool DeleteQueueNode(LinkQueue* q) {QueueNode* P = q->front->next;if (IsEmptyQueue(q)) //為空無法刪除return false;q->front->next = P->next; //使頭結點指向第一個結點的下一個結點if (P == q->rear) //如果P是尾指針,直接使隊頭等于隊尾q->rear = q->front;free(P);return true; }void frontNodeTorear(LinkQueue* q) { //用作把隊首結點移到隊尾QueueNode* P = q->front->next;q->front->next = P->next;P->next = NULL; //首進程摘下來q->rear->next = P; //尾部指針下一個指向摘下來的首進程q->rear = P; }bool cmp(RR a, RR b) //排序輔助函數 {return a.arrivaltime < b.arrivaltime; }

運行實例如下:
時間片為4的情況:

時間片為1的情況:

總結

以上是生活随笔為你收集整理的经典时间片轮转RR算法C语言实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。