-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoss.c
199 lines (174 loc) · 5.25 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
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<time.h>
struct PCB {
int occupied;
pid_t pid;
int startTimeSec;
int startTimeNano;
};
void help() {
printf("This program is designed to have a parent process fork off into child processes.\n");
printf("The child processes use a simulated clock in shared memory to keep track of runtime.\n");
printf("The runtime is a random number of seconds and nanoseconds between 1 and the time limit prescribed by the user.\n\n");
printf("The executable takes three flags: [-n proc], [-s simul], and [-t timelimit].\n");
printf("The value of proc determines the total number of child processes to be produced.\n");
printf("The value of simul determines the number of children that can run simultaneously.\n");
printf("The value of timelimit determines the maximum number of seconds that a child process can take.\n");
printf("\nMADE BY JACOB (JT) FOX\nSeptember 28th, 2023\n");
exit(1);
}
void incrementClock(int *shm_ptr) {
shm_ptr[1] += 10000;
if(shm_ptr[1] >= 1000000000) {
shm_ptr[1] = 0;
shm_ptr[0] += 1;
}
}
/*
void startPCB(int tableEntry, struct PCB *processTable[], int pidNumber, int *time) {
(*processTable[tableEntry]).occupied = 1;
(*processTable[tableEntry]).pid = pidNumber;
(*processTable[tableEntry]).startTimeSec = time[0];
(*processTable[tableEntry]).startTimeNano = time[1];
}
void endPCB(int pidNumber, int tableSize, struct PCB *processTable[]) {
int i;
for(i = 0; i < tableSize; i++) {
if(processTable[i]->pid == pidNumber) {
processTable[i]->occupied = 0;
return;
}
}
}*/
void outputTable(int rows, struct PCB processTable[]) {
printf("Process Table:\nEntry Occupied PID\tStartS StartN\n");
int i;
for(i = 0; i < rows; i++) {
printf("%d\t%d\t%d\t%d\t%d\t\n\n", i, processTable[i].occupied, processTable[i].pid, processTable[i].startTimeSec, processTable[i].startTimeNano);
}
}
int randNumGenerator(int max) {
srand(time(NULL));
return ((rand() % max) + 1);
}
int main(int argc, char** argv) {
int option;
int proc;
int simul;
int timelimit;
//allocate shared memory
const int sh_key = ftok("./oss.c", 0);
const int shm_id = shmget(sh_key, sizeof(int) * 2, IPC_CREAT | 0666);
if(shm_id <= 0) {
printf("Shared memory allocation failed\n");
exit(1);
}
//attach to shared memory
int *shm_ptr;
shm_ptr = shmat(shm_id, 0 ,0);
if(shm_ptr <= 0) {
printf("Attaching to shared memory failed\n");
exit(1);
}
while ((option = getopt(argc, argv, "hn:s:t:")) != -1) {
switch(option) {
case 'h':
help();
break;
case 'n':
proc = atoi(optarg);
break;
case 's':
simul = atoi(optarg);
break;
case 't':
timelimit = atoi(optarg);
break;
}
}
struct PCB processTable[proc];
int totalChildren;
int runningChildren;
totalChildren = 0;
runningChildren = 0;
//set clock to zero
shm_ptr[0] = 0;
shm_ptr[1] = 0;
//vars for fetching worker termTime values
const int maxNano = 1000000000;
int randNumS, randNumNano;
//char str for sending randNum values to the worker
char secStr[sizeof(int)];
char nanoStr[sizeof(int)];
//initialize child processes
while(runningChildren < simul) {
pid_t childPid = fork();
if(childPid == 0) {
randNumS = randNumGenerator(timelimit);
randNumNano = randNumGenerator(maxNano);
snprintf(secStr, sizeof(int), "%d", randNumS);
snprintf(nanoStr, sizeof(int), "%d", randNumNano);
execlp("./worker", secStr, nanoStr, NULL);
exit(1);
}
else {
processTable[runningChildren].occupied = 1;
processTable[runningChildren].pid = childPid;
processTable[runningChildren].startTimeSec = shm_ptr[0];
processTable[runningChildren].startTimeNano = shm_ptr[1];
runningChildren++;
totalChildren++;
}
}
int outputTimer;
outputTimer = 0;
int halfSecond = 500000000;
do {
incrementClock(shm_ptr);
/*if(sixtySecondsHasPassed)
terminateProgram();*/
if(abs(shm_ptr[1] - outputTimer) >= halfSecond){
outputTimer = shm_ptr[1];
printf("\nOSS PID:%d SysClockS:%d SysClockNano:%d\n", getpid(), shm_ptr[0], shm_ptr[1]);
outputTable(proc, processTable);
}
int status;
int pid = waitpid(-1, &status, WNOHANG); //Will return 0 if no processes have terminated
if(pid) {
processTable[totalChildren].occupied = 0;
runningChildren--;
if(totalChildren < proc) {
pid_t childPid = fork(); //Launches child
if(childPid == 0) {
randNumS = randNumGenerator(timelimit);
randNumNano = randNumGenerator(maxNano);
snprintf(secStr, sizeof(int), "%d", randNumS);
snprintf(nanoStr, sizeof(int), "%d", randNumNano);
execlp("./worker", secStr, nanoStr, NULL);
exit(1);
}
else {
processTable[totalChildren].occupied = 1;
processTable[totalChildren].pid = childPid;
processTable[totalChildren].startTimeSec = shm_ptr[0];
processTable[totalChildren].startTimeNano = shm_ptr[1];
runningChildren++;
totalChildren++;
}
}
}
} while(runningChildren);
pid_t wpid;
int status = 0;
while((wpid = wait(&status)) > 0);
//detach from and delete memory
shmdt(shm_ptr);
shmctl(shm_id, IPC_RMID, NULL);
return EXIT_SUCCESS;
}