2024-09-17 17:37:01 +05:30
|
|
|
#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;
|
2024-09-17 17:51:50 +05:30
|
|
|
vector<int> burst_times;
|
2024-09-17 17:37:01 +05:30
|
|
|
int in_cpu;
|
2024-09-19 15:33:35 +05:30
|
|
|
int current_burst_index;
|
2024-09-17 17:37:01 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
struct clock{
|
|
|
|
int push_signal; //boolean
|
|
|
|
int timer;
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
};
|
|
|
|
|
2024-09-19 15:33:35 +05:30
|
|
|
vector<process_detail> processes;
|
|
|
|
queue<process_detail*> ready_queue_fifo;
|
2024-09-17 17:37:01 +05:30
|
|
|
struct process_detail* CPU = NULL;
|
2024-09-21 05:54:36 +05:30
|
|
|
vector<process_detail*> waiting;
|
2024-09-19 15:33:35 +05:30
|
|
|
|
|
|
|
ofstream output_file("cpu_times.txt");
|
2024-09-17 17:37:01 +05:30
|
|
|
|
2024-09-21 05:54:36 +05:30
|
|
|
// -------------------------------------- THE FIFO --------------------------------------------------
|
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
void fifo() {
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
//clock initialized to 0
|
|
|
|
struct clock time;
|
|
|
|
memset(&time, 0, sizeof(struct clock));
|
2024-09-19 15:33:35 +05:30
|
|
|
time.timer = 0;
|
|
|
|
time.push_signal = 0;
|
2024-09-17 17:37:01 +05:30
|
|
|
int process_count = processes.size();
|
2024-09-19 15:33:35 +05:30
|
|
|
int completed_processes = 0;
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-19 15:33:35 +05:30
|
|
|
while(completed_processes < process_count){
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-21 05:54:36 +05:30
|
|
|
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
|
|
|
|
|
2024-09-19 15:33:35 +05:30
|
|
|
// breaking from the infinite loop
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
2024-09-21 05:54:36 +05:30
|
|
|
if(processes[i].burst_times[processes[i].current_burst_index] == -2) {
|
2024-09-19 15:33:35 +05:30
|
|
|
completed_processes++;
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
//managing arrival times
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
//if process not in cpu
|
2024-09-19 15:33:35 +05:30
|
|
|
if(processes[i].in_cpu != 1) {
|
2024-09-17 17:51:50 +05:30
|
|
|
if(time.timer == processes[i].burst_times[0]) {
|
2024-09-19 15:33:35 +05:30
|
|
|
ready_queue_fifo.push(&processes[i]);
|
|
|
|
processes[i].current_burst_index++;
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-21 05:54:36 +05:30
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 17:51:50 +05:30
|
|
|
|
2024-09-19 15:33:35 +05:30
|
|
|
if(CPU == NULL && !ready_queue_fifo.empty()) {
|
2024-09-17 17:37:01 +05:30
|
|
|
CPU = ready_queue_fifo.front();
|
|
|
|
CPU->in_cpu = 1;
|
2024-09-19 15:33:35 +05:30
|
|
|
// Record in_time when the process enters the CPU
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1 ) / 2<< " " << time.timer;
|
2024-09-17 17:37:01 +05:30
|
|
|
ready_queue_fifo.pop();
|
2024-09-19 15:33:35 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
2024-09-19 15:33:35 +05:30
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
if(CPU != NULL){
|
2024-09-17 17:37:01 +05:30
|
|
|
//check cpu_burst complete
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
2024-09-21 05:54:36 +05:30
|
|
|
if(processes[i].in_cpu == 1) {
|
2024-09-19 15:33:35 +05:30
|
|
|
if(CPU->burst_times[processes[i].current_burst_index] == 0){
|
|
|
|
// Record out_time when the process exits the CPU
|
|
|
|
output_file << " " << time.timer << endl;
|
2024-09-17 17:51:50 +05:30
|
|
|
CPU->in_cpu = 0;
|
2024-09-19 15:33:35 +05:30
|
|
|
CPU->current_burst_index++;
|
|
|
|
waiting.push_back(CPU); // process added to waiting queue
|
|
|
|
if(!ready_queue_fifo.empty()) {
|
|
|
|
CPU = ready_queue_fifo.front(); // process added to CPU
|
|
|
|
CPU->in_cpu = 1;
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
2024-09-19 15:33:35 +05:30
|
|
|
ready_queue_fifo.pop();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CPU = NULL;
|
|
|
|
}
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-21 05:54:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(CPU != NULL) {
|
|
|
|
CPU->burst_times[CPU->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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.timer++;
|
|
|
|
}
|
|
|
|
output_file.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------- The Shortest Job Fist -------------------------------------------
|
|
|
|
// 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
|
|
|
|
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;
|
|
|
|
time.push_signal = 0;
|
|
|
|
int process_count = processes.size();
|
|
|
|
int completed_processes = 0;
|
|
|
|
|
|
|
|
while(completed_processes < process_count){
|
|
|
|
|
|
|
|
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
|
|
|
|
|
|
|
|
// breaking from the infinite loop
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
if(processes[i].burst_times[processes[i].current_burst_index] == -2) {
|
|
|
|
completed_processes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//managing arrival times
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
//if process not in cpu
|
|
|
|
if(processes[i].in_cpu != 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(CPU == NULL && !ready_queue.empty()) {
|
|
|
|
CPU = ready_queue.top();
|
|
|
|
CPU->in_cpu = 1;
|
|
|
|
// Record in_time when the process enters the CPU
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(CPU != NULL){
|
|
|
|
//check cpu_burst complete
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
if(processes[i].in_cpu == 1) {
|
|
|
|
if(CPU->burst_times[processes[i].current_burst_index] == 0){
|
|
|
|
// Record out_time when the process exits the CPU
|
|
|
|
output_file << " " << time.timer << endl;
|
|
|
|
CPU->in_cpu = 0;
|
|
|
|
CPU->current_burst_index++;
|
|
|
|
waiting.push_back(CPU); // process added to waiting queue
|
|
|
|
if(!ready_queue.empty()) {
|
|
|
|
CPU = ready_queue.top(); // process added to CPU
|
|
|
|
CPU->in_cpu = 1;
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue.pop();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CPU = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(CPU != NULL) {
|
|
|
|
// reducing the cpu burst till it reaches 0
|
|
|
|
CPU->burst_times[CPU->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) {
|
|
|
|
// reducing the io burst till it reaches 0
|
|
|
|
waiting[j]->burst_times[waiting[j]->current_burst_index]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.timer++;
|
|
|
|
}
|
|
|
|
output_file.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------Pre-emptive shortest job --------------------------------------------
|
|
|
|
|
|
|
|
void pre_sjf() {
|
|
|
|
|
|
|
|
//clock initialized to 0
|
|
|
|
struct clock time;
|
|
|
|
memset(&time, 0, sizeof(struct clock));
|
|
|
|
time.timer = 0;
|
|
|
|
time.push_signal = 0;
|
|
|
|
int process_count = processes.size();
|
|
|
|
int completed_processes = 0;
|
|
|
|
|
|
|
|
while(completed_processes < process_count){
|
|
|
|
|
|
|
|
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
|
|
|
|
|
|
|
|
// breaking from the infinite loop
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
if(processes[i].burst_times[processes[i].current_burst_index] == -2) {
|
|
|
|
completed_processes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//managing arrival times
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
//if process not in cpu
|
|
|
|
if(processes[i].in_cpu != 1) {
|
|
|
|
if(time.timer == processes[i].burst_times[0]) {
|
|
|
|
ready_queue.push(&processes[i]);
|
|
|
|
if(CPU != NULL) {
|
|
|
|
ready_queue.push(CPU);
|
|
|
|
CPU->in_cpu = 0;
|
|
|
|
output_file << " " << time.timer << endl;
|
|
|
|
CPU = ready_queue.top();
|
|
|
|
CPU->in_cpu = 1;
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
2024-09-21 05:54:36 +05:30
|
|
|
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(CPU != NULL) {
|
|
|
|
ready_queue.push(CPU);
|
|
|
|
CPU->in_cpu = 0;
|
|
|
|
output_file << " " << time.timer << endl;
|
|
|
|
CPU = ready_queue.top();
|
|
|
|
CPU->in_cpu = 1;
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue.pop();
|
|
|
|
|
|
|
|
}
|
|
|
|
waiting[j]->current_burst_index++;
|
|
|
|
waiting[j] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(CPU == NULL && !ready_queue.empty()) {
|
|
|
|
CPU = ready_queue.top();
|
|
|
|
CPU->in_cpu = 1;
|
|
|
|
// Record in_time when the process enters the CPU
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(CPU != NULL){
|
|
|
|
//check cpu_burst complete
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
if(processes[i].in_cpu == 1) {
|
|
|
|
if(CPU->burst_times[processes[i].current_burst_index] == 0){
|
|
|
|
// Record out_time when the process exits the CPU
|
|
|
|
output_file << " " << time.timer << endl;
|
|
|
|
CPU->in_cpu = 0;
|
|
|
|
CPU->current_burst_index++;
|
|
|
|
waiting.push_back(CPU); // process added to waiting queue
|
|
|
|
if(!ready_queue.empty()) {
|
|
|
|
CPU = ready_queue.top(); // process added to CPU
|
|
|
|
CPU->in_cpu = 1;
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue.pop();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CPU = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(CPU != NULL) {
|
|
|
|
// reducing the cpu burst till it reaches 0
|
|
|
|
CPU->burst_times[CPU->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) {
|
|
|
|
// reducing the io burst till it reaches 0
|
|
|
|
waiting[j]->burst_times[waiting[j]->current_burst_index]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.timer++;
|
|
|
|
}
|
|
|
|
output_file.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------- Round Robin --------------------------------------------------
|
2024-09-21 22:54:41 +05:30
|
|
|
// vector<process_detail*> waiting;
|
2024-09-21 05:54:36 +05:30
|
|
|
void round_robin() {
|
2024-09-21 22:54:41 +05:30
|
|
|
//clock initialized to 0
|
2024-09-21 05:54:36 +05:30
|
|
|
struct clock time;
|
|
|
|
memset(&time, 0, sizeof(struct clock));
|
|
|
|
time.timer = 0;
|
2024-09-21 22:54:41 +05:30
|
|
|
time.push_signal = 0;
|
2024-09-21 05:54:36 +05:30
|
|
|
int process_count = processes.size();
|
2024-09-21 22:54:41 +05:30
|
|
|
// memset(&waiting, 0, process_count);
|
2024-09-21 05:54:36 +05:30
|
|
|
int completed_processes = 0;
|
2024-09-21 22:54:41 +05:30
|
|
|
int time_quantum = 5;
|
2024-09-21 05:54:36 +05:30
|
|
|
int current_quantum = 0;
|
2024-09-21 22:54:41 +05:30
|
|
|
|
|
|
|
|
|
|
|
while(completed_processes < process_count){
|
2024-09-21 05:54:36 +05:30
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
|
2024-09-21 05:54:36 +05:30
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
// breaking from the infinite loop
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
if(processes[i].burst_times[processes[i].current_burst_index] == -2) {
|
2024-09-21 05:54:36 +05:30
|
|
|
completed_processes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
//managing arrival times
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
//if process not in cpu
|
|
|
|
if(processes[i].in_cpu != 1) {
|
|
|
|
if(time.timer == processes[i].burst_times[0]) {
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue_fifo.push(&processes[i]);
|
|
|
|
processes[i].current_burst_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-21 22:54:41 +05:30
|
|
|
|
|
|
|
// 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) {
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue_fifo.push(waiting[j]);
|
|
|
|
waiting[j]->current_burst_index++;
|
|
|
|
waiting[j] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
2024-09-21 05:54:36 +05:30
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
if(CPU == NULL && !ready_queue_fifo.empty()) {
|
2024-09-21 05:54:36 +05:30
|
|
|
CPU = ready_queue_fifo.front();
|
|
|
|
CPU->in_cpu = 1;
|
|
|
|
// Record in_time when the process enters the CPU
|
2024-09-21 22:54:41 +05:30
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
2024-09-21 05:54:36 +05:30
|
|
|
ready_queue_fifo.pop();
|
2024-09-21 22:54:41 +05:30
|
|
|
current_quantum = time_quantum;
|
2024-09-21 05:54:36 +05:30
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
else if(CPU != NULL){
|
|
|
|
//check cpu_burst complete
|
|
|
|
for(int i = 0; i < process_count; ++i) {
|
|
|
|
if(processes[i].in_cpu == 1) {
|
|
|
|
if(CPU->burst_times[processes[i].current_burst_index] == 0 || current_quantum == 0){
|
|
|
|
// Record out_time when the process exits the CPU
|
|
|
|
output_file << " " << time.timer << endl;
|
|
|
|
CPU->in_cpu = 0;
|
|
|
|
if(CPU->burst_times[processes[i].current_burst_index] == 0) CPU->current_burst_index++;
|
|
|
|
if(current_quantum == 0) ready_queue_fifo.push(CPU);
|
|
|
|
waiting.push_back(CPU); // process added to waiting queue
|
|
|
|
if(!ready_queue_fifo.empty()) {
|
|
|
|
CPU = ready_queue_fifo.front(); // process added to CPU
|
|
|
|
CPU->in_cpu = 1;
|
|
|
|
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
|
|
|
ready_queue_fifo.pop();
|
|
|
|
current_quantum = time_quantum;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CPU = NULL;
|
|
|
|
}
|
|
|
|
}
|
2024-09-21 05:54:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
|
|
|
|
if(CPU != NULL) {
|
2024-09-21 05:54:36 +05:30
|
|
|
CPU->burst_times[CPU->current_burst_index]--;
|
2024-09-21 22:54:41 +05:30
|
|
|
current_quantum--;
|
2024-09-21 05:54:36 +05:30
|
|
|
}
|
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
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
|
2024-09-21 05:54:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
time.timer++;
|
|
|
|
}
|
2024-09-19 15:33:35 +05:30
|
|
|
output_file.close();
|
2024-09-17 17:37:01 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:54:41 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
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);
|
2024-09-21 22:54:41 +05:30
|
|
|
// ifstream file("process1.dat", ios::binary);
|
2024-09-17 17:37:01 +05:30
|
|
|
string buffer;
|
2024-09-19 15:33:35 +05:30
|
|
|
int pid = 0;
|
2024-09-17 17:37:01 +05:30
|
|
|
|
|
|
|
while(getline(file, buffer)) {
|
|
|
|
if(buffer[0] == '<'){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
istringstream iss(buffer);
|
|
|
|
string word;
|
|
|
|
struct process_detail pd;
|
|
|
|
memset(&pd,0,sizeof(struct process_detail));
|
2024-09-19 15:33:35 +05:30
|
|
|
pd.pid = pid++;
|
|
|
|
pd.current_burst_index = 0;
|
2024-09-17 17:37:01 +05:30
|
|
|
|
|
|
|
while(iss>>word){
|
2024-09-17 17:51:50 +05:30
|
|
|
pd.burst_times.push_back(stoi(word));
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
|
|
|
processes.push_back(pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
map<string, int> temp;
|
|
|
|
temp["fifo"] = 1;
|
2024-09-21 05:54:36 +05:30
|
|
|
temp["sjf"] = 2;
|
|
|
|
temp["pre_sjf"] = 3;
|
|
|
|
temp["rr"] = 4;
|
2024-09-17 17:37:01 +05:30
|
|
|
|
2024-09-21 05:54:36 +05:30
|
|
|
string temp1 = scheduler_algorithm;
|
2024-09-21 22:54:41 +05:30
|
|
|
// string temp1 = "rr";
|
2024-09-17 17:37:01 +05:30
|
|
|
|
|
|
|
switch(temp[temp1]){
|
|
|
|
case 1:
|
|
|
|
fifo();
|
|
|
|
break;
|
2024-09-21 05:54:36 +05:30
|
|
|
case 2:
|
|
|
|
sjf();
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
pre_sjf();
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
round_robin();
|
|
|
|
break;
|
2024-09-17 17:37:01 +05:30
|
|
|
default:
|
2024-09-21 05:54:36 +05:30
|
|
|
cout << "enter fifo or sjf or pre_sjf or rr" << endl;
|
2024-09-17 17:37:01 +05:30
|
|
|
}
|
2024-09-19 15:33:35 +05:30
|
|
|
|
2024-09-17 17:37:01 +05:30
|
|
|
return 0;
|
|
|
|
}
|