-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOS1.cpp
More file actions
215 lines (205 loc) · 5.5 KB
/
OS1.cpp
File metadata and controls
215 lines (205 loc) · 5.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<cstring>
#include<readline/readline.h>
#include<readline/history.h>
using namespace std;
int main() {
int i = -1; //Number of Processes
bool ops = true; //to run while loop
char* in = NULL; //to take input command from user
int status;
pid_t proc[128]; //array to store processes
int stat[128]; // array to store status of process id 1:running; 3:stopped; 0:terminated
pid_t endID[128]; // array to store status returned from waitpid() of all processes
int count = 0; //to maintain count of running processes
pid_t pid;
char* proc_names[128]; //store process names
while (ops)
{
for (int j = 0; j <= i; j++)
{
endID[j] = waitpid(proc[j], &status, WNOHANG); //check change in any of the child processes immediately without waiting using WNOHANG
if (endID[j] > 0 && stat[j] != 0)
{
stat[j] = 0;
cout << proc[j] << " completed" << endl; //to check if any process has been completed
count--;
for (int k = 0; k <= i; k++)
{ // as soon as a child process is completed automatically restart a stopped child process if present
if (count < 3 && stat[k] == 3)
{
stat[k] = 1;
kill(proc[k], SIGCONT); //automatically starting a stopped process
count++;
cout << proc[k] << " automatically restarted" << endl;
}
}
}
}
in = readline("BP> ");
while (strcmp(in,"")==0){
in = readline("BP> ");
}
if (strcmp(in, "exit") == 0)
{
for (int j = 0; j <= i; j++)
{
if (stat[j] != 0)
{
kill(proc[j], SIGTERM); //if command is exit then kill all non-terminated processes i.e status should not be 0(terminated)
cout << proc[j] << " killed" << endl;
}
}
ops = false; //end loop
break;
}
char* p = strtok(in, " \t");
if (strcmp(p, "bg") == 0)
{
i++; //increasing array index
p = strtok(NULL, " \t");
proc_names[i] = p;
char child[] = "./";
strcat(child, proc_names[i]);
char* arg_array[] = { child,NULL,NULL,NULL,NULL }; //array to be sent as parameter during execvp()
p = strtok(NULL, " \t");
arg_array[1] = p;
p = strtok(NULL, " \t");
arg_array[2] = p;
p = strtok(NULL, " \t");
arg_array[3] = p; //storing parameters of input in array
pid = fork(); //create a process
if (pid > 0) //parent process
{
proc[i] = pid;
if (count < 3) { //if less than 3 process running increment count and continue normally
count++;
stat[i] = 1; //set status to running
}
else {
kill(pid, SIGSTOP); //if 3 processes already running then stop
stat[i] = 3; //set status to stopped
}
}
else if (pid == 0) //child process
{
if (execvp(arg_array[0], arg_array) < 0) //execute execvp()
{
cout << "Fail" << endl;
}
}
else
{
cout << "fork failed" << endl;
exit(EXIT_FAILURE);
}
}
else if (strcmp(p, "bglist") == 0)
{
int j = 0;
for (j = 0; j <= i; j++) //if command is bglist display processes and their status
{
if (stat[j] == 1)
{
cout << proc[j] << " : "<<proc_names[j]<<" (running)" << endl;
}
else if (stat[j] == 0)
{
cout << proc[j] << " : " << proc_names[j] << " (terminated)" << endl;
}
else if (stat[j] == 3)
{
cout << proc[j] << " : " << proc_names[j] << " (stopped)" << endl;
}
}
}
else if (strcmp(p, "bgstop") == 0)
{
p = strtok(NULL, " \t");
bool found = false; //to check if process exist
for (int j = 0; j <= i; j++)
{
if (proc[j] == atoi(p))
{
if (stat[j] == 3)
{
cout << proc[j] << " already stopped" << endl; //if already stopped then display message and break
found = true;
break;
}
else if (stat[j] == 0)
{
cout << proc[j] << " already terminated" << endl; //if already stopped then display message and break
found = true;
break;
}
else
{
stat[j] = 3;
kill(atoi(p), SIGSTOP);
cout << p << " STOPPED" << endl; // stop process and display message
found = true;
count--;
for (int j = 0; j <= i; j++)
{
if (proc[j] != atoi(p) && count < 3 && stat[j] == 3) //search for stopped process and automatically restart
{
stat[j] = 1;
count++;
cout << proc[j] << " automatically restarted" << endl;
kill(proc[j], SIGCONT);
}
}
}
}
}
if(found==false) {
cout << atoi(p) << " does not exist" << endl; //if process not found
}
}
else if (strcmp(p, "bgkill") == 0)
{
p = strtok(NULL, " \t");
bool found = false; //to check if such process exists
for (int j = 0; j <= i; j++)
{
if (proc[j] == atoi(p))
{
if (stat[j] == 0) {
cout << proc[j] << " already terminated" << endl; //if process already killed
found = true;
break;
}
else
{
stat[j] = 0;
kill(atoi(p), SIGTERM); //kill process
cout << p << " killed" << endl;
found = true;
count--;
for (int j = 0; j <= i; j++)
{
if (proc[j] != atoi(p) && count < 3 && stat[j] == 3)
{
stat[j] = 1;
count++;
cout << proc[j] << " automatically restarted" << endl; //automatically start a stopped process
kill(proc[j], SIGCONT);
}
}
}
}
}
if (found==false) {
cout << atoi(p) << " does not exist" << endl; //if such process does not exist
}
}
}
}