-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJF.cpp
More file actions
82 lines (72 loc) · 2.5 KB
/
SJF.cpp
File metadata and controls
82 lines (72 loc) · 2.5 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
#include "SJF.h"
void startTime(process *p, int clock_time)
{
// add start time to process
p->start_time = clock_time;
}
void sjf(vector<process *> *processes)
{
// this is for checking the completness of processes
vector<bool> is_completed(processes->size(), false);
// timer for a process
int cpu_timer = 0;
// completed processes counter
unsigned int completed = 0;
while (completed != processes->size())
{
// this is to calculate min and max
int index = -1;
int min = 99999999;
for (unsigned int i = 0; i < processes->size(); i++)
{
// check for processes arrived now or earlier than the timer
if (processes->at(i)->arrival_time <= cpu_timer && !is_completed[i])
{
// look for the shortest arrived process and save its index
if (processes->at(i)->burst_time < min)
{
min = processes->at(i)->burst_time;
index = i;
}
// if two short processes have the same burst time
// checck for the earliest arrived job between the two and save its index
if (processes->at(i)->burst_time == min)
{
if (processes->at(i)->arrival_time < processes->at(index)->arrival_time)
{
min = processes->at(i)->burst_time;
index = i;
}
}
}
}
// if there exists a process do the claculation
// if not increase the timer by 1
if (index != -1)
{
startTime(processes->at(index), cpu_timer);
completionTime(processes->at(index));
turnAround(processes->at(index));
waitingTime(processes->at(index));
responseTime(processes->at(index));
is_completed[index] = true;
completed++;
cpu_timer = processes->at(index)->completion_time;
}
else
{
cpu_timer++;
}
}
/*
printing all neccessery information
*/
cout << "PID "
<< " Waiting time "
<< " Turn around time"
<< endl;
for (unsigned int i = 0; i < processes->size(); i++)
cout << " " << processes->at(i)->pid << "\t" << processes->at(i)->waiting_time << "\t\t" << processes->at(i)->turnaround_time << endl;
cout << "\nCPU clock time = "
<< cpu_timer << endl;
}