-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJN.cpp
More file actions
108 lines (100 loc) · 2.88 KB
/
Copy pathSJN.cpp
File metadata and controls
108 lines (100 loc) · 2.88 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace std;
void sjf(int p, int process[], int arrival[], int burst[], int wait[], int turnaround[], int completion[])
{
int i, j, pos, temp;
int total = 0, t = 0, min;
float avg_wt, avg_tat;
// Initialize wait and turnaround times
for (i = 0; i < p; i++)
{
wait[i] = 0;
turnaround[i] = 0;
completion[i] = -1;
}
// Sort processes based on arrival time
for (i = 0; i < p - 1; i++)
{
pos = i;
for (j = i + 1; j < p; j++)
{
if (arrival[j] < arrival[pos])
{
pos = j;
}
}
temp = process[i];
process[i] = process[pos];
process[pos] = temp;
temp = arrival[i];
arrival[i] = arrival[pos];
arrival[pos] = temp;
temp = burst[i];
burst[i] = burst[pos];
burst[pos] = temp;
}
// Calculate completion times, waiting times, and turnaround times
for (i = 0; i < p; i++)
{
min = 100000;
pos = -1;
for (j = 0; j < p; j++)
{
if (arrival[j] <= t && completion[j] == -1)
{
if (burst[j] < min)
{
min = burst[j];
pos = j;
}
}
}
if (pos != -1)
{
completion[pos] = t + burst[pos];
wait[pos] = completion[pos] - arrival[pos] - burst[pos];
if (wait[pos] < 0)
{
wait[pos] = 0;
}
turnaround[pos] = completion[pos] - arrival[pos];
total += turnaround[pos];
t = completion[pos];
}
else
{
t++;
}
}
// Calculate average waiting time and turnaround time
avg_wt = (float)total / p;
avg_tat = (float)total / p;
// Display output
cout << "Process\tArrival Time\tBurst Time\tWaiting Time\tCompletion Time\tTurnaround Time\n";
for (i = 0; i < p; i++)
{
cout << process[i] << "\t" << arrival[i] << "\t\t" << burst[i] << "\t\t" << wait[i] << "\t\t" << completion[i] << "\t\t" << turnaround[i] << "\n";
}
cout << "\nAverage Waiting Time: " << avg_wt << "\n";
cout << "Average Turnaround Time: " << avg_tat << "\n";
cout << "\nThroughput: " << (float)p / t << "\n";
cout << "CPU Utilization: " << (float)total / t << "\n";
}
int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;
int p = n;
int process[n], arrival[n], burst[n];
for (int i = 0; i < n; i++)
{
cout << "Enter the arrival time and burst time for process " << i + 1 << ": ";
cin >> arrival[i] >> burst[i];
process[i] = i + 1;
}
int wait[p], turnaround[p], completion[p];
sjf(p, process, arrival, burst, wait, turnaround, completion);
// rest of the SJF code here
return 0;
}