fifo added
This commit is contained in:
parent
a6772502e8
commit
97d0d231e1
|
@ -0,0 +1,124 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Process {
|
||||
int pid;
|
||||
int arrival_time;
|
||||
vector<int> burst_times;
|
||||
int current_burst_index;
|
||||
int completion_time;
|
||||
int waiting_time;
|
||||
int turnaround_time;
|
||||
bool in_cpu;
|
||||
};
|
||||
|
||||
vector<Process> processes;
|
||||
|
||||
void fifo() {
|
||||
queue<Process*> ready_queue;
|
||||
int current_time = 0;
|
||||
int completed_processes = 0;
|
||||
int process_count = processes.size();
|
||||
|
||||
while (completed_processes < process_count) {
|
||||
// Add processes to the ready queue based on arrival time
|
||||
for (auto& process : processes) {
|
||||
if (process.arrival_time <= current_time && !process.in_cpu) {
|
||||
ready_queue.push(&process);
|
||||
process.in_cpu = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ready_queue.empty()) {
|
||||
Process* current_process = ready_queue.front();
|
||||
ready_queue.pop();
|
||||
|
||||
// Simulate CPU execution
|
||||
for (int i = current_process->current_burst_index; i < current_process->burst_times.size(); i += 2) {
|
||||
int cpu_burst = current_process->burst_times[i];
|
||||
current_time += cpu_burst; // Advance time by CPU burst duration
|
||||
current_process->current_burst_index++;
|
||||
|
||||
// Handle I/O burst if there's one
|
||||
if (i + 1 < current_process->burst_times.size()) {
|
||||
int io_burst = current_process->burst_times[i + 1];
|
||||
current_time += io_burst; // Advance time by I/O burst duration
|
||||
}
|
||||
}
|
||||
|
||||
current_process->completion_time = current_time;
|
||||
current_process->turnaround_time = current_process->completion_time - current_process->arrival_time;
|
||||
current_process->waiting_time = current_process->turnaround_time - (current_process->burst_times.size() / 2);
|
||||
completed_processes++;
|
||||
} else {
|
||||
// No process is ready; advance time
|
||||
current_time++;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate averages
|
||||
int total_waiting_time = 0, total_turnaround_time = 0;
|
||||
for (const auto& process : processes) {
|
||||
total_waiting_time += process.waiting_time;
|
||||
total_turnaround_time += process.turnaround_time;
|
||||
}
|
||||
|
||||
double avg_waiting_time = static_cast<double>(total_waiting_time) / process_count;
|
||||
double avg_turnaround_time = static_cast<double>(total_turnaround_time) / process_count;
|
||||
|
||||
// Output results
|
||||
cout << "FIFO Scheduling Results:\n";
|
||||
cout << "Processes:\n";
|
||||
for (const auto& process : processes) {
|
||||
cout << "Process ID: " << process.pid
|
||||
<< ", Completion Time: " << process.completion_time
|
||||
<< ", Waiting Time: " << process.waiting_time
|
||||
<< ", Turnaround Time: " << process.turnaround_time << endl;
|
||||
}
|
||||
|
||||
cout << "Average Waiting Time: " << fixed << setprecision(2) << avg_waiting_time << endl;
|
||||
cout << "Average Turnaround Time: " << fixed << setprecision(2) << avg_turnaround_time << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 3) {
|
||||
cout << "Usage: ./scheduler.out <path-to-workload-file> <scheduler_algorithm>\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
ifstream file(argv[1]);
|
||||
string line;
|
||||
int pid = 0;
|
||||
|
||||
while (getline(file, line)) {
|
||||
if (line.empty()) continue;
|
||||
Process process;
|
||||
process.pid = pid++;
|
||||
process.current_burst_index = 0;
|
||||
process.in_cpu = false;
|
||||
|
||||
istringstream iss(line);
|
||||
iss >> process.arrival_time;
|
||||
|
||||
int burst_time;
|
||||
while (iss >> burst_time && burst_time != -1) {
|
||||
process.burst_times.push_back(burst_time);
|
||||
}
|
||||
processes.push_back(process);
|
||||
}
|
||||
|
||||
string algorithm = argv[2];
|
||||
if (algorithm == "fifo") {
|
||||
fifo();
|
||||
} else {
|
||||
cout << "Invalid scheduling algorithm. Please use 'fifo'.\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
P1 0 2
|
||||
P2 2 4
|
||||
P1 6 7
|
||||
P2 9 10
|
|
@ -0,0 +1,225 @@
|
|||
#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_cpu;
|
||||
int ptr;
|
||||
};
|
||||
|
||||
struct clock{
|
||||
int push_signal; //boolean
|
||||
int timer;
|
||||
|
||||
};
|
||||
|
||||
//// operator overloading
|
||||
//struct CompareHeight {
|
||||
// bool operator()(struct process_detail p1, struct process_detail p2)
|
||||
// {
|
||||
// // return "true" if "p1" is ordered
|
||||
// // before "p2", for example:
|
||||
// return p1.height < p2.height;
|
||||
// }
|
||||
//};
|
||||
|
||||
|
||||
vector<struct process_detail> processes;
|
||||
vector<struct process_detail> ready_queue;
|
||||
queue<struct process_detail*> ready_queue_fifo;
|
||||
vector<struct process_detail*> waiting;
|
||||
struct process_detail* CPU = NULL;
|
||||
|
||||
void fifo() {
|
||||
|
||||
//clock initialized to 0
|
||||
struct clock time;
|
||||
memset(&time, 0, sizeof(struct clock));
|
||||
int process_count = processes.size();
|
||||
|
||||
//ready queue initialized as process 1 will arrive at time 0
|
||||
ready_queue_fifo.push(&processes[0]);
|
||||
processes[0].ptr++;
|
||||
|
||||
int brk = 0;
|
||||
|
||||
while(true){
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
if(processes[i].burst_times[processes[i].ptr] == -1) {
|
||||
brk = 1;
|
||||
}
|
||||
else brk = 0;
|
||||
}
|
||||
if(brk) break;
|
||||
|
||||
//managing arrival times
|
||||
for(int i = 1; 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_fifo.push(&processes[i]);
|
||||
processes[i].ptr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//THE FIFO RULE
|
||||
if(CPU == NULL) {
|
||||
CPU = ready_queue_fifo.front();
|
||||
CPU->in_cpu = 1;
|
||||
ready_queue_fifo.pop();
|
||||
}
|
||||
else {
|
||||
//check cpu_burst complete
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
if(processes[i].in_cpu == 1) {
|
||||
if(time.push_signal + CPU->burst_times[processes[i].ptr] == time.timer){
|
||||
waiting.push_back(CPU); // process added to waiting queue
|
||||
CPU->in_cpu = 0;
|
||||
CPU = ready_queue_fifo.front(); // process added to CPU
|
||||
CPU->in_cpu = 1;
|
||||
ready_queue_fifo.pop();
|
||||
time.push_signal = time.push_signal + CPU->burst_times[processes[i].ptr] ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// removing form waiting list
|
||||
|
||||
for(int j = 0; j < waiting.size(); ++j) {
|
||||
if(waiting[j] != NULL) {
|
||||
if(waiting[j]->burst_times[waiting[j]->ptr] == 0) {
|
||||
ready_queue_fifo.push(waiting[j]);
|
||||
waiting[j]->ptr++;
|
||||
waiting[j] = NULL;
|
||||
}
|
||||
else waiting[j]->burst_times[waiting[j]->ptr]--; // reducing the io burst till it reaches 0
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
time.timer++;
|
||||
}
|
||||
|
||||
cout << "fifo" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
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.ptr = 0;
|
||||
|
||||
while(iss>>word){
|
||||
// if(i == 0){
|
||||
// pd.cpu_burst_times.push_back(stoi(word));
|
||||
// }
|
||||
// else if(i % 2 == 0){
|
||||
// pd.io_burst_times.push_back(stoi(word));
|
||||
// }
|
||||
// else if(i % 2 == 1){
|
||||
// }
|
||||
pd.burst_times.push_back(stoi(word));
|
||||
// i++;
|
||||
// cout << stoi(word) << endl;
|
||||
}
|
||||
processes.push_back(pd);
|
||||
}
|
||||
|
||||
map<string, int> temp;
|
||||
temp["fifo"] = 1;
|
||||
string temp1 = scheduler_algorithm;
|
||||
|
||||
|
||||
switch(temp[temp1]){
|
||||
case 1:
|
||||
fifo();
|
||||
break;
|
||||
default:
|
||||
cout << "enter fifo" << endl;
|
||||
}
|
||||
|
||||
cout << processes[0].in_cpu << endl;
|
||||
cout << processes[0].ptr << endl;
|
||||
|
||||
cout << processes[1].in_cpu << endl;
|
||||
cout << processes[1].ptr << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
I am writing the above code to as an answer for the following question:
|
||||
Process Scheduling
|
||||
Laboratory 3
|
||||
Duration: 3 weeks
|
||||
This assignment will help us learn different process scheduling algorithms and their relative pros
|
||||
and cons.
|
||||
To do this task, you will need to develop a simulator of a scheduler in C / C++. The simulator
|
||||
must take in the following command line arguments: <scheduling-algorithm>
|
||||
<path-to-workload-description-file>. The simulator must produce as output the following metrics:
|
||||
Makespan, Completion Time (average and maximum), and Waiting Time (average and
|
||||
maximum), Run Time of your simulator (not counting I/O). Also, report the schedule itself
|
||||
(choose a nice format which will also help you debug).
|
||||
For all the studies, we will use the workload description files given here. Each row in the file
|
||||
refers to one process. The row format is as follows:
|
||||
<process-arrival-time> <cpu-burst-1-duration> <io-burst-1-duration> <cpu-burst-2-duration>
|
||||
<io-burst-2-duration> … -1
|
||||
For example:
|
||||
0 100 2 200 3 25 -1 indicates arrival time = 0; CPU burst 1 duration = 100; I/O burst 1 duration =
|
||||
2; CPU burst 2 duration = 200; I/O burst 2 duration = 3; CPU burst 3 duration = 25; end of
|
||||
process.
|
||||
Assume that every line ends with -1. A process may have any number of CPU / I/O burst
|
||||
cycles terminated with a -1. There will be any number of processes, terminated by an end of file.
|
||||
The arrival times are in nondecreasing order.
|
||||
Part I
|
||||
Implement the following algorithms:
|
||||
A. First In First Out
|
||||
|
||||
|
||||
Also here is the input file:
|
||||
<html>
|
||||
<body>
|
||||
<pre>
|
||||
0 100 2 -1
|
||||
2 80 2 -1
|
||||
</pre></body></html>
|
||||
|
||||
|
||||
Help me write the appropriate code
|
Binary file not shown.
|
@ -13,9 +13,8 @@ struct process_detail {
|
|||
//cpu_burst_times[0] is arrival time
|
||||
int pid;
|
||||
vector<int> burst_times;
|
||||
// vector<int> io_burst_times;
|
||||
int in_cpu;
|
||||
int ptr = 0;
|
||||
int current_burst_index;
|
||||
};
|
||||
|
||||
struct clock{
|
||||
|
@ -35,66 +34,112 @@ struct clock{
|
|||
//};
|
||||
|
||||
|
||||
vector<struct process_detail> processes;
|
||||
vector<struct process_detail> ready_queue;
|
||||
queue<struct process_detail> ready_queue_fifo;
|
||||
vector<struct process_detail> waiting;
|
||||
vector<process_detail> processes;
|
||||
queue<process_detail*> ready_queue_fifo;
|
||||
vector<process_detail*> waiting;
|
||||
struct process_detail* CPU = NULL;
|
||||
int clock = 0;
|
||||
|
||||
ofstream output_file("cpu_times.txt");
|
||||
|
||||
void fifo() {
|
||||
|
||||
//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;
|
||||
|
||||
//ready queue initialized as process 1 will arrive at time 0
|
||||
ready_queue_fifo.push(processes[0]);
|
||||
processes[0].i++;
|
||||
while(completed_processes < process_count){
|
||||
|
||||
while(true){
|
||||
// breaking from the infinite loop
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
if(processes[i].burst_times[processes[i].current_burst_index] == -1) {
|
||||
completed_processes++;
|
||||
}
|
||||
}
|
||||
|
||||
//managing arrival times
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
//if process not in cpu
|
||||
if(proccesses[i].in_cpu != 1) {
|
||||
if(processes[i].in_cpu != 1) {
|
||||
if(time.timer == processes[i].burst_times[0]) {
|
||||
ready_queue_fifo.push(processes[ptr]);
|
||||
processes[i].i++;
|
||||
ready_queue_fifo.push(&processes[i]);
|
||||
processes[i].current_burst_index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//THE FIFO RULE
|
||||
if(CPU == NULL) {
|
||||
if(CPU == NULL && !ready_queue_fifo.empty()) {
|
||||
CPU = ready_queue_fifo.front();
|
||||
CPU->in_cpu = 1;
|
||||
// Record in_time when the process enters the CPU
|
||||
CPU->burst_times[CPU->current_burst_index]--;
|
||||
output_file << "P" << CPU->pid+1 << " " << time.timer;
|
||||
ready_queue_fifo.pop();
|
||||
|
||||
}
|
||||
else{
|
||||
// else if(CPU == NULL && ready_queue_fifo.empty()) {
|
||||
// // removing form waiting list
|
||||
// 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;
|
||||
// }
|
||||
// else waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0
|
||||
// }
|
||||
|
||||
// }
|
||||
// time.push_signal++;
|
||||
// }
|
||||
else {
|
||||
// removing form waiting list
|
||||
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;
|
||||
}
|
||||
else waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0
|
||||
}
|
||||
|
||||
}
|
||||
//check cpu_burst complete
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
if(proccesses[i].in_cpu == 1) {
|
||||
if(timer.push_signal + CPU->burst_times[ptr] == time.timer){
|
||||
waiting.push_back(CPU); // process added to waiting queue
|
||||
if(processes[i].in_cpu == 1 && CPU != NULL) {
|
||||
if(CPU->burst_times[processes[i].current_burst_index] == 0){
|
||||
// Record out_time when the process exits the CPU
|
||||
output_file << " " << time.timer << endl;
|
||||
// time.push_signal = time.push_signal + CPU->burst_times[processes[i].current_burst_index] ;
|
||||
CPU->in_cpu = 0;
|
||||
CPU = ready_queue_fifo.front(); // process added to CPU
|
||||
CPU->in_cpu = 1;
|
||||
ready_queue_fifo.pop();
|
||||
timer.push_signal = timer.push_signal + CPU->burst_times[ptr] ;
|
||||
CPU->current_burst_index++;
|
||||
// CPU->burst_times[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;
|
||||
// CPU->burst_times[CPU->current_burst_index]--;
|
||||
output_file << "P" << CPU->pid+1 << " " << time.timer; // New entry time
|
||||
ready_queue_fifo.pop();
|
||||
}
|
||||
else {
|
||||
CPU = NULL;
|
||||
}
|
||||
}
|
||||
else CPU->burst_times[CPU->current_burst_index]--;
|
||||
}
|
||||
}
|
||||
|
||||
// removing form wait
|
||||
|
||||
|
||||
}
|
||||
time.timer++;
|
||||
|
||||
// completed_processes++;
|
||||
}
|
||||
|
||||
output_file.close();
|
||||
cout << "fifo" << endl;
|
||||
return;
|
||||
}
|
||||
|
@ -113,8 +158,9 @@ int main(int argc, char **argv) {
|
|||
char *scheduler_algorithm = argv[2];
|
||||
|
||||
ifstream file(file_to_search_in, ios::binary);
|
||||
// ifstream file("process1.dat", ios::binary);
|
||||
string buffer;
|
||||
int pid = 1;
|
||||
int pid = 0;
|
||||
|
||||
while(getline(file, buffer)) {
|
||||
if(buffer[0] == '<'){
|
||||
|
@ -124,6 +170,8 @@ int main(int argc, char **argv) {
|
|||
string word;
|
||||
struct process_detail pd;
|
||||
memset(&pd,0,sizeof(struct process_detail));
|
||||
pd.pid = pid++;
|
||||
pd.current_burst_index = 0;
|
||||
|
||||
while(iss>>word){
|
||||
// if(i == 0){
|
||||
|
@ -138,13 +186,13 @@ int main(int argc, char **argv) {
|
|||
// i++;
|
||||
// cout << stoi(word) << endl;
|
||||
}
|
||||
pd.pid = pid;
|
||||
processes.push_back(pd);
|
||||
}
|
||||
|
||||
map<string, int> temp;
|
||||
temp["fifo"] = 1;
|
||||
string temp1 = scheduler_algorithm;
|
||||
// string temp1 = "fifo";
|
||||
|
||||
|
||||
switch(temp[temp1]){
|
||||
|
@ -154,5 +202,16 @@ int main(int argc, char **argv) {
|
|||
default:
|
||||
cout << "enter fifo" << endl;
|
||||
}
|
||||
|
||||
// cout << processes[0].in_cpu << endl;
|
||||
// cout << processes[0].current_burst_index << endl;
|
||||
|
||||
// cout << processes[1].in_cpu << endl;
|
||||
// cout << processes[1].current_burst_index << endl;
|
||||
|
||||
// cout << ready_queue_fifo.front()->pid << endl;
|
||||
// ready_queue_fifo.pop();
|
||||
// cout << ready_queue_fifo.front()->pid << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
<html>
|
||||
<body>
|
||||
<pre>
|
||||
0 2 2 1 -1
|
||||
1 2 2 1 -1
|
||||
</pre></body></html>
|
Loading…
Reference in New Issue