-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimekeeper.c
369 lines (294 loc) · 9.41 KB
/
timekeeper.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
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
FileName: main.c
Student Name: Tarun Sudhams
Student Number: 3035253876
Development Platform: MACOSX 10.14 with gcc compiler and Sublime Text(tested under Ubuntu 18.04)
Compilation: gcc timekeeper_3035253876.c -o timekeeper
Remarks:
*/
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
#include <ctype.h>
int parentProcessPID = 0;
int childProcessPID = 0;
int timeStatisticsTrigger = 1;
int cont = 1;
int firstProcess = 0;
int pipeCounter = 0;
int maintainPipes = 0;
int n = 0;
// Basic Signal handling function to catch signals
void signalhandler(int signum)
{
printf("Signal %d is caught for %d times\n", signum, ++n);
}
//Harcoding the signals listing in order to prompt when required
char *signame[]={"INVALID", "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP", "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGPOLL", "SIGPWR", "SIGSYS", NULL};
//Basic function for signal handling
static void handleyoursignal(void)
{
struct sigaction satmp;
sigemptyset(&satmp.sa_mask);
satmp.sa_flags = 0;
satmp.sa_handler = signalhandler;
sigaction(SIGINT, &satmp, NULL);
}
//Function to trigger time statistics and context switching
void dotimeStatisticsTrigger(int processID)
{
char inputStringOne[50];
char state;
char inputStringTwo[50];
FILE * file;
int z;
unsigned long long i, x;
unsigned long h, ut, st;
unsigned long long int starttime;
sprintf(inputStringOne, "/proc/%d/stat", processID);
file = fopen(inputStringOne, "r");
if (file == NULL)
{
printf("Error in open my proc file for %s\n", inputStringOne);
exit(0);
}
float uptime;
FILE* proc_uptime_file = fopen("/proc/uptime", "r");
fscanf(proc_uptime_file, "%f", &uptime);
FILE *filefile;
char lineToBeProcessed[100];
sprintf(inputStringTwo, "/proc/%d/status", processID);
filefile = fopen(inputStringTwo, "r");
int One;
int Two;
int final_value;
while (fgets(lineToBeProcessed, sizeof(lineToBeProcessed), filefile) != NULL)
{
char *endValue;
char *registerIterator;
char *registerIteratorValue;
endValue = strchr(lineToBeProcessed, '\n');
if (endValue != NULL)
{
*endValue = '\0';
}
endValue = strchr(lineToBeProcessed, ':');
if (endValue != NULL)
{
endValue[0] = '\0';
registerIterator = strdup(lineToBeProcessed);
if (registerIterator == NULL)
{
continue;
}
endValue += 1;
while ((endValue[0] != '\0') && (isspace((int) endValue[0]) != 0))
{
endValue++;
}
registerIteratorValue = strdup(endValue);
if (registerIteratorValue != NULL)
{
if(strcmp(registerIterator,"voluntary_ctxt_switches") == 0)
{
One = atoi(registerIteratorValue);
}
if(strcmp(registerIterator,"nonvoluntary_ctxt_switches") == 0)
{
Two = atoi(registerIteratorValue);
}
free(registerIteratorValue);
}
free(registerIterator);
}
}
final_value = One + Two;
fscanf(file, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu", &z, inputStringOne, &state, &z, &z, &z, &z, &z, (unsigned *)&z, &h, &h, &h, &h, &ut, &st, &h, &h, &h, &h, &h, &h, &starttime); //read all params and save the ones needed
fclose(file);
printf("real: %0.2fs, user:%0.2fs, system: %0.2fs, context_switch: %d\n",uptime - (starttime)/sysconf(_SC_CLK_TCK), ut*1.0f/sysconf(_SC_CLK_TCK), st*1.0f/sysconf(_SC_CLK_TCK), final_value);
}
void execution(int argc, char *argv[])
{
/* ------------ Declaring variables for statisics ------------ */
handleyoursignal();
/* ------------ End of Variables for statistics------------ */
/* ------------ Declaring variables for execution and piping ------------ */
int * mainInputCommands = (int *) malloc (sizeof(int) * argc);
int counterOfPipe = 1;
int pfd1[2];
int pfd2[2];
int currentRunningPID = 0;
pid_t pid;
int counterOfArg = 0;
int CurrentStatusOfPID;
int randomIntegerOne = 0;
/* ------------ End of Declaring Variables for execution and piping------------ */
/* ------------ Execution and Piping Starts Here------------ */
for(int randomIntegerTwo = 1; randomIntegerTwo < argc; randomIntegerTwo++)
{
if(strcmp(argv[randomIntegerTwo],"!") == 0)
{
mainInputCommands[randomIntegerOne] = randomIntegerTwo - counterOfPipe;
randomIntegerOne = randomIntegerOne + 1;
counterOfPipe = randomIntegerTwo + 1;
}
}
mainInputCommands[randomIntegerOne] = argc - counterOfPipe;
randomIntegerOne= randomIntegerOne + 1;
/* ------------Different Cases for Piping Start Here----------- */
// Adapted from the Lab 3 Piping and made
if(randomIntegerOne == 2)
{
pipe(pfd1);
}
else if(randomIntegerOne > 2)
{
pipe(pfd1);
pipe(pfd2);
}
counterOfPipe = 1;
pid_t childPIDs[randomIntegerOne];
/* ------------Process Creation Happens Here------------ */
for(int randomIntegerTwo = 0; randomIntegerTwo < randomIntegerOne; randomIntegerTwo++)
{
handleyoursignal();
pid = fork();
childPIDs[randomIntegerTwo] = pid;
if(pid < 0)
{
printf("ERROR: Forking Failed\n");
}
else if (pid == 0)
{
printf("Process with id: %d created for the command: %s\n", (int) getpid(), argv[counterOfPipe]);
break;
}
counterOfPipe += mainInputCommands[randomIntegerTwo] + 1;
currentRunningPID++;
sleep(1);
}
if(pid > 1)
{
if(randomIntegerOne == 2)
{
close(pfd1[0]); // closing both ends of the pipe opened earlier
close(pfd1[1]);
}
if(randomIntegerOne > 2)
{
close(pfd1[0]);
close(pfd1[1]);
close(pfd2[0]);
close(pfd2[1]);
}
counterOfPipe = 1;
for(int randomIntegerTwo = 0; randomIntegerTwo < randomIntegerOne; randomIntegerTwo++)
{
/* ------------Handling the process output based on the interruption or termination of the process------------ */
siginfo_t infop;
infop.si_pid = -1;
waitid(P_PID,(int) childPIDs[randomIntegerTwo], &infop, WEXITED | WNOWAIT | WSTOPPED);
waitid(P_PID,(int) childPIDs[randomIntegerTwo], &infop, WEXITED | WNOWAIT | WSTOPPED);
if(WEXITSTATUS(infop.si_status) == -1)
{
printf("Error executing the command\n");
}
else if(WIFSIGNALED(infop.si_status))
{
// adapted from https://stackoverflow.com/questions/16509614/signal-number-to-name
printf("The command \"%s\" interrupted by signal number = %d (%s)\n", argv[counterOfPipe], WTERMSIG(infop.si_status), signame[WTERMSIG(infop.si_status)]);
dotimeStatisticsTrigger(childPIDs[randomIntegerTwo]);
}
else
{
printf("The command \"%s\" terminated with returned status code = %d\n", argv[counterOfPipe] ,WEXITSTATUS(infop.si_status));
dotimeStatisticsTrigger(childPIDs[randomIntegerTwo]);
}
counterOfPipe = counterOfPipe + mainInputCommands[randomIntegerTwo] + 1;
sleep(1);
}
}
sleep(1);
if (pid==0)
{
char ** argumentVector = (char **) malloc(sizeof(char*) * mainInputCommands[currentRunningPID]);
for (int randomIntegerTwo = counterOfPipe; randomIntegerTwo < counterOfPipe + mainInputCommands[currentRunningPID]; randomIntegerTwo++)
{
argumentVector[counterOfArg] = (char *) malloc(sizeof(char) * strlen(argv[randomIntegerTwo]));
strcpy(argumentVector[counterOfArg], argv[randomIntegerTwo]);
counterOfArg++;
}
if (randomIntegerOne == 1)
{
if (execvp(argumentVector[0],argumentVector) == -1)
{
printf("execvp: No such file or directory\n");
exit(-1);
}
}
if (randomIntegerOne == 2)
{
if (currentRunningPID == 0)
{
close(pfd1[0]);
dup2(pfd1[1], STDOUT_FILENO);
close(pfd1[1]);
}
else if (currentRunningPID == 1)
{
close(pfd1[1]);
dup2(pfd1[0], STDIN_FILENO);
close(pfd1[0]);
}
if (execvp(argumentVector[0],argumentVector) == -1)
{
printf("execvp: No such file or directory\n");
exit(-1);
}
}
if (randomIntegerOne > 2)
{
if (currentRunningPID == 0)
{
close(pfd2[0]);
close(pfd2[1]);
close(pfd1[0]);
dup2(pfd1[1], STDOUT_FILENO);
close(pfd1[1]);
}
else if (currentRunningPID == randomIntegerOne - 1)
{
close(pfd1[0]);
close(pfd1[1]);
close(pfd2[1]);
dup2(pfd2[0], STDIN_FILENO);
close(pfd2[0]);
}
else
{
close(pfd1[1]);
close(pfd2[0]);
dup2(pfd1[0], STDIN_FILENO);
dup2(pfd2[1], STDOUT_FILENO);
close(pfd1[0]);
close(pfd2[1]);
}
if (execvp(argumentVector[0],argumentVector) == -1)
{
printf("execvp: No such file or directory\n");
exit(-1);
}
}
exit(0);
}
}
int main(int argc, char* argv[])
{
execution(argc, argv);
return 0;
}