-
Notifications
You must be signed in to change notification settings - Fork 1
/
Practical6.cpp
145 lines (129 loc) · 3.3 KB
/
Practical6.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cstdlib>
#ifdef __linux__
#define CLRSCR "clear"
#else
#define CLRSCR "cls"
#endif
using namespace std;
class schedulerSimulator;
class process
{
int arrivalTime;
int burstTime;
int processId;
int waitingTime;
int turnAroundTime;
friend class schedulerSimulator;
process &operator=(const process &temp);
public:
process();
void run();
void getProcessInfo();
};
class schedulerSimulator
{
const int numberOfProcesses;
process *queue;
void schedule();
public:
schedulerSimulator(int numberOfProcesses);
~schedulerSimulator();
void simulate();
};
int main()
{
system(CLRSCR);
int num;
cout << "Enter number of processes to simulate: ";
cin >> num;
schedulerSimulator schd(num);
schd.simulate();
cout << "Press enter to exit..." << endl;
cin.ignore();
cin.get();
}
process::process()
{
this->arrivalTime = 0;
this->burstTime = 0;
}
void process::getProcessInfo()
{
cout << "Enter process id: ";
cin >> this->processId;
cout << "Enter arrival time: ";
cin >> this->arrivalTime;
cout << "Enter burst time: ";
cin >> this->burstTime;
}
void process::run()
{
cout << "PID: " << processId
<< "\t\tburst time : " << burstTime
<< "\t\twaiting time : " << waitingTime
<< "\t\tturn around time : " << turnAroundTime
<< endl;
}
schedulerSimulator::schedulerSimulator(int numOfProcesses) : numberOfProcesses(numOfProcesses)
{
this->queue = new process[numberOfProcesses];
for (int i = 0; i < numberOfProcesses; i++)
{
cout << "Enter details of process " << i + 1 << endl;
this->queue[i].getProcessInfo();
}
}
schedulerSimulator::~schedulerSimulator()
{
delete[] this->queue;
}
void schedulerSimulator::simulate()
{
int avgWaitingTime = 0;
int avgTurnAroundTime = 0;
schedule();
queue[0].waitingTime = 0;
queue[0].turnAroundTime = queue[0].burstTime;
for (int i = 1; i < numberOfProcesses; i++)
{
queue[i].waitingTime = queue[i - 1].turnAroundTime;
queue[i].turnAroundTime = queue[i].waitingTime + queue[i].burstTime;
}
system(CLRSCR);
cout<<"FCFS scheduling"<<endl<<endl;
for (int i = 0; i < numberOfProcesses; i++)
{
this->queue[i].run();
avgWaitingTime += this->queue[i].waitingTime;
avgTurnAroundTime += this->queue[i].turnAroundTime;
}
cout << endl
<< "Average turn around time: " << avgTurnAroundTime / numberOfProcesses << endl
<< "Average waiting time: " << avgWaitingTime / numberOfProcesses << endl;
}
void schedulerSimulator::schedule()
{
process temp;
for (int i = 0; i < numberOfProcesses; i++)
{
for (int j = 0; j < numberOfProcesses - 1; j++)
{
if (this->queue[j].arrivalTime > this->queue[j + 1].arrivalTime)
{
temp = this->queue[j];
this->queue[j] = this->queue[j + 1];
this->queue[j + 1] = temp;
}
}
}
}
process &process::operator=(const process &temp)
{
this->processId = temp.processId;
this->burstTime = temp.burstTime;
this->turnAroundTime = temp.turnAroundTime;
this->waitingTime = temp.waitingTime;
this->arrivalTime = temp.arrivalTime;
return *this;
}