771 lines
25 KiB
C++
771 lines
25 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <cstring>
|
|
#include <unistd.h>
|
|
#include <chrono>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
struct process_detail {
|
|
//cpu_burst_times[0] is arrival time
|
|
int pid;
|
|
vector<int> burst_times;
|
|
int in_cpu1;
|
|
int in_cpu2;
|
|
int current_burst_index;
|
|
int arrvival_time = 0;
|
|
int wait_time = 0;
|
|
int cpu_time = 0;
|
|
int completion_time = 0;
|
|
};
|
|
|
|
struct clock{
|
|
int timer;
|
|
};
|
|
|
|
vector<process_detail> processes;
|
|
queue<process_detail*> ready_queue_fifo;
|
|
process_detail* CPU1 = NULL;
|
|
process_detail* CPU2 = NULL;
|
|
vector<string> out_cpu1;
|
|
vector<string> out_cpu2;
|
|
ofstream output_file("cpu_times.txt");
|
|
|
|
|
|
// ------------------------------------- THE FIFO ---------------------------------------
|
|
void fifo() {
|
|
// Clock initialized to 0
|
|
struct clock time;
|
|
memset(&time, 0, sizeof(struct clock));
|
|
time.timer = 0;
|
|
int process_count = processes.size();
|
|
int completed_processes = 0;
|
|
string out_string1 = "";
|
|
string out_string2 = "";
|
|
vector<process_detail*> waiting(process_count, NULL);
|
|
|
|
while(completed_processes < process_count) {
|
|
|
|
// Breaking from the infinite loop
|
|
for (int j = 0; j < process_count; ++j) {
|
|
if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
|
|
waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
|
|
waiting[j] = NULL;
|
|
completed_processes++;
|
|
}
|
|
}
|
|
|
|
// Managing arrival times
|
|
for (int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 != 1 || processes[i].in_cpu2 != 1) {
|
|
if(time.timer == processes[i].burst_times[0]) {
|
|
processes[i].arrvival_time = time.timer;
|
|
ready_queue_fifo.push(&processes[i]);
|
|
processes[i].current_burst_index++;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// Managing waiting queue
|
|
for (int j = 0; j < waiting.size(); ++j) {
|
|
if (waiting[j] != NULL) {
|
|
if (waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
|
|
ready_queue_fifo.push(waiting[j]);
|
|
waiting[j]->current_burst_index++;
|
|
waiting[j] = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (CPU1 == NULL && !ready_queue_fifo.empty()) {
|
|
CPU1 = ready_queue_fifo.front();
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue_fifo.pop();
|
|
}
|
|
|
|
if (CPU2 == NULL && !ready_queue_fifo.empty()) {
|
|
CPU2 = ready_queue_fifo.front();
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue_fifo.pop();
|
|
}
|
|
|
|
// Check CPU1
|
|
if(CPU1 != NULL) {
|
|
//check cpu_burst complete
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 == 1) {
|
|
processes[i].cpu_time += 1;
|
|
if(CPU1->burst_times[processes[i].current_burst_index] == 0){
|
|
out_string1 += " " + to_string(time.timer);
|
|
out_cpu1.push_back(out_string1);
|
|
CPU1->in_cpu1 = 0;
|
|
CPU1->current_burst_index++;
|
|
waiting[CPU1->pid] = CPU1; // process added to waiting queue
|
|
if(!ready_queue_fifo.empty()) {
|
|
CPU1 = ready_queue_fifo.front(); // process added to CPU
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue_fifo.pop();
|
|
}
|
|
else {
|
|
CPU1 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(CPU2 != NULL) {
|
|
//check cpu_burst complete
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu2 == 1) {
|
|
processes[i].cpu_time += 1;
|
|
if(CPU2->burst_times[processes[i].current_burst_index] == 0){
|
|
out_string2 += " " + to_string(time.timer);
|
|
out_cpu2.push_back(out_string2);
|
|
CPU2->in_cpu2 = 0;
|
|
CPU2->current_burst_index++;
|
|
waiting[CPU2->pid] = CPU2; // process added to waiting queue
|
|
if(!ready_queue_fifo.empty()) {
|
|
CPU2 = ready_queue_fifo.front(); // process added to CPU
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue_fifo.pop();
|
|
}
|
|
else {
|
|
CPU2 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(CPU1 != NULL) {
|
|
CPU1->burst_times[CPU1->current_burst_index]--;
|
|
}
|
|
|
|
if(CPU2 != NULL) {
|
|
CPU2->burst_times[CPU2->current_burst_index]--;
|
|
}
|
|
|
|
for(int j = 0; j < waiting.size(); ++j) {
|
|
if(waiting[j] != NULL) {
|
|
if(waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) {
|
|
waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0
|
|
}
|
|
}
|
|
}
|
|
// Increment the timer
|
|
time.timer++;
|
|
}
|
|
return;
|
|
|
|
}
|
|
|
|
// ----------------------------------------THE Shortest Job First----------------------------------
|
|
// Custom comparator for the priority queue
|
|
struct Compare {
|
|
bool operator()(process_detail* a, process_detail* b) {
|
|
// Compare the elements in the vector at the given indices
|
|
if (a->current_burst_index == 0) {
|
|
return a->burst_times[a->current_burst_index + 1] > b->burst_times[b->current_burst_index];
|
|
}
|
|
else if(b->current_burst_index == 0) {
|
|
return a->burst_times[a->current_burst_index] > b->burst_times[b->current_burst_index+1];
|
|
|
|
}
|
|
else if(b->current_burst_index == 0 && a->current_burst_index == 0)
|
|
return a->burst_times[a->current_burst_index+1] > b->burst_times[b->current_burst_index+1];
|
|
else return a->burst_times[a->current_burst_index] > b->burst_times[b->current_burst_index];
|
|
}
|
|
};
|
|
|
|
priority_queue<process_detail*, vector<process_detail*>, Compare> ready_queue;
|
|
|
|
void sjf() {
|
|
// Clock initialized to 0
|
|
struct clock time;
|
|
memset(&time, 0, sizeof(struct clock));
|
|
time.timer = 0;
|
|
int process_count = processes.size();
|
|
int completed_processes = 0;
|
|
vector<process_detail*> waiting(process_count, NULL);
|
|
string out_string1 = "";
|
|
string out_string2 = "";
|
|
|
|
while(completed_processes < process_count) {
|
|
|
|
// breaking from the infinite loop
|
|
for (int j = 0; j < process_count; ++j) {
|
|
if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
|
|
waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
|
|
waiting[j] = NULL;
|
|
completed_processes++;
|
|
}
|
|
}
|
|
|
|
// Managing arrival times
|
|
for (int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 != 1 || processes[i].in_cpu2 != 1) {
|
|
if(time.timer == processes[i].burst_times[0]) {
|
|
ready_queue.push(&processes[i]);
|
|
processes[i].current_burst_index++;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// Managing waiting queue
|
|
for (int j = 0; j < waiting.size(); ++j) {
|
|
if (waiting[j] != NULL) {
|
|
if (waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
|
|
ready_queue.push(waiting[j]);
|
|
waiting[j]->current_burst_index++;
|
|
waiting[j] = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (CPU1 == NULL && !ready_queue.empty()) {
|
|
// Assign the first process from the ready queue to the CPU
|
|
CPU1 = ready_queue.top();
|
|
CPU1->in_cpu1 = 1;
|
|
// Record in_time when the process enters the CPU
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
}
|
|
|
|
if (CPU2 == NULL && !ready_queue.empty()) {
|
|
// Assign the first process from the ready queue to the CPU
|
|
CPU2 = ready_queue.top();
|
|
CPU2->in_cpu2 = 1;
|
|
// Record in_time when the process enters the CPU
|
|
// output_file << endl;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU2->pid + 1 << ",2 " << time.timer;
|
|
ready_queue.pop();
|
|
}
|
|
|
|
// Check CPU1
|
|
if(CPU1 != NULL) {
|
|
//check cpu_burst complete
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 == 1) {
|
|
processes[i].cpu_time += 1;
|
|
if(CPU1->burst_times[processes[i].current_burst_index] == 0){
|
|
// Record out_time when the process exits the CPU
|
|
out_string1 += " " + to_string(time.timer);
|
|
// output_file << out_string1 << endl;
|
|
out_cpu1.push_back(out_string1);
|
|
CPU1->in_cpu1 = 0;
|
|
CPU1->current_burst_index++;
|
|
waiting[CPU1->pid] = CPU1; // process added to waiting queue
|
|
if(!ready_queue.empty()) {
|
|
CPU1 = ready_queue.top(); // process added to CPU
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
}
|
|
else {
|
|
CPU1 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(CPU2 != NULL) {
|
|
//check cpu_burst complete
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu2 == 1) {
|
|
processes[i].cpu_time += 1;
|
|
if(CPU2->burst_times[processes[i].current_burst_index] == 0){
|
|
// Record out_time when the process exits the CPU
|
|
out_string2 += " " + to_string(time.timer);
|
|
// output_file << out_string2 << endl;
|
|
out_cpu2.push_back(out_string2);
|
|
CPU2->in_cpu2 = 0;
|
|
CPU2->current_burst_index++;
|
|
waiting[CPU2->pid] = CPU2; // process added to waiting queue
|
|
if(!ready_queue.empty()) {
|
|
CPU2 = ready_queue.top(); // process added to CPU
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
}
|
|
else {
|
|
CPU2 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(CPU1 != NULL) {
|
|
CPU1->burst_times[CPU1->current_burst_index]--;
|
|
}
|
|
|
|
if(CPU2 != NULL) {
|
|
CPU2->burst_times[CPU2->current_burst_index]--;
|
|
}
|
|
|
|
for(int j = 0; j < waiting.size(); ++j) {
|
|
if(waiting[j] != NULL) {
|
|
if(waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) {
|
|
waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0
|
|
}
|
|
}
|
|
}
|
|
// Increment the timer
|
|
time.timer++;
|
|
}
|
|
return;
|
|
}
|
|
// --------------------------- The Pre-emptive Shortest Job First ---------------------------------
|
|
|
|
void pre_sjf() {
|
|
|
|
// Clock initialized to 0
|
|
struct clock time;
|
|
memset(&time, 0, sizeof(struct clock));
|
|
time.timer = 0;
|
|
int process_count = processes.size();
|
|
int completed_processes = 0;
|
|
string out_string1 = "";
|
|
string out_string2 = "";
|
|
vector<process_detail*> waiting(process_count, NULL);
|
|
|
|
|
|
while(completed_processes < process_count) {
|
|
|
|
// breaking from the infinite loop
|
|
for (int j = 0; j < process_count; ++j) {
|
|
if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
|
|
waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
|
|
waiting[j] = NULL;
|
|
completed_processes++;
|
|
}
|
|
}
|
|
|
|
// Managing arrival times
|
|
for (int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 != 1 || processes[i].in_cpu2 != 1) {
|
|
if(time.timer == processes[i].burst_times[0]) {
|
|
ready_queue.push(&processes[i]);
|
|
if(CPU1 != NULL) {
|
|
ready_queue.push(CPU1);
|
|
CPU1->in_cpu1 = 0;
|
|
out_string1 += " " + to_string(time.timer);
|
|
// output_file << out_string1 << endl;
|
|
out_cpu1.push_back(out_string1);
|
|
// output_file << " " << time.timer << endl;
|
|
CPU1 = ready_queue.top();
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
}
|
|
if(CPU2 != NULL) {
|
|
ready_queue.push(CPU2);
|
|
CPU2->in_cpu2 = 0;
|
|
out_string2 += " " + to_string(time.timer);
|
|
// output_file << out_string2 << endl;
|
|
out_cpu2.push_back(out_string2);
|
|
CPU2 = ready_queue.top();
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
}
|
|
processes[i].current_burst_index++;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// Managing waiting queue
|
|
for (int j = 0; j < waiting.size(); ++j) {
|
|
if (waiting[j] != NULL) {
|
|
if (waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
|
|
ready_queue.push(waiting[j]);
|
|
if(CPU1 != NULL) {
|
|
ready_queue.push(CPU1);
|
|
CPU1->in_cpu1 = 0;
|
|
out_string1 += " " + to_string(time.timer);
|
|
// output_file << out_string1 << endl;
|
|
out_cpu1.push_back(out_string1);
|
|
CPU1 = ready_queue.top();
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
|
|
}
|
|
if(CPU2 != NULL) {
|
|
ready_queue.push(CPU2);
|
|
CPU2->in_cpu2 = 0;
|
|
out_string2 += " " + to_string(time.timer);
|
|
// output_file << out_string2 << endl;
|
|
out_cpu2.push_back(out_string2);
|
|
CPU2 = ready_queue.top();
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU2->pid+1 << ",2" << " " << time.timer; // New entry time
|
|
ready_queue.pop();
|
|
|
|
}
|
|
waiting[j]->current_burst_index++;
|
|
waiting[j] = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (CPU1 == NULL && !ready_queue.empty()) {
|
|
// Assign the first process from the ready queue to the CPU
|
|
CPU1 = ready_queue.top();
|
|
CPU1->in_cpu1 = 1;
|
|
// Record in_time when the process enters the CPU
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU1->pid + 1 << ",1 " << time.timer;
|
|
ready_queue.pop();
|
|
}
|
|
|
|
if (CPU2 == NULL && !ready_queue.empty()) {
|
|
// Assign the first process from the ready queue to the CPU
|
|
CPU2 = ready_queue.top();
|
|
CPU2->in_cpu2 = 1;
|
|
// Record in_time when the process enters the CPU
|
|
// output_file << endl;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU2->pid + 1 << ",2 " << time.timer;
|
|
ready_queue.pop();
|
|
}
|
|
|
|
// Check CPU1
|
|
if(CPU1 != NULL) {
|
|
//check cpu_burst complete
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 == 1) {
|
|
if(CPU1->burst_times[processes[i].current_burst_index] == 0){
|
|
// Record out_time when the process exits the CPU
|
|
out_string1 += " " + to_string(time.timer);
|
|
// output_file << out_string1 << endl;
|
|
out_cpu1.push_back(out_string1);
|
|
CPU1->in_cpu1 = 0;
|
|
CPU1->current_burst_index++;
|
|
waiting[CPU1->pid] = CPU1; // process added to waiting queue
|
|
if(!ready_queue.empty()) {
|
|
CPU1 = ready_queue.top(); // process added to CPU
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
}
|
|
else {
|
|
CPU1 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(CPU2 != NULL) {
|
|
//check cpu_burst complete
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu2 == 1) {
|
|
if(CPU2->burst_times[processes[i].current_burst_index] == 0){
|
|
// Record out_time when the process exits the CPU
|
|
out_string2 += " " + to_string(time.timer);
|
|
// output_file << out_string2 << endl;
|
|
out_cpu2.push_back(out_string2);
|
|
CPU2->in_cpu2 = 0;
|
|
CPU2->current_burst_index++;
|
|
waiting[CPU2->pid] = CPU2;// process added to waiting queue
|
|
if(!ready_queue.empty()) {
|
|
CPU2 = ready_queue.top(); // process added to CPU
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
|
|
ready_queue.pop();
|
|
}
|
|
else {
|
|
CPU2 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(CPU1 != NULL) {
|
|
CPU1->burst_times[CPU1->current_burst_index]--;
|
|
}
|
|
|
|
if(CPU2 != NULL) {
|
|
CPU2->burst_times[CPU2->current_burst_index]--;
|
|
}
|
|
|
|
for(int j = 0; j < waiting.size(); ++j) {
|
|
if(waiting[j] != NULL) {
|
|
if(waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) {
|
|
waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0
|
|
}
|
|
}
|
|
}
|
|
// Increment the timer
|
|
time.timer++;
|
|
}
|
|
return;
|
|
}
|
|
|
|
// ---------------------------------- The Round Robin--------------------------------------------
|
|
|
|
void round_robin() {
|
|
struct clock time;
|
|
memset(&time, 0, sizeof(struct clock));
|
|
time.timer = 0;
|
|
int process_count = processes.size();
|
|
int completed_processes = 0;
|
|
int time_quantum = 5;
|
|
int current_quantum1 = 0;
|
|
int current_quantum2 = 0;
|
|
string out_string1 = "";
|
|
string out_string2 = "";
|
|
// Initialize waiting vector with NULLs for each process slot
|
|
vector<process_detail*> waiting(process_count, NULL);
|
|
|
|
|
|
while (completed_processes < process_count) {
|
|
// Check for process completion
|
|
for (int j = 0; j < process_count; ++j) {
|
|
if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
|
|
waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
|
|
waiting[j] = NULL;
|
|
completed_processes++;
|
|
}
|
|
}
|
|
|
|
// Managing arrival times
|
|
for (int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 != 1 || processes[i].in_cpu2 != 1) {
|
|
if(time.timer == processes[i].burst_times[0]) {
|
|
processes[i].arrvival_time = time.timer;
|
|
ready_queue_fifo.push(&processes[i]);
|
|
processes[i].current_burst_index++;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// Managing waiting queue
|
|
for (int j = 0; j < waiting.size(); ++j) {
|
|
if (waiting[j] != NULL) {
|
|
if (waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
|
|
ready_queue_fifo.push(waiting[j]);
|
|
waiting[j]->current_burst_index++;
|
|
waiting[j] = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Assign a process to CPU1 if available
|
|
if (CPU1 == NULL && !ready_queue_fifo.empty()) {
|
|
CPU1 = ready_queue_fifo.front();
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
|
ready_queue_fifo.pop();
|
|
current_quantum1 = time_quantum;
|
|
}
|
|
|
|
// Assign a process to CPU2 if available
|
|
if (CPU2 == NULL && !ready_queue_fifo.empty()) {
|
|
CPU2 = ready_queue_fifo.front();
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
|
ready_queue_fifo.pop();
|
|
current_quantum2 = time_quantum;
|
|
}
|
|
|
|
if (CPU1 != NULL) {
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu1 == 1){
|
|
processes[i].cpu_time += 1;
|
|
if (CPU1->burst_times[CPU1->current_burst_index] == 0 || current_quantum1 == 0) {
|
|
// output_file << " " << time.timer << endl;
|
|
out_string1 += " " + to_string(time.timer);
|
|
out_cpu1.push_back(out_string1);
|
|
CPU1->in_cpu1 = 0;
|
|
if (CPU1->burst_times[CPU1->current_burst_index] == 0){
|
|
CPU1->current_burst_index++;
|
|
waiting[CPU1->pid] = CPU1;
|
|
}
|
|
|
|
else if (current_quantum1 == 0) ready_queue_fifo.push(CPU1);
|
|
|
|
// Place the process in its corresponding waiting slot by pid
|
|
|
|
if (!ready_queue_fifo.empty()) {
|
|
CPU1 = ready_queue_fifo.front();
|
|
CPU1->in_cpu1 = 1;
|
|
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
|
ready_queue_fifo.pop();
|
|
current_quantum1 = time_quantum;
|
|
} else {
|
|
CPU1 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (CPU2 != NULL) {
|
|
for(int i = 0; i < process_count; ++i) {
|
|
if(processes[i].in_cpu2 == 1){
|
|
processes[i].cpu_time += 1;
|
|
if (CPU2->burst_times[CPU2->current_burst_index] == 0 || current_quantum2 == 0) {
|
|
// output_file << " " << time.timer << endl;
|
|
out_string2 += " " + to_string(time.timer);
|
|
out_cpu2.push_back(out_string2);
|
|
CPU2->in_cpu2 = 0;
|
|
if (CPU2->burst_times[CPU2->current_burst_index] == 0){
|
|
CPU2->current_burst_index++;
|
|
waiting[CPU2->pid] = CPU2;
|
|
}
|
|
|
|
else if (current_quantum2 == 0) ready_queue_fifo.push(CPU2);
|
|
|
|
// Place the process in its corresponding waiting slot by pid
|
|
|
|
if (!ready_queue_fifo.empty()) {
|
|
CPU2 = ready_queue_fifo.front();
|
|
CPU2->in_cpu2 = 1;
|
|
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
|
// output_file << "P" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
|
ready_queue_fifo.pop();
|
|
current_quantum2 = time_quantum;
|
|
} else {
|
|
CPU2 = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(CPU1 != NULL) {
|
|
CPU1->burst_times[CPU1->current_burst_index]--;
|
|
current_quantum1--;
|
|
}
|
|
|
|
if(CPU2 != NULL) {
|
|
CPU2->burst_times[CPU2->current_burst_index]--;
|
|
current_quantum2--;
|
|
}
|
|
|
|
// Manage IO bursts in waiting queue
|
|
for (int j = 0; j < process_count; ++j) {
|
|
if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) {
|
|
waiting[j]->burst_times[waiting[j]->current_burst_index]--;
|
|
}
|
|
}
|
|
|
|
// Increment the timer
|
|
time.timer++;
|
|
}
|
|
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if(argc != 3)
|
|
{
|
|
cout <<"usage: ./scheduler.out <path-to-workload-file> <scheduler_algorithm>\nprovided arguments:\n";
|
|
for(int i = 0; i < argc; i++)
|
|
cout << argv[i] << "\n";
|
|
return -1;
|
|
}
|
|
|
|
char *file_to_search_in = argv[1];
|
|
char *scheduler_algorithm = argv[2];
|
|
|
|
ifstream file(file_to_search_in, ios::binary);
|
|
// ifstream file("process1.dat", ios::binary);
|
|
string buffer;
|
|
int pid = 0;
|
|
|
|
while(getline(file, buffer)) {
|
|
if(buffer[0] == '<'){
|
|
continue;
|
|
}
|
|
istringstream iss(buffer);
|
|
string word;
|
|
struct process_detail pd;
|
|
memset(&pd,0,sizeof(struct process_detail));
|
|
pd.pid = pid++;
|
|
pd.current_burst_index = 0;
|
|
|
|
while(iss>>word){
|
|
pd.burst_times.push_back(stoi(word));
|
|
}
|
|
processes.push_back(pd);
|
|
}
|
|
|
|
map<string, int> temp;
|
|
temp["fifo"] = 1;
|
|
temp["sjf"] = 2;
|
|
temp["pre_sjf"] = 3;
|
|
temp["rr"] = 4;
|
|
|
|
string temp1 = scheduler_algorithm;
|
|
// string temp1 = "pre_sjf";
|
|
|
|
// Start time point
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
switch(temp[temp1]){
|
|
case 1:
|
|
fifo();
|
|
break;
|
|
case 2:
|
|
sjf();
|
|
break;
|
|
case 3:
|
|
pre_sjf();
|
|
break;
|
|
case 4:
|
|
round_robin();
|
|
break;
|
|
default:
|
|
cout << "enter fifo or sjf or pre_sjf or rr" << endl;
|
|
}
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
|
|
|
output_file << "CPU1" << endl;
|
|
for(int i = 0; i < out_cpu1.size(); ++i) {
|
|
output_file << out_cpu1[i] << endl;
|
|
}
|
|
|
|
output_file << "CPU2" << endl;
|
|
for(int i = 0; i < out_cpu2.size(); ++i) {
|
|
output_file << out_cpu2[i] << endl;
|
|
}
|
|
float tot = 0;
|
|
int count = processes.size();
|
|
for(int i = 0; i < processes.size(); ++i) {
|
|
tot += processes[i].completion_time;
|
|
cout << "Process " << i+1 << " Completion Time: " << processes[i].completion_time << endl;
|
|
}
|
|
cout << "Average Completion Time: " << tot/count << endl;
|
|
tot = 0;
|
|
for(int i = 0; i < processes.size(); ++i) {
|
|
tot += processes[i].completion_time - processes[i].cpu_time;
|
|
cout << "Process " << i+1 << " Waiting Time: " << processes[i].completion_time - processes[i].cpu_time << endl;
|
|
// cout << "Process " << i+1 << " Waiting Time: " << processes[i].wait_time << endl;
|
|
}
|
|
cout << "Average Waiting Time: " << tot/count << endl;
|
|
|
|
std::cout << "Execution time: " << duration.count() << " ms" << std::endl;
|
|
return 0;
|
|
} |