生活随笔
收集整理的這篇文章主要介紹了
SRTF最短剩余时间优先调度C语言实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
什么是SRTF?
SRTF=Shortest Remaining Time First
實現思想:當新進程進入就緒隊列時,如果它需要處理的時間比當前進程處理時間短,則將當前進程掛起,把CPU分配給新進程。
SRTF就是SJF的搶占式版本。
#include<stdio.h>
#include<stdlib.h>
typedef struct PCB
{int id
;int arrivetime
;int runtime
;int counttime
;struct PCB
* next
;
}*task
, pcb
;
pcb
* creattask(int x
, int y
, int z
) {task newtask
= (task
)malloc(sizeof(pcb
));newtask
->id
= x
;newtask
->arrivetime
= y
;newtask
->runtime
= z
;newtask
->counttime
= z
;newtask
->next
= NULL;return newtask
;
}
void deltask(pcb
* n
, int x
) {task d
;if (n
!= NULL) {while (n
->next
->id
!= x
) {n
= n
->next
;}d
= n
->next
;n
->next
= d
->next
;free(d
);}
}
void count(pcb
* n
, int t
) { pcb
* q
, * p
;int qp
=0; int temp
= t
; float time
= 0; float zt
= 0, dt
= 0; while (t
!= 0) {p
= n
->next
;q
= p
;while (p
!= NULL) { if (p
->arrivetime
< q
->arrivetime
) {q
= p
;}p
= p
->next
;}p
= n
->next
;while (p
!= NULL) { if ((p
->arrivetime
<= time
|| p
->arrivetime
== q
->arrivetime
) && p
->counttime
< q
->counttime
)q
= p
;p
= p
->next
;}q
->counttime
--;if(qp
==0||qp
!=q
->id
)printf("當前執行的任務序號為 %d \n", q
->id
);qp
= q
->id
;if (time
< q
->arrivetime
)time
= q
->arrivetime
;time
++;if (q
->counttime
== 0) {printf("任務%d結束\n", q
->id
);printf("該任務周轉時間為 %.0f \n", time
- q
->arrivetime
);zt
= zt
+ time
- q
->arrivetime
;printf("該任務帶權周轉時間為 %.2f \n\n", (time
- q
->arrivetime
) / q
->runtime
);dt
= dt
+ (time
- q
->arrivetime
) / q
->runtime
;deltask(n
, q
->id
);--t
;}}printf("\n");printf("平均周轉時間為 %.2f \n", zt
/ temp
);printf("平均帶權周轉時間為 %.2f \n", dt
/ temp
);
}
int main() {int n
, i
, y
, z
;task tail
= NULL;task head
= NULL;printf("請輸入任務數量:");scanf("%d", &n
);tail
= (task
)malloc(sizeof(pcb
));head
= tail
;for (i
= 1; i
<= n
; i
++) {printf("請輸入%d號任務的到達時間、運行時間:", i
);scanf("%d%d", &y
, &z
);tail
->next
= creattask(i
, y
, z
);tail
= tail
->next
;}count(head
, n
);
}
SRTF的特點:
優點:保證新的短作業一進入系統就能很快的到服務,平均等待時間短。
缺點:為保證進程斷點現場,統計進程剩余時間增加了系統開銷,不利于長作業。
作業調度用得少,進程調度用得多。
總結
以上是生活随笔為你收集整理的SRTF最短剩余时间优先调度C语言实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。