FCFS调度的C程序

FCFS调度的C程序

给定n个进程,即P1,P2,P3,…,Pn以及它们对应的执行时间。任务是使用FCFS CPU调度算法来计算平均等待时间和平均周转时间。

等待时间和周转时间是什么?

  • 周转时间是进程提交和完成之间的时间间隔。

周转时间 = 进程完成时间 – 进程提交时间

  • 等待时间是周转时间与执行时间的差。

等待时间 = 周转时间 – 执行时间

什么是FCFS调度?

先到先服务(FCFS)也被称为先进先出(FIFO),是一种CPU调度算法,根据进程在就绪队列中排队的顺序分配CPU。

FCFS采用非抢占调度,意味着一旦CPU被分配给一个进程,它就不会离开CPU,直到该进程终止或由于某些I/O中断而停止。

示例

假设有四个进程按P2,P3,P1的顺序到达,其对应的执行时间如下表所示。同时,将它们的到达时间设为0。

Process Order of arrival Execution time in msec
P1 3 15
P2 1 3
P3 2 3

显示系统中进程P1、P2和P3的等待时间的甘特图

FCFS调度的C程序

如上所示,

进程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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程