Mohd monis choudhary 2023-310-163
Operating Systems Lab: Assignment 2
Learning Objective:
To understand how to code a simple CPU Scheduling Algorithm.
Task:
Write a C++ program to simulate FCFS (First Come First Serve) CPU scheduling algorithm to find average
waiting time for the below problem:
Process Burst Time
P0 24
P1 3
P2 3
Program Input:
Number of Processes and Burst Time for each process.
Program Output:
Average Waiting Time and Turnaround Time.
C++ Code for FCFS Scheduling:
#include <iostream>
using namespace std;
void findWaitingTime(int processes[], int n, int bt[], int wt[]) {
wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = bt[i - 1] + wt[i - 1];
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}
void findAverageTime(int processes[], int n, int bt[]) {
int wt[n], tat[n];
float total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Processes Burst Time Waiting Time Turnaround Time\n";
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
Mohd monis choudhary 2023-310-163
cout << " " << i << "\t\t" << bt[i] << "\t " << wt[i] << "\t\t " << tat[i] <<
endl;
}
cout << "\nAverage waiting time = " << (total_wt / n);
cout << "\nAverage turnaround time = " << (total_tat / n) << endl;
}
int main() {
int processes[] = {0, 1, 2};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {24, 3, 3};
findAverageTime(processes, n, burst_time);
return 0;
}
Output:
Final Computed Waiting and Turnaround Times:
Average waiting time = 17.0
Average turnaround time = 27.0