生活随笔
收集整理的這篇文章主要介紹了
经典时间片轮转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
); 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
) {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
); 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; }else if (0<q_ready
->front
->next
->node
.Servicetime
) {if (x
< t
) {;}else {x
= 0; if (!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
) 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语言实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。