-
Notifications
You must be signed in to change notification settings - Fork 0
/
A3main.c
306 lines (270 loc) · 13.2 KB
/
A3main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <sys/resource.h>
#include <utmp.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <sys/utsname.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include "a3.h"
#define MAX_LEN 1024
void CtrlC(int sig)
{
char c;
signal(sig, SIG_IGN);
printf("Enter y/Y to quit: \n");
c = getchar();
if (c == 'y' || c == 'Y')
exit(0);
else{
signal(SIGINT, CtrlC);
}
getchar(); // Get new line character
}
//Function to print all stats
void finPrint(bool sys, bool user, bool graph, bool sequen, int samples, int tdelay){
// Create Pipes
int pipe_mem[2], pipe_cpu[2], pipe_user[2];
//declare arrays to store memory and cpu graphical usage
char memory_arr[1024][1024];
char graph_arr[1024][1024];
double prev_virt;
long int prev_cpu = 0;
long int prev_idle = 0;
for (int i = 0; i < samples; i++)
{
if (pipe(pipe_mem) == -1 || pipe(pipe_cpu) == -1 || pipe(pipe_user) == -1) {
fprintf(stderr, "Unable to create pipe (%s)\n", strerror(errno));
exit(1);
}
pid_t pid_memory = fork();
if (pid_memory == -1){
fprintf(stderr, "Unable to fork (%s)\n", strerror(errno));
exit(1);
}
else if (pid_memory == 0) {
// Child process
close(pipe_mem[0]); // Close unused read
close(pipe_cpu[0]);
close(pipe_cpu[1]);
close(pipe_user[0]);
close(pipe_user[1]);
// Call Memory Function
getMemory(pipe_mem);
exit(0);
}
else {
pid_t pid_users = fork();
if(pid_users == -1){
fprintf(stderr, "Unable to fork (%s)\n", strerror(errno));
exit(1);
}
if (pid_users == 0) {
// Child process
close(pipe_user[0]); // Close unused read
close(pipe_mem[0]); close(pipe_mem[1]); close(pipe_cpu[0]); close(pipe_cpu[1]);
// Call Users Function
userPrint(pipe_user);
exit(0);
}
else {
pid_t pid_cpu = fork();
if (pid_cpu == -1)
{
fprintf(stderr, "Unable to fork (%s)\n", strerror(errno));
exit(1);
}
else if (pid_cpu == 0) {
close(pipe_cpu[0]);
close(pipe_mem[0]);
close(pipe_mem[1]);
close(pipe_user[0]);
close(pipe_user[1]);
getCpu(pipe_cpu);
close(pipe_cpu[1]);
exit(0);
}
else {
// Main partent Process
// Close the write end of the pipe for the parent
close(pipe_cpu[1]); close(pipe_mem[1]); close(pipe_user[1]);
// Wait for information retrival
waitpid(pid_memory, NULL, 0);
waitpid(pid_users, NULL, 0);
waitpid(pid_cpu, NULL, 0);
//if sequen is true, print iteration number
if(sequen){
printf(">>> iteration %d\n", i);
starter(samples, tdelay);
if(sys && !user){
struct mem_stat* mem = (struct mem_stat*) malloc(sizeof(struct mem_stat));
struct cpu_stat* cpu = (struct cpu_stat*) malloc(sizeof(struct cpu_stat));
if (mem == NULL || cpu == NULL) {
// Handle memory allocation failure
printf("Memory allocation failed.\n");
exit(1);
}
read(pipe_mem[0], mem, sizeof(struct mem_stat));
read(pipe_cpu[0], cpu, sizeof(struct cpu_stat));
memoryPrint(memory_arr, samples, graph, i, &prev_virt, mem);
cpuPrint(graph_arr, samples, graph, i, &prev_cpu, &prev_idle, cpu);
close(pipe_cpu[0]); close(pipe_mem[0]); close(pipe_user[0]);
}
else if(user && sys){
struct mem_stat* mem = (struct mem_stat*) malloc(sizeof(struct mem_stat));
struct cpu_stat* cpu = (struct cpu_stat*) malloc(sizeof(struct cpu_stat));
if (mem == NULL || cpu == NULL) {
// Handle memory allocation failure
printf("Memory allocation failed.\n");
exit(1);
}
read(pipe_mem[0], mem, sizeof(struct mem_stat));
read(pipe_cpu[0], cpu, sizeof(struct cpu_stat));
memoryPrint(memory_arr, samples, graph, i, &prev_virt, mem);
char temp[1024];
int bytesRead;
// Print Divider
printf("--------------------------------------------\n");
printf("### Sessions/users ###\n");
while ((bytesRead = read(pipe_user[0], temp, sizeof(temp) - 1)) > 0) {
temp[bytesRead] = '\0';
printf("%s", temp);
}
cpuPrint(graph_arr, samples, graph, i, &prev_cpu, &prev_idle, cpu);
close(pipe_cpu[0]); close(pipe_mem[0]); close(pipe_user[0]);
}
else if(user && !sys){
char temp[1024];
int bytesRead;
// Print Divider
printf("--------------------------------------------\n");
printf("### Sessions/users ###\n");
while ((bytesRead = read(pipe_user[0], temp, sizeof(temp) - 1)) > 0) {
temp[bytesRead] = '\0';
printf("%s", temp);
}
close(pipe_cpu[0]); close(pipe_mem[0]); close(pipe_user[0]);
}
}
else{
printf("\033[H \033[2J \n");
starter(samples, tdelay);
if(sys && !user){
struct mem_stat* mem = (struct mem_stat*) malloc(sizeof(struct mem_stat));
struct cpu_stat* cpu = (struct cpu_stat*) malloc(sizeof(struct cpu_stat));
if (mem == NULL || cpu == NULL) {
// Handle memory allocation failure
printf("Memory allocation failed.\n");
exit(1);
}
read(pipe_mem[0], mem, sizeof(struct mem_stat));
read(pipe_cpu[0], cpu, sizeof(struct cpu_stat));
memoryPrint(memory_arr, samples, graph, i, &prev_virt, mem);
cpuPrint(graph_arr, samples, graph, i, &prev_cpu, &prev_idle, cpu);
close(pipe_cpu[0]); close(pipe_mem[0]); close(pipe_user[0]);
}
else if(user && sys){
struct mem_stat* mem = (struct mem_stat*) malloc(sizeof(struct mem_stat));
struct cpu_stat* cpu = (struct cpu_stat*) malloc(sizeof(struct cpu_stat));
if (mem == NULL || cpu == NULL) {
// Handle memory allocation failure
printf("Memory allocation failed.\n");
exit(1);
}
read(pipe_mem[0], mem, sizeof(struct mem_stat));
read(pipe_cpu[0], cpu, sizeof(struct cpu_stat));
memoryPrint(memory_arr, samples, graph, i, &prev_virt, mem);
//printf(">>> iteration %d\n", i);
close(pipe_mem[0]);
char temp[1024];
int bytesRead;
// Print Divider
printf("--------------------------------------------\n");
printf("### Sessions/users ###\n");
while ((bytesRead = read(pipe_user[0], temp, sizeof(temp) - 1)) > 0) {
temp[bytesRead] = '\0';
printf("%s", temp);
}
cpuPrint(graph_arr, samples, graph, i, &prev_cpu, &prev_idle, cpu);
close(pipe_cpu[0]); close(pipe_user[0]);
free(mem);
free(cpu);
}
else if(user && !sys){
char temp[1024];
int bytesRead;
// Print Divider
printf("--------------------------------------------\n");
printf("### Sessions/users ###\n");
while ((bytesRead = read(pipe_user[0], temp, sizeof(temp) - 1)) > 0) {
temp[bytesRead] = '\0';
printf("%s", temp);
}
close(pipe_cpu[0]); close(pipe_mem[0]); close(pipe_user[0]);
}
}
sleep(tdelay);
ender();
}
}
}
}
}
//Main function where arguments are parsed
int main(int argc, char *argv[]){
signal(SIGINT, CtrlC);
int samples = 10, tdelay = 1;
//default values
bool sys = true, user = true, graph = false, sequen = false;
bool sys_seen = false, user_seen = false;
bool int_args = false;
//check for correct number of arguments
bool any = false;
for (int i = 0; i < argc; i++)
{
if((strcmp(argv[i], "--system") == 0) || (strcmp(argv[i], "-s") == 0)){
any = true;
//if user has not been seen, set user to false
if (user_seen == false) user = false, sys_seen = true;
//if user has been seen, set user to true
else{ user = true, sys = true, sys_seen = true;}
}
if((strcmp(argv[i], "--user") == 0)|| (strcmp(argv[i], "-u") == 0)){
any = true;
//if sys has not been seen, set sys to false
if (sys_seen == false) sys = false, user_seen = true;
//if sys has been seen, set sys to true
else{ sys = true, user = true, user_seen = true;}
}
if((strcmp(argv[i], "--graphics") == 0) || (strcmp(argv[i], "-g") == 0)){
any = true;
graph = true;
}
if((strcmp(argv[i], "--sequential") == 0)|| (strcmp(argv[i], "-se") == 0)){
any = true;
sequen = true;
}
//if argument is a number, set samples to that number
if ((strncmp(argv[i], "--samples=", 10) == 0)){ samples = atoi(argv[i] + 10); any = true;}
if((strncmp(argv[i], "-sa=", 4) == 0)){ samples = atoi(argv[i] + 4); any = true;}
//check for positional arguments
if ((isdigit(*argv[i])) && (int_args == false)) {samples = atoi(argv[i]), int_args = true; any = true;}
else if((isdigit(*argv[i])) && (int_args == true)) {tdelay = atoi(argv[i]); any = true;}
//if argument is a number, set tdelay to that number
if((strncmp(argv[i], "--tdelay=", 9) == 0)){ tdelay = atoi(argv[i] + 9); any = true;}
if((strncmp(argv[i], "-td=", 4) == 0)){ tdelay = atoi(argv[i] + 4); any = true;}
}
//if no arguments are entered, print all stats
if((any == true) || (argc ==1) ){ finPrint(sys, user, graph, sequen, samples, tdelay);}
//if invalid arguments are entered, print error message
else{ printf("Please enter valid arguments\n");}
return 0;
}