C語言FCFS排程程式


假設我們有n個程序,即P1、P2、P3、……、Pn及其對應的突發時間。任務是使用先來先服務(FCFS) CPU排程演算法找到平均等待時間和平均週轉時間。

什麼是等待時間和週轉時間?

  • 週轉時間是程序提交和完成之間的時間間隔。

    週轉時間 = 程序完成時間 – 程序提交時間

  • 等待時間是週轉時間和突發時間之差

    等待時間 = 週轉時間 – 突發時間

什麼是FCFS排程?

先來先服務(FCFS),也稱為先進先出(FIFO),是一種CPU排程演算法,其中CPU按照程序在就緒佇列中排隊的順序分配給程序。

FCFS遵循非搶佔式排程,這意味著一旦CPU分配給一個程序,它就不會離開CPU,直到程序終止或由於某些I/O中斷而暫停。

示例

假設有四個程序按照P2、P3、P1的順序到達,它們對應的執行時間如下表所示。此外,假設它們的到達時間為0。

程序到達順序執行時間(毫秒)
P1315
P213
P323

甘特圖顯示系統中程序P1、P2和P3的等待時間

如上所示:

程序P2的等待時間為0

程序P3的等待時間為3

程序P1的等待時間為6

平均時間 = (0 + 3 + 6) / 3 = 3 毫秒。

由於我們假設到達時間為0,因此週轉時間和完成時間相同。

示例

Input-:  processes = P1, P2, P3
        Burst time = 5, 8, 12
Output-:
Processes  Burst    Waiting    Turn around
1          5        0           5
2          8        5           13
3          12       13          25
Average Waiting time = 6.000000
Average turn around time = 14.333333

演算法

Start                                                                                                     Step 1-> In function int waitingtime(int proc[], int n, int burst_time[], int wait_time[])
   Set wait_time[0] = 0
   Loop For i = 1 and i < n and i++
      Set wait_time[i] = burst_time[i-1] + wait_time[i-1]
   End For
Step 2-> In function int turnaroundtime( int proc[], int n, int burst_time[], int wait_time[], int tat[])
   Loop For  i = 0 and i < n and i++
      Set tat[i] = burst_time[i] + wait_time[i]
   End For
Step 3-> In function int avgtime( int proc[], int n, int burst_time[])
   Declare and initialize wait_time[n], tat[n], total_wt = 0, total_tat = 0;
   Call waitingtime(proc, n, burst_time, wait_time)
   Call turnaroundtime(proc, n, burst_time, wait_time, tat)
   Loop For  i=0 and i<n and i++
      Set total_wt = total_wt + wait_time[i]
      Set total_tat = total_tat + tat[i]
      Print process number, burstime wait time and turnaround time
   End For
   Print "Average waiting time =i.e. total_wt / n
   Print "Average turn around time = i.e. total_tat / n
Step 4-> In int main()
   Declare the input int proc[] = { 1, 2, 3}
   Declare and initialize n = sizeof proc / sizeof proc[0]
   Declare and initialize burst_time[] = {10, 5, 8}
   Call avgtime(proc, n, burst_time)
Stop

示例

 線上演示

#include <stdio.h>
// Function to find the waiting time for all processes
int waitingtime(int proc[], int n,
int burst_time[], int wait_time[]) {
   // waiting time for first process is 0
   wait_time[0] = 0;
   // calculating waiting time
   for (int i = 1; i < n ; i++ )
   wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
   return 0;
}
// Function to calculate turn around time
int turnaroundtime( int proc[], int n,
int burst_time[], int wait_time[], int tat[]) {
   // calculating turnaround time by adding
   // burst_time[i] + wait_time[i]
   int i;
   for ( i = 0; i < n ; i++)
   tat[i] = burst_time[i] + wait_time[i];
   return 0;
}
//Function to calculate average time
int avgtime( int proc[], int n, int burst_time[]) {
   int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
   int i;
   //Function to find waiting time of all processes
   waitingtime(proc, n, burst_time, wait_time);
   //Function to find turn around time for all processes
   turnaroundtime(proc, n, burst_time, wait_time, tat);
   //Display processes along with all details
   printf("Processes  Burst   Waiting Turn around 
"
);    // Calculate total waiting time and total turn    // around time    for ( i=0; i<n; i++) {       total_wt = total_wt + wait_time[i];       total_tat = total_tat + tat[i];       printf(" %d\t  %d\t\t %d \t%d
"
, i+1, burst_time[i], wait_time[i], tat[i]);    }    printf("Average waiting time = %f
"
, (float)total_wt / (float)n);    printf("Average turn around time = %f
"
, (float)total_tat / (float)n);    return 0; } // main function int main() {    //process id's    int proc[] = { 1, 2, 3};    int n = sizeof proc / sizeof proc[0];    //Burst time of all processes    int burst_time[] = {5, 8, 12};    avgtime(proc, n, burst_time);    return 0; }

輸出

Processes  Burst    Waiting    Turn around
1          5        0           5
2          8        5           13
3          12       13          25
Average Waiting time = 6.000000
Average turn around time = 14.333333

更新於:2023年9月2日

49K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.