forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst Come, First Serve – CPU Scheduling | (Non-preemptive)
77 lines (63 loc) · 1.41 KB
/
First Come, First Serve – CPU Scheduling | (Non-preemptive)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// C++ program to Calculate Waiting
// Time for given Processes
#include <iostream>
using namespace std;
// Function to Calculate waiting time
// and average waiting time
void CalculateWaitingTime(int at[],
int bt[], int N)
{
// Declare the array for waiting
// time
int wt[N];
// Waiting time for first process
// is 0
wt[0] = 0;
// Print waiting time process 1
cout << "PN\t\tAT\t\t"
<< "BT\t\tWT\n\n";
cout << "1"
<< "\t\t" << at[0] << "\t\t"
<< bt[0] << "\t\t" << wt[0] << endl;
// Calculating waiting time for
// each process from the given
// formula
for (int i = 1; i < 5; i++) {
wt[i] = (at[i - 1] + bt[i - 1]
+ wt[i - 1]) - at[i];
// Print the waiting time for
// each process
cout << i + 1 << "\t\t" << at[i]
<< "\t\t" << bt[i] << "\t\t"
<< wt[i] << endl;
}
// Declare variable to calculate
// average
float average;
float sum = 0;
// Loop to calculate sum of all
// waiting time
for (int i = 0; i < 5; i++) {
sum = sum + wt[i];
}
// Find average waiting time
// by dividing it by no. of process
average = sum / 5;
// Print Average Waiting Time
cout << "\nAverage waiting time = "
<< average;
}
// Driver code
int main()
{
// Number of process
int N = 5;
// Array for Arrival time
int at[] = { 0, 1, 2, 3, 4 };
// Array for Burst Time
int bt[] = { 4, 3, 1, 2, 5 };
// Function call to find
// waiting time
CalculateWaitingTime(at, bt, N);
return 0;
}