-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.cpp
142 lines (111 loc) · 4.45 KB
/
scheduler.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
#include "clkUtilities.h"
#include "schedulerUtilities.h"
#include "RRScheduler.h"
#include "HPFScheduler.h"
#include "SRTNScheduler.h"
#include <iostream>
#include <string>
#include <iomanip>
#include "defs.h"
using namespace std;
int main(int argc, char* argv[])
{
bool EndScheduler = false;
string SchAlgo = argv[0]; //Scheduler algorithm passed to the process
Scheduler* scheduler; //Scheduler object
//Create scheduler according to scheduler algorithm
if(SchAlgo == HPF) {
scheduler = new HPFScheduler();
cout <<"Created HPFScheduler\n";
}
else if(SchAlgo == SRTN){
scheduler = new SRTNScheduler();
cout <<"Created SRTNScheduler\n";
}
else {
scheduler = new RRScheduler();
cout <<"Created RRScheduler\n";
}
initQueue(false); //Subscribe to message queue
initClk();
int Clock = -1; //Initial clock time = -1
string status; //Process state
/*
HPF -> scheduler continue when it receives signal from process
SRTN -> invoked when another process pushed in queue
RR -> invoked every quantum
*/
int ProcessState=-1000;
while (1)
{
cout<<"Scheduler Clock "<<getClk()<<endl;
vector<struct processData> PD; //Vector of arrived processes
int end = getData(Clock,PD); //Get processes from message queue
if (end == -1) //If end process is received
EndScheduler = true;
if(PD.size()!=0)
cout<<"count me"<<endl;
//this must be before checking for state or otherwise the code will break
scheduler->pushDataToQueue(PD); //Push received processes in the priority queue
struct PCB Process;
int state = scheduler->getProcess(Process); //Get process from queue
if(state == -1) //If no processes in the scheduler queue block until a new process arrives
{
if (EndScheduler == true)
break; //End scheduler
cout<<"no processes in the queue nothing to be ran"<<endl;
kill(getppid(),SIGIO);
cout<<"Scheduler: waiting because no processes are available"<<endl;
pause();
//cout<<"Schduler: done waiting because either a process has arrived or something went wrong, if no process arrived check the signals"<<endl;
continue;
}
Clock = getClk(); //clock at which process starts running
if(Process.Pid == -1) //Process status = started (if first time to run) or resumed
{
status = "started";
Process.WaitingTime = Clock - Process.PD.ArrivalTime;
}
else if(ProcessState!=LASTPROCESS){
status = "resumed";
Process.WaitingTime += Clock - (Process.WaitingTime + Process.PD.RunningTime - Process.RemainingTime);
}
if(ProcessState!=LASTPROCESS)
{
//cout<<"clock ()() "<<Clock<<endl;
scheduler->logProcessData(Clock,status,Process);
}
ProcessState=scheduler->runProcess(Process);
PD.clear();
//----------------experimental
end = getData(Clock,PD); //Get processes from message queue
if (end == -1) //If end process is received
EndScheduler = true;
scheduler->pushDataToQueue(PD); //Push received processes in the priority queue
//----------------experimental
//cout<<"after run process"<<endl;
int Stop = getClk(); //clock at which process finishes/stops running
Process.RemainingTime -= (Stop - Clock); //subtract running time from the process remaining time
//cout<<" process remaining time "<<Process.RemainingTime<<endl;
if (Process.RemainingTime <= 0) //process finished
{
status = "finished";
scheduler->logProcessData(Stop,status,Process);
int TA = Stop - Process.PD.ArrivalTime; //Turn around
double WTA = (double)TA / Process.PD.RunningTime; //Weighted turn around
cout << " TA " << TA << " WTA " << setprecision(2) << fixed << WTA << endl;
cout<<" queue size " <<scheduler->Size<<endl;
}
else
{ if(ProcessState!=-10)
{
cout<<"down :";
scheduler->logProcessData(Stop,"stopped",Process);
}
scheduler->returnProcessToQueue(Process);
}
// while(Stop==getClk()){};
}
cout << "Scheduler exiting...\n";
return 0;
}