2.1 Complete “fifo.cpp” for a FIFO scheduling algorithm. Instruction: 1. Function “RunProcess()” is to simulate the the

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

2.1 Complete “fifo.cpp” for a FIFO scheduling algorithm. Instruction: 1. Function “RunProcess()” is to simulate the the

Post by answerhappygod »

2.1 Complete “fifo.cpp” for a FIFO scheduling algorithm.Instruction:
1. Function “RunProcess()” is to simulate the the run time of aprocess. It changes the state from ’W’ (waiting) to ’R’ and recordthe start time and the finish time.
2. Function “Schedule()” has a while loop. It retrieves aprocess from the head and runs it. Note that the head processalways has a earlier arriving time than the remainingprocesses.
3. You need to complete the missing part of function“CreateProcess”: • Suppose that the list is empty at the beginning(head==NULL). • For every “new proc”, you need to insert it into acorrect position for FIFO schedule based on the arriving time. •The last process’s next should point to NULL. • You should get asimilar output as Figure 1.
2 1 Complete Fifo Cpp For A Fifo Scheduling Algorithm Instruction 1 Function Runprocess Is To Simulate The The 1
2 1 Complete Fifo Cpp For A Fifo Scheduling Algorithm Instruction 1 Function Runprocess Is To Simulate The The 1 (19.9 KiB) Viewed 34 times
below is the code where the changes must be made in thecreateProcess function
//// main.cpp// FIFO//// Created by Zhimin Gao on 10/3/19.// Copyright © 2019 Zhimin Gao. All rights reserved.//
#include <iostream>#include <chrono>#include "fifo.h"
using namespace std;using namespace std::chrono;
void FIFO::CreateProcess() { cout << "Enter number of process: "; cin >> num_proc; for (int i=1; i<=num_proc; ++i) { PCB * new_proc = NULL; new_proc = new PCB; if(NULL == new_proc) { cerr << "Error dueto allocating memory for new PCB" << endl; return; } cout << "Enter the name of "<< i << "th process: "; cin >> new_proc->name; cout << "Enter the arriving timeof " << i << "th process: "; cin >>new_proc->ArriveTime; cout << "Enter the service timeof " << i << "th process:"; cin >>new_proc->ServiceTime; new_proc->next = NULL; //Insert the new process into thecorrect position so the head arrived earliest, then its next, thenits next's next, then so on.
//TO-DO: Complete your codeshere.
//Set other arrtributes for the newprocess. new_proc->StartTime=0; new_proc->FinshTime=0; new_proc->TurnAroundTime=0; new_proc->state= 'W'; new_proc->RunTime=0; new_proc->NeedTime=0; }}
void FIFO::Schedule() { PCB * current_proc = head; PCB * new_proc = NULL; while(current_proc) { if(current_proc->state == 'W'){ new_proc =current_proc; RunProcess(new_proc); } current_proc =current_proc->next; head = current_proc; free(new_proc); new_proc = NULL; } cout << "Average turn-around time is " <<(double)sum_turn_around_time/(double)num_proc << endl;}
void FIFO::RunProcess(PCB * proc) { //get current time proc->StartTime = time; cout << "[" << proc->StartTime<< "]: Current process " << proc->name << "starts." << endl; time+=proc->ServiceTime; proc->state = 'R'; // Set the currect state torunning proc->FinshTime = time; //Set the finishtime proc->TurnAroundTime = proc->FinshTime -proc->ArriveTime; //Set the whole process time sum_turn_around_time +=(double)proc->TurnAroundTime;}
int main(int argc, const char * argv[]) { FIFO fifo; fifo.CreateProcess(); fifo.Schedule(); return 0;}
//// fifo.h// Programming Assisgnment 2//// Created by Zhimin Gao on 10/3/19.// Copyright © 2019 Zhimin Gao. All rights reserved.//
#ifndef fifo_h#define fifo_h#include "pcb.h"
class FIFO : public Scheduler{private: double sum_turn_around_time; //sum oftotal turn-around time int num_proc; //number of processes PCB * head; //the head of the queue PCB * tail; //the tail of the queue long time; //indicate the current timepublic: FIFO() { sum_turn_around_time = 0; num_proc = 0; head = NULL; tail = NULL; } void CreateProcess(); void Schedule(); void RunProcess(PCB * proc);};
#endif /* fifo_h */
//// pcb.h// Programming Assisgnment 2//// Created by Zhimin Gao on 10/3/19.// Copyright © 2019 Zhimin Gao. All rights reserved.//
#ifndef pcb_h#define pcb_h
#include <iostream>#include <string>using namespace std;
typedef struct PCB_{ string name; //Process's name char state; //Process's state: R=running; W=waiting long ArriveTime; //Process's arrival time long StartTime; //Process's start time long FinshTime; //Process's finish time long ServiceTime; //Process's execution time double TurnAroundTime; //Process's turn-around time
long RunTime; //Time that a process has run long NeedTime; //Time that a process still needs to run struct PCB_ *next; //Points to the next process in list}PCB;
class Scheduler{public: virtual void RunProcess(PCB * proc) = 0; virtual void Schedule() = 0; virtual void CreateProcess() = 0;};
#endif /* pcb_h */
Enter number of process: 2 Enter the name of 1th process: a Enter the arriving time of 1th process: 3 Enter the service time of 1th process:5 Enter the name of 2th process: b Enter the arriving time of 2th process: 1 Enter the service time of 2th process:4 [1]: Current process b starts. [5]: Current process a starts. Average turn-around time is 5.5 Program ended with exit code: 8 Figure 1: Sample output
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply