-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
153 lines (135 loc) · 3.66 KB
/
app.cpp
File metadata and controls
153 lines (135 loc) · 3.66 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
#include "./FCFS.h"
#include "./SJF.h"
#include "./RR.h"
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <sstream>
using namespace std;
void completionTime(process *p)
{
// calculating completion time
p->completion_time = p->start_time + p->burst_time;
}
void waitingTime(process *process)
{
// calculating waiting time
process->waiting_time = process->turnaround_time - process->burst_time;
}
void turnAround(process *process)
{
// calculating turn around time
process->turnaround_time = process->completion_time - process->arrival_time;
}
void responseTime(process *process)
{
// calculating response time
process->response_time = process->start_time - process->arrival_time;
}
// args options
enum Sched_Alg
{
FCFS,
SJF,
RR,
ALL,
INVALID
};
// retreiving args
Sched_Alg op_return(char *str)
{
if (strcmp(str, "-fcfs") == 0)
return FCFS;
else if (strcmp(str, "-sjf") == 0)
return SJF;
else if (strcmp(str, "-rr") == 0)
return RR;
else if (strcmp(str, "-all") == 0)
return ALL;
return INVALID;
}
// file reading
void readFile(vector<process *> *processes)
{
ofstream f;
string line;
// given file in canvas
std::ifstream infile("processes.txt");
if (infile.is_open())
{
// loop through the file and get every line
while (getline(infile, line))
{
std::istringstream iss(line);
int pid, burst_time, arrival_time;
// pushing the numbers to the above vars
// as the arrangment explaind in the assignment specs
if (!(iss >> pid >> burst_time >> arrival_time))
{
break;
}
// allocate new process
process *p = (process *)malloc(sizeof(process));
p->pid = pid, p->burst_time = burst_time, p->arrival_time = arrival_time;
// push process to the processes vector
processes->push_back(p);
}
}
f.close();
}
int main(int argc, char *argv[])
{
vector<process *> processes;
readFile(&processes);
int time_quantum = 2;
float context_switch = 0.1;
// help menu
string help_menu = "Choose one of the following algorithms as an argument:\n\t-all\t\tall algorithms\n\t-fcfs\t\tFirst-Come-First-Serve\n\t-sjf\t\tShortest-Job-First\n\t-rr\t\tRound-Robin";
// check if argument are given then enter the switch statement
if (argc > 1)
{
switch (op_return(argv[1]))
{
case FCFS:
cout << "*********[FCFS]*********\n"
<< endl;
fcfs(&processes);
break;
case SJF:
cout << "*********[SJF]*********\n"
<< endl;
sjf(&processes);
break;
case RR:
cout << "*********[RR]*********\n"
<< endl;
rr(&processes, time_quantum, context_switch);
break;
case ALL:
cout << "\n*********[FCFS]*********\n"
<< endl;
fcfs(&processes);
cout << "\n*********[SJF]*********\n"
<< endl;
sjf(&processes);
cout << "\n*********[RR]*********\n"
<< endl;
rr(&processes, time_quantum, context_switch);
break;
case INVALID:
cout << help_menu << endl;
break;
default:
break;
}
}
else
{
cout << help_menu << endl;
}
// free allocated memory for processes
for (unsigned int i = 0; i < processes.size(); i++)
free(processes[i]);
return 0;
}