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...