-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsched.cpp
More file actions
179 lines (149 loc) · 4.69 KB
/
sched.cpp
File metadata and controls
179 lines (149 loc) · 4.69 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define VVP vector<pair<int,pair<int,int> > >
#define tup pair<int,pair<int,int> >
#define ArvlTime(pno) process[(pno)].second.first
#define PID(pno) process[(pno)].first
#define BurstTime(pno) process[(pno)].second.second
using namespace std;
//------- Function Declarations --------
void Sched_FCFS(VVP &process);
void Sched_RR(VVP &process, int time_quantom);
void Sched_SJF(VVP &process);
int string2number(char *s);
bool FCFSsort(const tup &p1, const tup &p2);
bool SJFsort(const tup &p1, const tup &p2);
//--------------------------------------
int main(int argc, char *argv[])
{
if(argc<3)
{
perror("Insufficient Arguments :(\nUsage:\t ./Sched [input_file] [FCFS|RR|SJF] <time_quantom>\nExample:\t ./Sched input.txt FCFS\n\n");
return 0;
}
//taking data from input file stream
ifstream inputs(argv[1]);
/*if(ios::fail())
{
perror("Couldn't open selected file !\nHint: try to open file which exists :)\n\n");
return 0;
}*/
//storing it into vector
VVP process;
int pid,arvl,burst;
while(inputs>>pid>>arvl>>burst)
process.push_back(make_pair(pid,make_pair(arvl,burst)));
//done
//identifying scheduling algorithm
if(strcmp(argv[2],"FCFS")==0)
Sched_FCFS(process);
else if(strcmp(argv[2],"RR")==0)
{
if(argc==4)
Sched_RR(process,string2number(argv[3]));
else
perror("You have to enter the value of time_quantom for RR\nUsage: ./Sched [input_file] [FCFS|RR|SJF] <time_quantom>\n\n");
}
else if(strcmp(argv[2],"SJF")==0)
Sched_SJF(process);
else
perror("Enter Valid Scheduling algorithm name\n\n");
//done
return 0;
}
void Sched_FCFS(VVP &process)
{
sort(process.begin(),process.end(),FCFSsort);
int sys_time=0;
int pno=0;
for(;;sys_time++)
{
if(ArvlTime(pno) <= sys_time && BurstTime(pno)>0)
{
cout<<"<system time "<<sys_time<<"> process "<<PID(pno)<<" is running\n";
BurstTime(pno)--;
}
else if(BurstTime(pno)==0)
{
cout<<"<system time "<<sys_time<<"> process "<<PID(pno)<<" is Finished...\n";
pno++;
if(pno<process.size())
{
cout<<"<system time "<<sys_time<<"> process "<<PID(pno)<<" is running\n";
BurstTime(pno)--;
}
else break;
}
else
cout<<"<system time "<<sys_time<<"> --- CPU idle ---\n";
}
cout<<"<system time "<<sys_time<<"> All Process are done now.";
}
void Sched_RR(VVP &process,int time_quantom)
{
}
void Sched_SJF(VVP &process)
{
sort(process.begin(),process.end(),SJFsort);
int sys_time=0;
int pno=0;
int n = process.size();
for(;;sys_time++)
{
if(ArvlTime(pno) <= sys_time && BurstTime(pno)>0)
{
cout<<"<system time "<<sys_time<<"> process "<<PID(pno)<<" is running\n";
BurstTime(pno)--;
}
else if(BurstTime(pno)==0)
{
cout<<"<system time "<<sys_time<<"> process "<<PID(pno)<<" is Finished...\n";
pno++;
for(int i=pno;i<n;i++)
{
if(ArvlTime(i)<sys_time)
ArvlTime(i)=sys_time; //bringing up the arrival time so to sort it later
}
sort(process.begin()+pno,process.end(),SJFsort);
if(pno<n)
{
cout<<"<system time "<<sys_time<<"> process "<<PID(pno)<<" is running\n";
BurstTime(pno)--;
}
else break;
}
else
cout<<"<system time "<<sys_time<<"> --- CPU idle ---\n";
}
cout<<"<system time "<<sys_time<<"> All Process are done now.";
}
bool FCFSsort(const tup &p1, const tup &p2)
{
return (p1.second.first<p2.second.first) ||
(p1.second.first==p2.second.first && p1.first<p2.first);
}
bool SJFsort(const tup &p1, const tup &p2)
{
return (p1.second.first<p2.second.first) ||
(p1.second.first==p2.second.first && p1.second.second<p2.second.second);
}
int string2number(char *s)
{
int number=0;
int len = strlen(s);
int place=1;
for(int i=len-1;i>=0;i--)
{
if(s[i]<'0' || s[i]>'9')
{
perror("Please Enter a valid Numerical Value for time_quantom.\n");
exit(1);
}
number += (s[i]-'0')*place;
place *= 10;
}
return number;
}