Question: simulate a Three-State process model (ready, running and blocked) and a simple process control block structure in c++. Your program will read input and directives

simulate a Three-State process model (ready, running and blocked) and a simple process control block structure in c++. Your program will read input and directives from a file. The input describes a time sequence of events that occur. These are the full set of events you will simulate: Event Description cpu The processor executes for 1 time step the currently running process new A new process is created and put at tail of the ready queue done The currently running process has nished wait X The currently running process has done an I/O operation and is waiting on event X event X Event X has occurred, the process waiting on that event should be made ready. use the following psudo code.

* @description Implement a simulation of a basic 3 process state system

* Ready, Running, Blocked. Simulation includes a round-robin scheduler

* with time slice scheduling. Need to implement a basic Process

* Control Block (PCB) in order to implement the round robin scheduler.

* Program will also have ready queues, and possible queues or other

* structures to keep track of blocked processes and the events they

* are waiting on.

*/

#include

#include

#include

#include

using namespace std;

int CurrentSystemTime = 1;

int nextProcess;

enum ProcessState;

{

NEW,

READY,

RUNNING,

BLOCKED,

DONE

};

struct ProcessControlBlock

{

int pid;

int start;

int timeSliceQuantumsUsed

ProcessState state;

int event;

};

ProcessControlBlock processTable[100];

/** The process simulator.

* The main loop for running a simulation. We read simulation

* events from a file

*

* @param simfilename The name of the file (e.g. simulaiton-01.sim) to open

* and read simulated event sequence from.

* @param timeSliceQuantum The value to be used for system time slicing preemption

* for this simulation.

*/

void runSimulation(char* simfilename, int timeSliceQuantum)

{

ifstream simeventsfile(simfilename);

string command;

int eventnum;

if (!simeventsfile.is_open())

{

cout << "Error: could not open simulator events file: " << simfilename << endl;

exit(1);

}

while (!simeventsfile.eof())

{

simeventsfile >> command;

// Handle the next simulated event we just read from the

// simulation event file

if (command == "cpu")

{

cout << " cpu: simulate a cpu cycle here" << endl;

}

else if (command == "new")

{

cout << " new: simulate creation of new process here" << endl;

ProcessControlBlock newProcess;

newProcess.pid = nextProcessID;

nextProcessID++;

newProcess.state = NEW;

processTable[newProcess.pid] = newProcess;

}

else if (command == "done")

{

cout << " done: simulate termination of currently running process here" << endl;

}

else if (command == "wait")

{

simeventsfile >> eventnum;

cout << " wait: eventnum: " << eventnum <<

" simulate event blocked and waiting" << endl;

}

else if (command == "event")

{

simeventsfile >> eventnum;

cout << " event: eventnum: " << eventnum <<

" simulate event occurring possibly making some processes ready" << endl;

}

else if (command == "exit")

{

// we use an explicit indicator to ensure simulation exits correctly

break;

}

else

{

cout << " ERROR: unknown command: " << command << endl;

exit(0);

}

}

simeventsfile.close();

}

/** Main entry point of simulator

* The main entry point of the process simulator. We simply set up

* and initialize the environment, then call the appropriate function

* to begin the simulation. We expect a single command line argument

* which is the name of the simulation event file to process.

*

* @param argc The argument count

* @param argv The command line argument values. We expect argv[1] to be the

* name of a file in the current directory holding process events

* to simulate.

*/

int main(int argc, char** argv)

{

int timeSliceQuantum = 0;

// validate command line arguments

if (argc != 3)

{

cout << "Error: expecting event file as first command line parameter and time slice quantum as second" << endl;

cout << "Usage: " << argv[0] << " simeventfile.sim time_slice" << endl;

exit(1);

}

// Assume second command line argument is the time slice quantum and parse it

timeSliceQuantum = atoi(argv[2]);

if (timeSliceQuantum <= 0)

{

cout << "Error: invalid time slice quantum received: " << timeSliceQuantum << endl;

exit(1);

}

// Invoke the function to actually run the simulation

runSimulation(argv[1], timeSliceQuantum);

// if don't want to use command line do following.

// need to recompile by hand since file

// name to get simulated events from is hard coded

//runSimulation("simulation-01.sim", 5);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!