C++ ::::: In this assignment you will implement a basic single threaded 3 state processor with the following process states: ready, blocked, and processing. The 2 additional states are notArrived and...

1 answer below »

C++ ::::: In this assignment you will implement a basic single threaded 3 state processor with the following process states: ready, blocked, and processing. The 2 additional states are notArrived and done (these are analogous to the New and Exit states we discussed in class).


Notes:



  • You place a process on the "processor" by marking the processor as unavailable - processorAvailable = false;
    Then the process that is to be running has its processor time incremented for the time units that it is to be running

  • When a process is placed on the processor it should continue to process until it is done or blocked by an IOEvent

  • When a process is complete or blocked, the first ready process in the vector (by index) should be placed on the processor. This is a basic first come, first served scheduler




Processor Flow



  1. Set processes to ready (when they arrive and become unblocked)

  2. Check for thefirstprocess that is ready

  3. "Process" (processorAvailable = false) for 1 time unit (i.e., one while loop iteration)

  4. Repeat 3 until done or blocked (make sure that time isn't standing still for other processes)

  5. Repeat steps 1-4 until all processes are complete




Compiling


Typemakeand press enter


Just make sure your program can compile on the campus machines




Running


./program [file] [sleepDuration]


file - filename for the process information file to use (see Input section below)


sleepDuration - how long the program sleeps after each loop iteration





main.cpp #include "process.h" #include // for sleep #include // for sleep int main(int argc, char* argv[]) { // single thread processor // it's either processing something or it's not bool processorAvailable = true; // vector of processes, fill by reading from a file vector processList; // Do not touch long time = 1; long sleepDuration = 50; string file; stringstream ss; // Do not touch switch(argc) { case 1: file = "procList.txt"; // default input file break; case 2: file = argv[1]; // file given from command line break; case 3: file = argv[1]; // file given ss.str(argv[2]); // sleep duration given ss >> sleepDuration; break; default: cerr < "incorrect="" number="" of="" command="" line="" arguments"="">< endl;="" cout="">< "usage:="" "="">< argv[0]="">< "="" [file]="" [sleepduration]"="">< endl;="" return="" 1;="" break;="" }="" read="" in="" from="" the="" file="" while()="" {="" set="" state="" of="" all="" processes="" that="" should="" be="" ready="" to="" ready="" hint:="" arrival="" time="" and="" processes="" becoming="" unblocked="" select="" the="" next="" process="" that="" should="" be="" given="" to="" the="" processor="" increment="" the="" selected="" process's="" processortime="" set="" state="" of="" the="" process="" based="" on="" the="" following:="" required="" process="" time="" met="" -="" done;="" ioevent="" started="" -="" blocked;="" make="" sure="" to="" track="" the="" time="" that="" processes="" have="" been="" blocked="" you="" can="" do="" update="" this="" just="" about="" anywhere="" as="" long="" as="" it="" gets="" updated="" each="" time="" iteration="" do="" not="" touch="" cout="">< time++="">< '\t';="" do="" not="" touch="" you="" may="" wish="" to="" use="" a="" second="" vector="" of="" processes="" (you="" don't="" need="" to,="" but="" you="" can)="" printprocessstates(processlist);="" change="" processlist="" to="" another="" vector="" of="" processes="" if="" desired="" this_thread::sleep_for(chrono::milliseconds(sleepduration));="" do="" not="" touch="" }="" return="" 0;="" }="" makefile="" cxx="g++" flags="-W" -wall="" -wextra="" -wpedantic="" -werror="" -std="c++11" libraries="-lpthread" .phony:="" default="" run="" default:="" run="" run:="" ${cxx}="" ${flags}="" *.cpp="" ${libraries}="" -o="" program="" clean:="" -@rm="" -rf="" *.o="" program="" core="" process.cpp="" #include="" "process.h"="" void="" initprocesssetfromfile(const="" string&="" filename,="">& processVector) { stringstream ss; ifstream in(fileName.c_str()); string line, strItem; Process proc; IOEvent io; processVector.clear(); if(!in.good()) { cerr < "initprocesssetfromfile="" error="" unable="" to="" open="" file="" \""="">< filename="">< "\""="">< endl;="" return;="" }="" processvector.reserve(20);="" while(getline(in,="" line))="" {="" ss.clear();="" ss.str(line);="" ss="">> proc.arrivalTime; ss >> proc.reqProcessorTime; proc.ioEvents.clear(); proc.ioEvents.reserve(20); while(ss >> io.time) { ss >> io.duration; proc.ioEvents.push_back(io); } processVector.push_back(proc); } } void printProcessStates(vector& processVector) { char stateChar; for(auto & Proc : processVector) { switch (Proc.state) { case ready: stateChar = 'r'; break; case processing: stateChar = 'p'; break; case blocked: stateChar = 'b'; break; case notArrived: stateChar = 'n'; break; case done: stateChar = 'd'; break; } cout < statechar="">< '="" ';="" }="" cout="">< endl;="" }="" void="">& processVector) { cout < "at="" |="" dt="" |="" rqpt="" |="" pt="" |="" s="" |="" io"="">< endl;="" for(auto="" &="" proc="" :="" processvector)="" {="" proc.printprocess();="" }="" }="" bool="">& processVector) { for(auto & Proc : processVector) { if(Proc.state != done) { return false; } } return true; } process.h /* Relationship between IOEvent::time and Process time: IOEvent::time represents the number of time steps into a process execution that an IO Event will occur. This value will be relative to Process::processorTime. For example, if a Process has an IOEvent with a time value of 10, then that means that when the process hits it's 10th time step (i.e., Process::processorTime = 10) it should enter a blocked state for IOEvent::duration time steps. */ #pragma once #include #include #include #include #include using namespace std; struct IOEvent { IOEvent() : time(-1), duration(0) {} long time; // The time the event occurs during the process execution long duration; // The duration that the process will be Blocked by this IOEvent }; enum State { ready, processing, blocked, notArrived, done }; // Used to track the three process states struct Process { Process() : arrivalTime(-1), doneTime(-1), reqProcessorTime(0), processorTime(0), state(notArrived) {} long arrivalTime; // When the process will start/become runnable long doneTime; // Convenience variable, use this to keep track of when a process completes long reqProcessorTime; // Total amount of processor time needed long processorTime; // Amount of processor given to this process State state; // State of the process vector ioEvents; // The IO events for this process IOEvent currentEvent; // Used to track the progress of the event when process is blocked void printProcess() { cout < setw(2)="">< arrivaltime="">< "="" |";="" cout="">< setw(3)="">< donetime="">< "="" |";="" cout="">< setw(5)="">< reqprocessortime="">< "="" |";="" cout="">< setw(3)="">< processortime="">< "="" |";="" cout="">< setw(2)="">< state="">< "="" |";="" for="" (auto="" &="" event="" :="" ioevents)="" {="" cout="">< "="" "="">< event.time="">< ",="" "="">< event.duration="">< ";";="" }="" cout="">< endl;="" }="" };="" read="" data="" from="" a="" file="" (param="" filename)="" and="" parse="" it="" into="" a="" vector="" of="" processes="" (param="" processvector)="" void="" initprocesssetfromfile(const="" string&="" filename,="">& processVector); // Print the state of all the processes in the vector void printProcessStates(vector& processVector); // Print all information about all processes from a vector (debugging function) void printProcessSet(vector& processVector); // Check if all of the processes are complete bool allProcessesComplete(vector& processVector); procList.txt 1 20 2 40 5

Answered Same DaySep 14, 2021

Answer To: C++ ::::: In this assignment you will implement a basic single threaded 3 state processor with the...

Karthi answered on Sep 15 2021
140 Votes
fivestate/main.cpp
#include "process.h"
#include // for sleep
#include // for sleep
int main(int argc, char* argv[])
{
// single thread processor
// it's either processing something or it's not
bool processorAvailable = true;
// acts as a processorAvailable in the for loop
bool tempAvailable = true;
// vector of processes, fill by reading from a file
vector processList;
// number of processes in the processList
unsigned long processes;
// tracks the event number in the processes
vector eventIndex;
// tracks the time an event has been active
vector eventDuration;

// Do not touch
long time = 1;
long sleepDuration = 50;
string file;
stringstream ss;
// Do not touch
switch(argc)
{
case 1:
file = "procList.txt"; // default input file
break;
case 2:
file = argv[1]; // file given from command line
break;
case 3:
file = argv[1]; // file given
ss.str(argv[2]); // sleep duration given
ss >> sleepDuration;
break;
default:
cerr << "incorrect number of command line arguments" << endl;
cout << "usage: " << argv[0] << " [file] [sleepDuration]" << endl;
return 1;
break;
}
// Read in from the file
initProcessSetFromFile(file, processList);
processes = processList.size();
// reserve data for the vectors
eventIndex.reserve(processes);
eventDuration.reserve(processes);
// fill the vectors
for ( unsigned long i = 0; i < processes; i++ )
{
eventIndex.push_back(0);
eventDuration.push_back(0);
}
// main loop for processing functionality
while( allProcessesComplete(processList) == false )
{
// processes all the processes in processList
for ( unsigned long i = 0; i < processes; i++ )
{
// checks for processes that have not arrived
if ( processList.at(i).state == notArrived )
{
if ( processList.at(i).arrivalTime == time )
{
processList.at(i).state = ready;
// setting the first currentEvent
if ( processList.at(i).ioEvents.size() != 0 )
processList.at(i).currentEvent = processList.at(i).ioEvents.at(0);
// Handling of 0 processing time process
if ( processList.at(i).reqProcessorTime == 0 )
{
processList.at(i).state = done;
processList.at(i).processorTime = 0;
processList.at(i).doneTime = time;
}
}
}
if ( processorAvailable == true )
{
// processes the next available process
for ( unsigned long j = 0; j < processes; j++ )
{
if ( ( processList.at(j).state == ready ) && ( tempAvailable == true ))
{
tempAvailable = false;
processorAvailable = tempAvailable;
processList.at(j).state = processing;
}
}
}
// Sets processes that are finished to done and block the process if there is an ioevent
if ( processList.at(i).state == processing )
{
processList.at(i).processorTime++;

if ( processList.at(i).processorTime == processList.at(i).reqProcessorTime )
{
processList.at(i).state = done;
processList.at(i).doneTime = time;
processorAvailable = true;
}
else if (( processList.at(i).ioEvents.size() != 0 ) && ( processList.at(i).currentEvent.time == processList.at(i).processorTime ))
{
processList.at(i).state = blocked;
processorAvailable = true;
}
}
// Unblocks processes if their ioevent is finished and continues if it is not finished
if ( processList.at(i).state == blocked )
{
eventDuration.at(i)++;
if ( eventDuration.at(i) == processList.at(i).currentEvent.duration )
{
processList.at(i).state = ready;
eventDuration.at(i) = 0;
eventIndex.at(i)++;
if ( eventIndex.at(i) < processList.at(i).ioEvents.size() )
{
processList.at(i).currentEvent = processList.at(i).ioEvents.at(eventIndex.at(i));
}
}
}
}
tempAvailable = processorAvailable;
// Do not touch
cout << time++ << '\t'; // Do not touch
// You may wish to use a second vector of processes (you don't need to, but you can)
printProcessStates(processList); // change processList to another vector of processes if desired
this_thread::sleep_for(chrono::milliseconds(sleepDuration)); // Do not touch
}
return 0;
}
fivestate/Makefile
CXX = g++
FLAGS = -W -Wall -Wextra -Wpedantic -Werror -std=c++11
LIBRARIES = -lpthread
.PHONY: default run
default: run
run:
    ${CXX} ${FLAGS} *.cpp ${LIBRARIES} -o program
clean:
    -@rm -rf *.o program core
fivestate/process.cpp
#include "process.h"
void initProcessSetFromFile(const string& fileName, vector& processVector)
{
stringstream ss;
ifstream in(fileName.c_str());
string line, strItem;
Process proc;
IOEvent io;
processVector.clear();
if(!in.good())
{
cerr << "initProcessSetFromFile error unable to open file \"" << fileName << "\"" << endl;
return;
}
processVector.reserve(20);
while(getline(in, line))
{
ss.clear();
ss.str(line);
ss >> proc.arrivalTime;
ss >> proc.reqProcessorTime;
proc.ioEvents.clear();
proc.ioEvents.reserve(20);
while(ss >> io.time)
{
ss >> io.duration;
proc.ioEvents.push_back(io);
}
processVector.push_back(proc);
}
}
void printProcessStates(vector& processVector)
{
char stateChar;
for(auto & Proc : processVector)
{
switch (Proc.state)
{
case ready:
stateChar = 'r';
break;
case processing:
stateChar = 'p';
break;
case blocked:
stateChar = 'b';
break;
case notArrived:
stateChar = 'n';
break;
case done:
stateChar = 'd';
break;
}
cout << stateChar << ' ';
}
cout << endl;
}
void printProcessSet(vector& processVector)
{
cout << "AT | DT | RQPT | PT | S | IO" << endl;
for(auto & Proc : processVector)
{
Proc.printProcess();
}
}
bool allProcessesComplete(vector& processVector)
{
for(auto & Proc : processVector)
{
if(Proc.state != done)
{
return false;
}
}
return true;
}
fivestate/process.h
/*
Relationship between IOEvent::time and Process time:
IOEvent::time represents the number of time steps into a process execution that
an IO Event will occur. This value will be relative to Process::processorTime.
For example, if a Process has an IOEvent with a time value of 10, then that means
that when the process hits it's 10th time step (i.e., Process::processorTime = 10)
it should enter a blocked state for IOEvent::duration time steps.
*/
#pragma once
#include
#include
#include
#include
#include
using namespace std;
struct IOEvent
{
IOEvent() : time(-1), duration(0) {}
long time; // The time the event occurs during the process execution
long duration; // The duration that the process will be Blocked by this IOEvent
};
enum State { ready, processing, blocked, notArrived, done }; // Used to track the three process states
struct Process
{
Process() : arrivalTime(-1), doneTime(-1), reqProcessorTime(0), processorTime(0), state(notArrived) {}
long arrivalTime; // When the process will start/become runnable
long doneTime; // Convenience variable, use this to keep track of when a process completes
long reqProcessorTime; // Total amount of processor time needed
long processorTime; // Amount of processor given to this process
State state; // State of the process
vector ioEvents; // The IO events for this process
IOEvent currentEvent; // Used to track the progress of the event when process is blocked
void printProcess()
{
cout << setw(2) << arrivalTime << " |";
cout << setw(3) << doneTime << " |";
cout << setw(5) << reqProcessorTime << " |";
cout << setw(3) << processorTime << " |";
cout << setw(2) << state << " |";
for (auto & event : ioEvents)
{
cout << " " << event.time << ", " << event.duration << ";";
}
cout << endl;
}
};
// Read data from a file (param fileName) and parse it into a vector of Processes (param processVector)
void initProcessSetFromFile(const string& fileName, vector& processVector);
// Print the state of all the processes in the vector
void printProcessStates(vector& processVector);
// Print all information about all processes from a vector (debugging function)
void printProcessSet(vector& processVector);
// Check if all of the processes are complete
bool allProcessesComplete(vector& processVector);
fivestate/procList.txt
1 20
2 40 5 10 15 10
2 80 20 10 40 10 60 10
2 20 10 40
4 60 20 10 40 10
4 50 40 15
6 40
6 30 5 5 10 5 15 5
8 11 2 4 4 4 6 4 8 4 10 4
8 30 10 5 20 10
8 30 10 5 20 10
fivestate/sampleOutput.txt
1    p n n n n n n n n n n
2    p r r r n n n n n n n
3    p r r r n n n n n n n
4    p r r r r r n n n n n
5    p r r r r r n n n n n
6    p r r r r r r r n n n
7    p r r r r r r r n n n
8    p r r r r r r r r r r
9    p r r r r r r r r r r
10    p r r r r r r r r r r
11    p r r r r r r r r r r
12    p r r r r r r r r r r
13    p r r r r r r r r r r
14    p r r r r r r r r r r
15    p r r r r r r r r r r
16    p r r r r r r r r r r
17    p r r r r r r r r r r
18    p r r r r r r r r r r
19    p r r r r r r r r r r
20    p r r r r r r r r r r
21    p r r r r r r r r r r
22    p r r r r r r r r r r
23    p r r r r r r r r r r
24    p r r r r r r r r r r
25    p r r r r r r r r r r
26    p r r r r r r r r r r
27    p r r r r r r r r r r
28    p r r r r r r r r r r
29    p r r r r r r r r r r
30    p r r r r r r r r r r
31    p r r r r r r r r r r
32    p r r r r r r r r r r
33    p r r r r r r r r r r
34    p r r r r r r r r r r
35    p r r r r r r r r r r
36    p r r r r r r r r r r
37    p r r r r r r r r r r
38    p r r r r r r r r r r
39    p r r r r r r r r r r
40    p r r r r r r r r r r
41    p r r r r r r r r r r
42    p r r r r r r r r r r
43    p r r r r r r r r r r
44    p r r r r r r r r r r
45    p r r r r r r r r r r
46    p r r r r r r r r r r
47    p r r r r r r r r r r
48    p r r r r r r r r r r
49    p r r r r r r r r r r
50    p r r r r r r r r r r
51    p...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here