CS 421 Objectives: Write a program to compare the performance of the Round Robin, non-preemptive Shortest Job First, and SRTF scheduling algorithms. Details: Input: A file of processes and related CPU...

1 answer below »
Simulation of Round Robin, non-preemptive Shortest JobFirst, and SRTF scheduling algorithms.


CS 421 Objectives: Write a program to compare the performance of the Round Robin, non-preemptive Shortest Job First, and SRTF scheduling algorithms. Details: Input: A file of processes and related CPU burst times and arrival times. There will be no header in the file and each line will be of the form, "" with spaces between each field. Example: A 10 0 B 1 1 C 2 3 D 1 0 E 5 1 You should read the data into a data structure. Then you will simulate the behavior of the 3 scheduling algorithms on the data, one at a time. For each algorithm, your program will need to print out a sort of vertical Gantt chart followed by some summary statistics. Gantt chart: First print the name of the scheduling algorithm, then each time a process is scheduled, print out time of the scheduling decision, the processID, and the reason for the context switch. When the last process completes, print the end time, and "Complete". The 3 possible reasons for a context switch are: Process terminated Quantum expired Process preempted by process with shorter burst time Example: SJF Scheduling 0 D Process terminated 1 B Process terminated 2 E Process terminated 7 C Process terminated 9 A Process terminated 19 Complete Summary statistics: Then print out the turnaround time and waiting time for each process and the average turnaround time and waiting time. Example: Process ID Turnaround Time Waiting Time A 19 9 B 1 0 C 6 4 D 1 0 E 6 1 Average 33/5 = 6.6 14/5 = 2.8 Then, repeat for the other 2 scheduling algorithms. If 2 processes have the same burst length (in SJF) or arrive at the same time (in RR), handle them in alphabetical order. If a process A is preempted at the same time a new process B arrives, put process A in the queue before B (essentially giving running processes a bit higher priority than new processes.) You might use an ordered queue to handle prioritization in SJF and SRTF. Use a quantum of 4 for the Round-Robin scheduler. Assume no preemption in the shortest-job first scheduler. Of course, there is preemption in the RR and SRTF schedulers. How to get started: Note that this program is essentially a simulation and hence is supposed to simulate the behavior of a real system implementing the scheduling algorithms on a set of processes. As a result, I would suggest implementing versions of the real data structures that an OS would use here, namely a job queue (a queue of all processes in the system) and a ready queue (the set of jobs ready to run.) You could then update the ready queue every (simulated) second by moving any jobs which have arrived from the job queue to the ready queue. Then your scheduling algorithm would examine the ready queue to choose the next process to run. You might maintain a process’ burst time or remaining burst time as part of the process’ record in the event queue as well so that your scheduler has all the information it needs right there. Remember to do one piece at a time and make sure that you always have something to turn in before moving on to the next piece. Note that this would include the corresponding output. How can you check if your output is correct? (Because you would want to check, right?) You can easily work out the scheduling order by hand. Requirements: You must write an C++ program which simulates all 3 scheduling algorithms, not 3 separate programs. yunl Highlight Untitled A 1 0 B 4 0 C 3 2 D 1 2 E 12 3 F 1 4 G 3 5 H 2 5
Answered Same DayOct 13, 2021

Answer To: CS 421 Objectives: Write a program to compare the performance of the Round Robin, non-preemptive...

Kushal answered on Oct 15 2021
143 Votes
CODE
#include
#include
#include
#include
using namespace std;
class process {
public:
pid_t p_no = 0;
time_t start_AT = 0, AT = 0,
BT_left = 0, BT = 0, temp_BT = 0,
CT = 0, TAT = 0, WT = 0, RT = 0;
int priority = 0;


void set_CT(time_t time)
{
CT = time;
set_TAT();
set_WT();
}

void set_TAT()
{
TAT = CT - start_AT;
}

void set_WT()
{
WT = TAT - BT;
}
void P_set()
{
start_AT = AT;
BT_left = BT;
}
void set_RT(time_t time)
{
RT = time - start_AT;
}

friend bool operator<(const process& a, const process& b)
{
return a.AT > b.AT;
}
};
process pop_index(priority_queue* main_queue, int index)
{
priority_queue rm_index;
int i;
process p;
switch (index) {
case 0:
p = (*main_queue).top();
(*main_queue).pop();
break;
default:
for (i = 0; i < index; i++) {
rm_index.push((*main_queue).top());
(*main_queue).pop();
}
p = (*main_queue).top();
(*main_queue).pop();
while (!(*main_queue).empty()) {
rm_index.push((*main_queue).top());
(*main_queue).pop();
}
(*main_queue) = rm_index;
break;
}
return p;
}
time_t min_BT(priority_queue main_queue, time_t clock)
{
time_t min = 0;
while (!main_queue.empty() && main_queue.top().AT <= clock) {
if (min == 0 || min > main_queue.top().BT_left)
min = main_queue.top().BT_left;
main_queue.pop();
}
return min;
}
int min_BT_index(priority_queue main_queue, time_t limit)
{
int index, i = 0;
time_t min = 0;
while (!main_queue.empty() && main_queue.top().AT <= limit) {
if (min == 0 || main_queue.top().BT_left < min) {
min = main_queue.top().BT_left;
index = i;
}
main_queue.pop();
i++;
}
return index;
}

priority_queue SJF_P_run(priority_queue ready_queue,
queue* gantt)
{
priority_queue completion_queue;
process p;
time_t clock = 0;

while (!ready_queue.empty()) {
while (clock < ready_queue.top().AT) {
p.temp_BT++;
clock++;
}
if (p.temp_BT > 0) {
p.p_no = -1;
p.CT = clock;
(*gantt).push(p);
}
p = pop_index(&ready_queue, min_BT_index(ready_queue, clock));
if (p.AT == p.start_AT)
p.set_RT(clock);
while (p.BT_left > 0 && (ready_queue.empty()
|| clock < ready_queue.top().AT
|| p.BT_left <= min_BT(ready_queue, clock))) {
p.BT_left--;
p.temp_BT++;
clock++;
}
if (p.BT_left == 0) {
p.AT = p.start_AT;
p.set_CT(clock);
(*gantt).push(p);
p.temp_BT = 0;
completion_queue.push(p);
}
else {
p.AT = clock;
p.CT = clock;
(*gantt).push(p);
p.temp_BT = 0;
ready_queue.push(p);
}
}

return completion_queue;
}
priority_queue SJF_NP_run(priority_queue ready_queue,
queue* gantt)
{
priority_queue completion_queue;
process p;
time_t clock = 0;
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here