CSC-4730 / P3 / StrideScheduler.cpp
StrideScheduler.cpp
Raw
//CSC_4730 | P3 | User Stride Scheduler | Caleb A. Collar | 10.6.21

#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//Methods.
void sorter(void);

//Globals.
struct job {
	string name;
	int stride, priority, pass;
};

vector<string> args;
vector<job> runnable, blocked;
job newjob, running, clean;

int main(int argc, char* argv[]) {
	//Check for simulated input file.
	if(argc > 2) {
		printf("Too many agruments. \n");
		exit(0);
	}
	if(argc < 2) {
		printf("Argument was expected. \n");
		exit(0);
	}
	
	ifstream file(argv[1]);
	if (file.good());
	else {
		printf("File error.");
		exit(0);
	}

    //Storing arguments.
	string buffer;
	while(getline(file, buffer)) args.push_back(buffer);
	
    //Opcode: "newjob".
	for(int i = 0; i < args.size(); i++) {
		if(args[i].substr(0,6) == "newjob") {
			newjob.name = args[i].substr(7,1);
			stringstream stream(args[i].substr(9));
			stream >> newjob.priority;
			if(newjob.priority == 0) newjob.priority = 1;
			newjob.stride = 100000/ newjob.priority;
			cout << "New job: " << newjob.name << " added with priority: " << newjob.priority << endl;
			if(running.name == "") {
				running = newjob;
				newjob = clean;
				cout << "Job: " << running.name << " scheduled." << endl;
			}
			else {
				if(runnable.size() == 0) {
					newjob.pass = running.pass;
					runnable.push_back(newjob);
				}
				else {
					sorter();
					newjob.pass = runnable[0].pass;
					runnable.push_back(newjob);
				}
			}
			newjob = clean;
		}

		//Opcode: "finish".
		if(args[i].substr(0,6) == "finish"){
			if(running.name != "") {
				cout << "Job: " << running.name << " completed." << endl;
				running = clean;
				if(runnable.size() == 0) cout << "System is idle." << endl;
				else {
					sorter();
					running = runnable[0];
					runnable.erase(runnable.begin());
					cout << "Job: " << running.name << " scheduled." << endl;
				}
			}
			else cout << "Error. System is idle." << endl;
		}

		//Opcode: "Interrupt".
		if(args[i].substr(0,6) == "interr") {
			if(running.name == "") cout << "Error. System is idle." << endl;
			else {
				running.pass = running.pass + running.stride;
				runnable.push_back(running);
				sorter();
				running = clean;
				running = runnable[0];
				runnable.erase(runnable.begin());
				cout << "Job: " << running.name << " scheduled." << endl;
			}
		}

		//Opcode "Block".
		if(args[i].substr(0,6) == "block") {
			if(running.name == "") cout << "Error. System is idle." << endl;
			else {	
				cout << "Job: " << running.name << " blocked." << endl;
				blocked.push_back(running);
				running = clean;
				if(runnable.size() == 0) cout << "System is idle." << endl;
				else {
					sorter();
					running = runnable[0];
					runnable.erase(runnable.begin());
					cout << "Job: " << running.name << " scheduled." << endl;
				}
			}
		}

		//Opcode: "Unblock".
		if(args[i].substr(0,6) == "unbloc") {
			bool found = false;
			for(int j = 0; j < blocked.size(); j++) {
				if(blocked[j].name == args[i].substr(8)) {
					if(running.name != "") blocked[j].pass = running.pass;
					else {
						if(runnable.size() == 0) blocked[j].pass = 0;
						else {
							sorter();
							blocked[j].pass = runnable[0].pass;
						}
					}

					int passSet = blocked[j].pass;
					runnable.push_back(blocked[j]);
					blocked.erase(blocked.begin()+ j);
					found = true;
					cout << "Job: " << args[i].substr(8) << " has unblocked. Pass set to: " << passSet << endl;
				}
			}
			if(found == false) cout << "Error. Job: " << args[i].substr(8) << " not blocked." << endl;
		}

		//Opcode: "Runnable".
		if(args[i].substr(0,6) == "runnab") {
			cout << "Runnable:" << endl;
			if(runnable.size() == 0) cout << "None" << endl;
			else {
				sorter();
				//cout << "NAME" << setw(10) << "STRIDE" << setw(6) << "PASS" << setw(5) << "PRI" << endl;
				cout << setw(8) << left << "NAME";
				cout << setw(8) << left << "STRIDE";
				cout << setw(6) << left << "PASS";
				cout << setw(6) << right << "PRI" << endl;
				for(int i = 0; i < runnable.size(); i++) {
					cout << setw(8) << left << runnable[i].name;
					cout << setw(8) << left << runnable[i].stride;
					cout << setw(6) << left << runnable[i].pass;
					cout << setw(6) << right << runnable[i].priority;
					cout << endl;
				}
			}
		}

		//Opcode: "Running".
		if(args[i].substr(0,6) == "runnin") {
			cout << "Running:" << endl;
			if(running.name == "") cout << "None" << endl;
			else {
				cout << setw(8) << left << "NAME";
				cout << setw(8) << left << "STRIDE";
				cout << setw(6) << left << "PASS";
				cout << setw(6) << right << "PRI" << endl;
				cout << setw(8) << left << running.name;
				cout << setw(8) << left << running.stride;
				cout << setw(6) << left << running.pass;
				cout << setw(6) << right << running.priority;
				cout << endl;
			}
		}

		//Opcode: "Blocked".
		if(args[i].substr(0,6) == "blocke") {
			cout << "Blocked:" << endl;
			if(blocked.size() == 0) cout << "None" << endl;
			else {
				//cout << "NAME" << setw(10) << "STRIDE" << setw(6) << "PASS" << setw(5) << "PRI" << endl;
				cout << setw(8) << left << "NAME";
				cout << setw(8) << left << "STRIDE";
				cout << setw(6) << left << "PASS";
				cout << setw(6) << right << "PRI" << endl;

				for(int i = 0; i < blocked.size(); i++) {
					cout << setw(8) << left << blocked[i].name;
					cout << setw(8) << left << blocked[i].stride;
					cout << setw(6) << left << blocked[i].pass;
					cout << setw(6) << right << blocked[i].priority;
					cout << endl;
				}
			}
		}
	}
	return 0;
}

void sorter() {
	if(runnable.size() < 2) return;
	else {
		for(int i = 0; i < runnable.size(); i++) {
			for(int j = i + 1; j < runnable.size(); j++) {
				if(runnable[i].pass > runnable[j].pass) swap(runnable[i], runnable[j]);
				else if(runnable[i].pass == runnable[j].pass){
				    if(runnable[i].name[0] > runnable[j].name[0]) swap(runnable[i], runnable[j]);
				}
			}
		}
	}
}