single cpu completed
This commit is contained in:
parent
b2ae341467
commit
296c926b7e
|
@ -1,6 +1,95 @@
|
|||
{
|
||||
"files.associations": {
|
||||
"ostream": "cpp",
|
||||
"fstream": "cpp"
|
||||
"fstream": "cpp",
|
||||
"any": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"barrier": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"bitset": "cpp",
|
||||
"cctype": "cpp",
|
||||
"cfenv": "cpp",
|
||||
"charconv": "cpp",
|
||||
"chrono": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"complex": "cpp",
|
||||
"concepts": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"coroutine": "cpp",
|
||||
"csetjmp": "cpp",
|
||||
"csignal": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cuchar": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"set": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"ratio": "cpp",
|
||||
"regex": "cpp",
|
||||
"source_location": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"future": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"latch": "cpp",
|
||||
"limits": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ranges": "cpp",
|
||||
"scoped_allocator": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"shared_mutex": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"syncstream": "cpp",
|
||||
"thread": "cpp",
|
||||
"typeindex": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"valarray": "cpp",
|
||||
"variant": "cpp",
|
||||
"expected": "cpp",
|
||||
"format": "cpp",
|
||||
"spanstream": "cpp",
|
||||
"stacktrace": "cpp",
|
||||
"stdfloat": "cpp"
|
||||
}
|
||||
}
|
|
@ -8,4 +8,4 @@
|
|||
5 3 2 3 2 3 -1
|
||||
6 5 -1
|
||||
10 200 2 3 -1
|
||||
</pre></body></html>
|
||||
</pre></body></html>
|
|
@ -1,6 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
<pre>
|
||||
0 2 2 1 -1
|
||||
0 20 2 1 -1
|
||||
1 2 2 1 -1
|
||||
2 1 2 1 -1
|
||||
</pre></body></html>
|
|
@ -1,58 +1,9 @@
|
|||
P1,1 0 2
|
||||
P2,0 2 3
|
||||
P3,0 3 4
|
||||
P4,0 4 5
|
||||
P5,0 5 6
|
||||
P5,1 6 8
|
||||
P6,1 8 10
|
||||
P6,1 10 10
|
||||
P5,1 10 13
|
||||
P6,1 13 15
|
||||
P5,2 15 18
|
||||
P6,1 18 19
|
||||
P4,1 19 28
|
||||
P3,1 28 30
|
||||
P4,1 30 90
|
||||
P3,1 90 92
|
||||
P4,2 92 122
|
||||
P3,1 122 125
|
||||
P4,3 125 195
|
||||
P3,1 195 197
|
||||
P4,4 197 207
|
||||
P3,1 207 209
|
||||
P4,5 209 219
|
||||
P3,1 219 277
|
||||
P2,1 277 279
|
||||
P3,1 279 349
|
||||
P2,1 349 351
|
||||
P3,2 351 391
|
||||
P2,1 391 394
|
||||
P3,3 394 464
|
||||
P2,1 464 466
|
||||
P3,4 466 486
|
||||
P2,1 486 488
|
||||
P3,5 488 498
|
||||
P2,1 498 566
|
||||
P1,1 566 568
|
||||
P2,1 568 648
|
||||
P1,1 648 650
|
||||
P2,2 650 700
|
||||
P1,1 700 703
|
||||
P2,3 703 773
|
||||
P1,1 773 775
|
||||
P2,4 775 815
|
||||
P1,1 815 817
|
||||
P2,5 817 827
|
||||
P1,1 827 914
|
||||
P7,1 914 916
|
||||
P1,1 916 1006
|
||||
P7,1 1006 1008
|
||||
P1,2 1008 1088
|
||||
P7,1 1088 1091
|
||||
P1,3 1091 1161
|
||||
P7,1 1161 1163
|
||||
P1,4 1163 1223
|
||||
P7,1 1223 1225
|
||||
P1,5 1225 1235
|
||||
P7,1 1235 1424
|
||||
P7,2 1426 1429
|
||||
P1,1 0 5
|
||||
P2,1 5 7
|
||||
P3,1 7 8
|
||||
P1,1 8 13
|
||||
P2,2 13 14
|
||||
P3,2 14 15
|
||||
P1,1 15 20
|
||||
P1,1 20 25
|
||||
P1,2 27 28
|
||||
|
|
Binary file not shown.
|
@ -15,6 +15,10 @@ struct process_detail {
|
|||
vector<int> burst_times;
|
||||
int in_cpu;
|
||||
int current_burst_index;
|
||||
int arrvival_time = 0;
|
||||
int wait_time = 0;
|
||||
int cpu_time = 0;
|
||||
int completion_time = 0;
|
||||
};
|
||||
|
||||
struct clock{
|
||||
|
@ -26,9 +30,10 @@ struct clock{
|
|||
vector<process_detail> processes;
|
||||
queue<process_detail*> ready_queue_fifo;
|
||||
struct process_detail* CPU = NULL;
|
||||
vector<process_detail*> waiting;
|
||||
|
||||
ofstream output_file("cpu_times.txt");
|
||||
vector<string> out_strings;
|
||||
|
||||
|
||||
// -------------------------------------- THE FIFO --------------------------------------------------
|
||||
|
||||
|
@ -41,23 +46,28 @@ void fifo() {
|
|||
time.push_signal = 0;
|
||||
int process_count = processes.size();
|
||||
int completed_processes = 0;
|
||||
vector<process_detail*> waiting(process_count, NULL);
|
||||
string out_string = "";
|
||||
|
||||
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) {
|
||||
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 process not in cpu
|
||||
if(processes[i].in_cpu != 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++;
|
||||
}
|
||||
|
@ -79,7 +89,8 @@ void fifo() {
|
|||
CPU = ready_queue_fifo.front();
|
||||
CPU->in_cpu = 1;
|
||||
// Record in_time when the process enters the CPU
|
||||
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1 ) / 2<< " " << time.timer;
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->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();
|
||||
|
||||
}
|
||||
|
@ -88,16 +99,20 @@ void fifo() {
|
|||
//check cpu_burst complete
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
if(processes[i].in_cpu == 1) {
|
||||
processes[i].cpu_time += 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;
|
||||
out_string += " " + to_string(time.timer);
|
||||
out_strings.push_back(out_string);
|
||||
// output_file << " " << time.timer << endl;
|
||||
CPU->in_cpu = 0;
|
||||
CPU->current_burst_index++;
|
||||
waiting.push_back(CPU); // process added to waiting queue
|
||||
waiting[CPU->pid] = 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
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
||||
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
ready_queue_fifo.pop();
|
||||
}
|
||||
else {
|
||||
|
@ -123,7 +138,6 @@ void fifo() {
|
|||
|
||||
time.timer++;
|
||||
}
|
||||
output_file.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -134,7 +148,16 @@ void fifo() {
|
|||
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];
|
||||
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];
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -149,17 +172,22 @@ void sjf() {
|
|||
time.push_signal = 0;
|
||||
int process_count = processes.size();
|
||||
int completed_processes = 0;
|
||||
// Initialize waiting vector with NULLs for each process slot
|
||||
vector<process_detail*> waiting(process_count, NULL);
|
||||
string out_string = "";
|
||||
|
||||
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) {
|
||||
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) {
|
||||
|
@ -173,7 +201,7 @@ void sjf() {
|
|||
}
|
||||
|
||||
// managing waiting queue
|
||||
for(int j = 0; j < waiting.size(); ++j) {
|
||||
for(int j = 0; j < process_count; ++j) {
|
||||
if(waiting[j] != NULL) {
|
||||
if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
|
||||
ready_queue.push(waiting[j]);
|
||||
|
@ -187,24 +215,29 @@ void sjf() {
|
|||
CPU = ready_queue.top();
|
||||
CPU->in_cpu = 1;
|
||||
// Record in_time when the process enters the CPU
|
||||
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->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.pop();
|
||||
}
|
||||
|
||||
else if(CPU != NULL){
|
||||
if(CPU != NULL){
|
||||
//check cpu_burst complete
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
if(processes[i].in_cpu == 1) {
|
||||
processes[i].cpu_time += 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;
|
||||
out_string += " " + to_string(time.timer);
|
||||
out_strings.push_back(out_string);
|
||||
// output_file << " " << time.timer << endl;
|
||||
CPU->in_cpu = 0;
|
||||
CPU->current_burst_index++;
|
||||
waiting.push_back(CPU); // process added to waiting queue
|
||||
waiting[CPU->pid] = CPU; // process added to waiting queue
|
||||
if(!ready_queue.empty()) {
|
||||
CPU = ready_queue.top(); // 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
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
||||
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
ready_queue.pop();
|
||||
}
|
||||
else {
|
||||
|
@ -232,7 +265,6 @@ void sjf() {
|
|||
|
||||
time.timer++;
|
||||
}
|
||||
output_file.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -247,17 +279,22 @@ void pre_sjf() {
|
|||
time.push_signal = 0;
|
||||
int process_count = processes.size();
|
||||
int completed_processes = 0;
|
||||
// Initialize waiting vector with NULLs for each process slot
|
||||
vector<process_detail*> waiting(process_count, NULL);
|
||||
string out_string = "";
|
||||
|
||||
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) {
|
||||
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) {
|
||||
|
@ -268,10 +305,13 @@ void pre_sjf() {
|
|||
if(CPU != NULL) {
|
||||
ready_queue.push(CPU);
|
||||
CPU->in_cpu = 0;
|
||||
output_file << " " << time.timer << endl;
|
||||
out_string += " " + to_string(time.timer);
|
||||
out_strings.push_back(out_string);
|
||||
// output_file << " " << time.timer << endl;
|
||||
CPU = ready_queue.top();
|
||||
CPU->in_cpu = 1;
|
||||
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
||||
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
ready_queue.pop();
|
||||
|
||||
}
|
||||
|
@ -281,22 +321,25 @@ void pre_sjf() {
|
|||
}
|
||||
|
||||
// managing waiting queue
|
||||
for(int j = 0; j < waiting.size(); ++j) {
|
||||
for(int j = 0; j < process_count; ++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;
|
||||
out_string += " " + to_string(time.timer);
|
||||
out_strings.push_back(out_string);
|
||||
// output_file << " " << time.timer << endl;
|
||||
CPU = ready_queue.top();
|
||||
CPU->in_cpu = 1;
|
||||
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
||||
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
ready_queue.pop();
|
||||
|
||||
}
|
||||
waiting[j]->current_burst_index++;
|
||||
waiting[j] = NULL;
|
||||
waiting[j] = NULL; // Process leaves waiting queue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -305,24 +348,29 @@ void pre_sjf() {
|
|||
CPU = ready_queue.top();
|
||||
CPU->in_cpu = 1;
|
||||
// Record in_time when the process enters the CPU
|
||||
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->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.pop();
|
||||
}
|
||||
|
||||
else if(CPU != NULL){
|
||||
if(CPU != NULL){
|
||||
//check cpu_burst complete
|
||||
for(int i = 0; i < process_count; ++i) {
|
||||
if(processes[i].in_cpu == 1) {
|
||||
processes[i].cpu_time += 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;
|
||||
out_string += " " + to_string(time.timer);
|
||||
out_strings.push_back(out_string);
|
||||
// output_file << " " << time.timer << endl;
|
||||
CPU->in_cpu = 0;
|
||||
CPU->current_burst_index++;
|
||||
waiting.push_back(CPU); // process added to waiting queue
|
||||
waiting[CPU->pid] = CPU; // process added to waiting queue
|
||||
if(!ready_queue.empty()) {
|
||||
CPU = ready_queue.top(); // 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
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
|
||||
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
ready_queue.pop();
|
||||
}
|
||||
else {
|
||||
|
@ -340,6 +388,7 @@ void pre_sjf() {
|
|||
}
|
||||
|
||||
for(int j = 0; j < waiting.size(); ++j) {
|
||||
// if(waiting[j] != NULL) waiting[j]->wait_time += 1;
|
||||
if(waiting[j] != NULL) {
|
||||
if(waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) {
|
||||
// reducing the io burst till it reaches 0
|
||||
|
@ -350,7 +399,6 @@ void pre_sjf() {
|
|||
|
||||
time.timer++;
|
||||
}
|
||||
output_file.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -358,110 +406,109 @@ void pre_sjf() {
|
|||
// ------------------------------------------- Round Robin --------------------------------------------------
|
||||
// vector<process_detail*> waiting;
|
||||
void round_robin() {
|
||||
//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();
|
||||
// memset(&waiting, 0, process_count);
|
||||
int completed_processes = 0;
|
||||
int time_quantum = 5;
|
||||
int current_quantum = 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;
|
||||
int time_quantum = 5;
|
||||
int current_quantum = 0;
|
||||
|
||||
while(completed_processes < process_count){
|
||||
// Initialize waiting vector with NULLs for each process slot
|
||||
vector<process_detail*> waiting(process_count, NULL);
|
||||
string out_string = "";
|
||||
|
||||
// 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) {
|
||||
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 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].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;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Manage arrival times
|
||||
for (int i = 0; i < process_count; ++i) {
|
||||
if (processes[i].in_cpu != 1 && time.timer == processes[i].burst_times[0]) {
|
||||
ready_queue_fifo.push(&processes[i]);
|
||||
processes[i].current_burst_index++;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
|
||||
ready_queue_fifo.pop();
|
||||
current_quantum = time_quantum;
|
||||
// Manage waiting queue
|
||||
for (int j = 0; j < process_count; ++j) {
|
||||
if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
|
||||
ready_queue_fifo.push(waiting[j]);
|
||||
waiting[j]->current_burst_index++;
|
||||
waiting[j] = NULL; // Process leaves waiting queue
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// Assign a process to CPU if available
|
||||
if (CPU == NULL && !ready_queue_fifo.empty()) {
|
||||
CPU = ready_queue_fifo.front();
|
||||
CPU->in_cpu = 1;
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->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_quantum = time_quantum;
|
||||
}
|
||||
|
||||
else if(CPU != NULL){
|
||||
//check cpu_burst complete
|
||||
if (CPU != NULL) {
|
||||
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;
|
||||
if(processes[i].in_cpu == 1){
|
||||
processes[i].cpu_time += 1;
|
||||
if (CPU->burst_times[CPU->current_burst_index] == 0 || current_quantum == 0) {
|
||||
// output_file << " " << time.timer << endl;
|
||||
out_string += " " + to_string(time.timer);
|
||||
out_strings.push_back(out_string);
|
||||
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
|
||||
if (CPU->burst_times[CPU->current_burst_index] == 0){
|
||||
CPU->current_burst_index++;
|
||||
waiting[CPU->pid] = CPU;
|
||||
}
|
||||
|
||||
else if (current_quantum == 0) ready_queue_fifo.push(CPU);
|
||||
|
||||
// Place the process in its corresponding waiting slot by pid
|
||||
|
||||
if (!ready_queue_fifo.empty()) {
|
||||
CPU = ready_queue_fifo.front();
|
||||
CPU->in_cpu = 1;
|
||||
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
|
||||
out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->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_quantum = time_quantum;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
CPU = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process CPU burst and quantum
|
||||
if (CPU != NULL) {
|
||||
CPU->burst_times[CPU->current_burst_index]--;
|
||||
current_quantum--;
|
||||
}
|
||||
|
||||
if(CPU != NULL) {
|
||||
CPU->burst_times[CPU->current_burst_index]--;
|
||||
current_quantum--;
|
||||
}
|
||||
// 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]--;
|
||||
}
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
time.timer++;
|
||||
}
|
||||
output_file.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
if(argc != 3)
|
||||
|
@ -476,7 +523,7 @@ 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);
|
||||
// ifstream file("../WorkloadFiles/process1.dat", ios::binary);
|
||||
string buffer;
|
||||
int pid = 0;
|
||||
|
||||
|
@ -504,7 +551,10 @@ int main(int argc, char **argv) {
|
|||
temp["rr"] = 4;
|
||||
|
||||
string temp1 = scheduler_algorithm;
|
||||
// string temp1 = "rr";
|
||||
// string temp1 = "sjf";
|
||||
|
||||
// Start time point
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
switch(temp[temp1]){
|
||||
case 1:
|
||||
|
@ -523,5 +573,23 @@ int main(int argc, char **argv) {
|
|||
cout << "enter fifo or sjf or pre_sjf or rr" << endl;
|
||||
}
|
||||
|
||||
// End time point
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
||||
|
||||
|
||||
for(int i = 0; i < out_strings.size(); ++i){
|
||||
output_file << out_strings[i] << endl;
|
||||
}
|
||||
output_file.close();
|
||||
for(int i = 0; i < processes.size(); ++i) {
|
||||
cout << "Process " << i+1 << " Completion Time: " << processes[i].completion_time << endl;
|
||||
}
|
||||
for(int i = 0; i < processes.size(); ++i) {
|
||||
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;
|
||||
}
|
||||
|
||||
std::cout << "Execution time: " << duration.count() << " ms" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue