-
Notifications
You must be signed in to change notification settings - Fork 0
/
running_state.cpp
95 lines (88 loc) · 3.06 KB
/
running_state.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <sstream>
#include <memory>
#include <time.h>
#include <utility>
#include "utilities.h"
int main(int argc, char *argv[])
{
srand(time(NULL));
process proc(nullptr);
int ready_running = open("ready2running", O_RDONLY);
if (ready_running < 0)
std::cout << "Could not open ready2running in running.\n";
int running_block = open("running2block", O_WRONLY);
if (running_block < 0)
std::cout << "Could not open running2block in running.\n";
int running_ready = open("running2ready", O_WRONLY);
if (running_ready < 0)
std::cout << "Could not open running2ready in running.\n";
int running_exit = open("running2exit", O_WRONLY);
int time_quantum = std::stoi(utils::readFromPipe(ready_running));
std::cout<<"Time quantum received in RUNNING : "<<time_quantum<<"\n";
size_t burst, remaining_burst;
bool block;
//setNonBlock(ready_running);
std::string block_type[3] = {"INPUT", "OUTPUT", "PRINTER"};
std::string data = utils::readFromPipe(ready_running);
while (data != "closed")
{
utils::createProc(data, proc);
std::cout << "RUNNING: " << proc->proc_name << " scheduled.\n";
burst = 0;
if (time_quantum == -1)
remaining_burst = proc->remaining_burst;
else
{
if (time_quantum < proc->remaining_burst)
remaining_burst = time_quantum;
else
remaining_burst = proc->remaining_burst;
}
while (burst < remaining_burst)
{
sleep(1);
burst++;
block = false;
if (burst == 5)
{
block = rand() % 2;
if (block)
{
proc->remaining_burst -= burst;
proc->block_type = rand() % 3;
data = utils::createPacket(proc);
utils::writeToPipe(running_block, data);
std::cout << "RUNNING: " << proc->proc_name << " BLOCKED for "<<
block_type[proc->block_type]<<".\n";
data = "NEXT";
utils::writeToPipe(running_ready, data);
break;
}
}
}
if (!block)
{
proc->remaining_burst -= burst;
data = utils::createPacket(proc);
utils::writeToPipe(running_ready, data);
if (proc->remaining_burst == 0)
{
utils::writeToPipe(running_exit, data);
std::cout << "RUNNING: " << proc->proc_name << " EXITED.\n";
}
}
data = utils::readFromPipe(ready_running);
}
std::cout << "RUNNING: READY STATE CLOSED THE PIPE.\n";
close(ready_running);
close(running_block);
close(running_exit);
close(running_ready);
exit(0);
}