-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss.c
356 lines (319 loc) · 8.71 KB
/
oss.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Hanzhe Huang
12/01/2021
oss.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <time.h> //time
#include <sys/wait.h> //wait
#include <signal.h>
#include <ctype.h> //isprint
#include <unistd.h> //sleep, alarm
#include "queue.c"
#include "structs.h"
#include "descriptor.c"
#include "clock.c"
#include "oss.h"
// Reference: https://www.tutorialspoint.com/inter_process_communication/inter_process_communication_shared_memory.htm
// Reference: https://stackoverflow.com/questions/19461744/how-to-make-parent-wait-for-all-child-processes-to-finish
// Reference: https://www.tutorialspoint.com/inter_process_communication/inter_process_communication_message_waitQueue.htm
// Reference: https://www.geeksforgeeks.org/ipc-using-message-waitQueue/
extern int errno;
static char* logName;
int main(int argc, char** argv) {
// Signal handlers;
signal(SIGINT, sigint_parent);
signal(SIGALRM, sigalrm);
// Interval constants
const int maxTimeBetweenNewProcsSecs = 1;
const int maxTimeBetweenNewProcsNS = 50000000;
// Statistics
int processNum = 0, idle = 1, allocations = 0;
FILE* fptr;
int i;
int pid = 1, pidIndex = 0;
struct clock tempClock = {0, 0};
struct clock lastNewProcessTime = {0, 0};
time_t t;
srand((unsigned) time(&t));
// Initialize wait queue
int waitQueue[MAX_PRO];
initQueue(waitQueue, MAX_PRO);
logName = malloc(200);
logName = LOGFILE;
// Clear log file
fptr = fopen(logName, "w");
fclose(fptr);
fptr = fopen(logName, "a");
// Shm Init
struct shmseg *shmp;
int shmid = shmget(SHM_KEY, BUF_SIZE, 0666|IPC_CREAT);
if (shmid == -1) {
perror("Error: shmget");
exit(-1);
}
shmp = shmat(shmid, 0, 0);
initshmobj(shmp);
initDescriptor(shmp,RES_SIZE,MAX_PRO);
// Message queue init
int msgid = msgget(MSG_KEY, 0666 | IPC_CREAT);
if (msgid == -1) {
perror("Error: msgget");
exit(-1);
}
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("Error: msgctl");
exit(-1);
}
msgid = msgget(MSG_KEY, 0666 | IPC_CREAT);
/*
freeResource(shmp, RES_SIZE, MAX_PRO, 0);
printDescriptor(fptr,shmp,RES_SIZE,MAX_PRO);
*/
// Main loop
while(1){
idle = 1;
// If process limit isn't reached and clock has advanced, fork a process
for (i = 0; i < MAX_PRO; i++){
if (shmp->processTable[i].processPid == 0) {
break;
}
}
if (i != MAX_PRO && (isClockLarger(shmp->ossclock, lastNewProcessTime) == 0) && processNum < TOTAL_PRO){
if (msgrcv(msgid, &msg_t, sizeof(msg_t), 3, IPC_NOWAIT) != -1 && processNum > 17){
waitpid(-1, NULL, 0);
}
else if (processNum > 17){
break;
}
idle = 0;
pid = fork();
switch ( pid )
{
case -1:
perror("Error: fork");
return -1;
case 0: // Child, terminates
child();
break;
default: // Parent
// Increment total processes created
shmp->numberProcesses++;
processNum++;
printf("%d\n",shmp->numberProcesses);
if (shmp->numberProcesses > 18){
exit(-1);
}
if (maxTimeBetweenNewProcsSecs != 0){
lastNewProcessTime.clockSecs = shmp->ossclock.clockSecs + (rand() % maxTimeBetweenNewProcsSecs);
}
else {
lastNewProcessTime.clockSecs = shmp->ossclock.clockSecs + 0;
}
lastNewProcessTime.clockNS = shmp->ossclock.clockNS + (rand() % maxTimeBetweenNewProcsNS);
if (lastNewProcessTime.clockNS >= 1000000000){
lastNewProcessTime.clockSecs++;
lastNewProcessTime.clockNS -= 1000000000;
}
// Store pid to process table, dispatch it
printf("Child %d forked at %d:%d.\n", pid, shmp->ossclock.clockSecs, shmp->ossclock.clockNS);
shmp->processTable[i].processPid = pid;
//msg_t.mtype = pid;
//msgsnd(msgid, &msg_t, sizeof(msg_t), 0);
//msgrcv(msgid, &msg_t, sizeof(msg_t), 1, 0);
idle = 0;
break;
}
}
// Check if wait queue has items
if ((pid = dequeue(waitQueue, MAX_PRO)) != -1){
pidIndex = getPidIndex(shmp, pid);
if (allocateForProcess(shmp, RES_SIZE, MAX_PRO, pidIndex) != 0){
printf("Putting P%d to wait queue at time %d:%d.\n", pidIndex, shmp->ossclock.clockSecs, shmp->ossclock.clockNS);
if (enqueue(waitQueue, MAX_PRO, pid) != 0){
perror("Error: enqueue");
exit(-1);
}
}
else {
msg_t.mtype = pid;
msgsnd(msgid, &msg_t, sizeof(msg_t), 0);
allocations++;
if (allocations == 20){
allocations = 0;
printDescriptor(fptr,shmp, RES_SIZE, MAX_PRO);
}
}
}
// Check for resource allocation requests
if (msgrcv(msgid, &msg_t, sizeof(msg_t), 1, IPC_NOWAIT) != -1){
pidIndex = getPidIndex(shmp, msg_t.pid);
if (allocateForProcess(shmp, RES_SIZE, MAX_PRO, pidIndex) != 0){
printf("Putting P%d to wait queue at time %d:%d.\n", pidIndex, shmp->ossclock.clockSecs, shmp->ossclock.clockNS);
if (enqueue(waitQueue, MAX_PRO, msg_t.pid) != 0){
perror("Error: enqueue");
exit(-1);
}
}
else {
msg_t.mtype = msg_t.pid;
msgsnd(msgid, &msg_t, sizeof(msg_t), 0);
allocations++;
if (allocations == 20){
allocations = 0;
printDescriptor(fptr,shmp, RES_SIZE, MAX_PRO);
}
}
}
// Check for resource freeing requests
if (msgrcv(msgid, &msg_t, sizeof(msg_t), 2, IPC_NOWAIT) != -1){
pidIndex = getPidIndex(shmp, msg_t.pid);
freeResource(shmp, RES_SIZE, MAX_PRO, pidIndex);
printf("Freed resources from P%d.\n", pidIndex);
msg_t.mtype = msg_t.pid;
msgsnd(msgid, &msg_t, sizeof(msg_t), 0);
//printDescriptor(fptr,shmp,RES_SIZE,MAX_PRO);
}
// Check if no process was made, increment system clock by a small amount
if (idle == 1){
tempClock.clockSecs = 0;
tempClock.clockNS = 4000000;
incrementClockShm(shmp, tempClock.clockSecs, tempClock.clockNS);
//printf("...\n");
}
// Check if oss is due for termination
if (processNum >= TOTAL_PRO){
for (i = 0; i < MAX_PRO; i++){
if (shmp->processTable[i].processPid > 0){
break;
}
}
if (i == MAX_PRO){
fclose(fptr);
logexit();
return 0;
}
}
}
return -1;
}
// Logs termination time
void logexit(){
FILE* fptr;
char timeBuffer[40];
time_t tempTime = time(NULL);
fptr = fopen(logName, "a");
strftime(timeBuffer, 40, "%H:%M:%S", localtime(&tempTime));
printf("Saving: %s %d terminated\n", timeBuffer, getpid());
fprintf(fptr, "OSS: %s %d terminated\n", timeBuffer, getpid());
fclose(fptr);
}
// Signals
void sigint_parent(int sig){
printf("Process %d exiting...\n",getpid());
deallocate();
printf("Terminating child processes...\n");
kill(0, SIGINT);
parent();
logexit();
exit(0);
}
void sigint(int sig){
kill(0, SIGINT);
exit(0);
}
void sigalrm(int sig){
printf("Program timed out.\n");
kill(0, SIGINT);
exit(0);
}
// Parent function to wait for children processes
void parent(){
int childpid;
while ((childpid = (wait(NULL))) > 0);
printf("Stopped waiting for children.\n");
}
// Reference: http://www.cs.umsl.edu/~sanjiv/classes/cs4760/src/shm.c
// Reference: https://www.geeksforgeeks.org/signals-c-set-2/
void child(){
signal(SIGINT, sigint);
signal(SIGALRM, SIG_IGN);
// Exec user process
if ((execl("userprocess", "userprocess", (char*)NULL)) == -1){
perror("Error: execl/stdin");
exit(-1);
}
exit(1);
}
// Deallocates shared memory & message queue
void deallocate(){
//shm
int shmid = shmget(SHM_KEY, BUF_SIZE, 0666|IPC_CREAT);
if (shmid == -1) {
perror("Error: shmget");
exit(-1);
}
if (shmctl(shmid, IPC_RMID, 0) == -1) {
perror("Error: shmctl");
exit(-1);
}
//msg
int msgid = msgget(MSG_KEY, 0666 | IPC_CREAT);
if (msgid == -1) {
perror("Error: msgget");
exit(-1);
}
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("Error: msgctl");
exit(-1);
}
printf("Shared memory & message queue deallocated.\n");
}
// Returns the shared memory segment
struct shmseg* shmobj(){
struct shmseg *shmp;
int shmid = shmget(SHM_KEY, BUF_SIZE, 0666|IPC_CREAT);
if (shmid == -1) {
perror("Error: shmget");
exit(-1);
}
shmp = shmat(shmid, 0, 0);
return shmp;
}
// Initializes shared memory segment
void initshmobj(struct shmseg* shmp){
int i;
shmp->numberProcesses = 0;
shmp->ossclock.clockSecs = 0;
shmp->ossclock.clockNS = 0;
for (i = 0; i < MAX_PRO; i++){
shmp->processTable[i].processPid = 0;
}
}
// Increments the clock by seconds and nanoseconds
void incrementClockShm(struct shmseg* shmp, int incS, int incNS){
shmp->ossclock.clockSecs += incS;
shmp->ossclock.clockNS += incNS;
if (shmp->ossclock.clockNS >= 1000000000){
shmp->ossclock.clockSecs++;
shmp->ossclock.clockNS -= 1000000000;
}
}
// Returns the index of a process in the process table
int getPidIndex(struct shmseg* shmp, int pid){
int i;
for (i = 0; i < MAX_PRO; i++){
if (shmp->processTable[i].processPid == pid){
return i;
}
}
printf("Error: getPidIndex: %d\n", pid);
exit(-1);
}