-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeCommunication.c
More file actions
543 lines (430 loc) · 10.3 KB
/
Copy pathpipeCommunication.c
File metadata and controls
543 lines (430 loc) · 10.3 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include<ctype.h>
#define START "Welcome : Enter the commands to your liking\n"
#define DIVIDE "Error : division by zero\n"
#define MESSAGE "Invalid command, type help\n"
#define NOELEMENTS "No elements in the list\n"
#define HELP "=========================\nWelcome to our program guide:\nType add number1 number2 ...\nType sub number1 number2 ...\nType mult number1 number2 ...\nType div number1 number2 ...\nType run argument1 ...\nType list : To view currenly running processes and list[ all] for all processes executed\nType kill PID, to kill an actively running prcocess\nType exit : to exit our program\nNote: All the operations are not case-sensitve, example: Add, aDD, ADd are valid\nMinimun number of inputs for arthimetic operation is Two\n=========================\n"
int checkDigit(char * token);
struct Node{
int id;
int active;
char name[15];
};
//Node array
struct Node * processArray[30];//if typed exit then it will be freed
int processCounter = 0;
int pd[2];
int pd1[2];
void printArray()
{
int i = 0;
char buff[2000];
int count = 0;
for(;i<processCounter;i++)
{
count += sprintf(&buff[count],"Process ID = %d , Active = %d, Process name = %s\n",processArray[i]-> id,processArray[i]->active,processArray[i]->name);
}
write(pd1[1],buff,count);
}
void printActiveArray()
{
int i = 0;
int count=0;
int activeProcesses = 0;
char buff[1000];
for(;i<processCounter;i++)
{
if(processArray[i]->active == 1)
{
count += sprintf(&buff[count],"Process ID = %d, Process name = %s\n",processArray[i]->id,processArray[i]->name);
activeProcesses++;
}
buff[count] = '\0';
count++;
}
if( activeProcesses > 0)
write(pd1[1],buff,count);
else write(pd1[1],"Currently no active processes listed\n",strlen("Currently no active processes listed\n"));
}
void inactiveProcess(int search)
{
int i;
for(i = 0; i<processCounter;i++)
{
if(processArray[i]->id == search)
{ processArray[i]->active = 0;
break;
}
}
}
static void signalhandler(int signo)
{
int status;
int pid;
pid = waitpid(-1,&status,0);
inactiveProcess(pid);
}
char* methodArithmetic(char * token, char * buff)
{
char * identifier = token;
int sum = 0;
int firstNumber = 0;
int divideZero = 0;
int check = 0;
int arguments = 0;
if((token = strtok(NULL," \n")) == NULL)
{
strcpy(buff,MESSAGE);
return buff;
}
while(token != NULL)
{
check = checkDigit(token);
if(check == 0)
break;
if(strcasecmp("add",identifier) == 0)
{
sum += atoi(token);
}
else if(strcasecmp("sub",identifier) == 0)
{
if(firstNumber == 0)
{
sum = atoi(token);
firstNumber = 1;
}
else sum -= atoi(token);
}
else if(strcasecmp("mult",identifier) == 0)
{
if(firstNumber == 0)
{ sum = atoi(token);
firstNumber = 1;
}
else sum *= atoi(token);
}
else if(strcasecmp("div",identifier) == 0)
{
if(firstNumber == 0)
{ sum = atoi(token);
firstNumber = 1;
}
else
{
if( atoi(token) == 0)
{ divideZero = 1;
break;
}
sum /= atoi(token);
}
}
arguments++;
token = strtok(NULL," \n");
}
if(check == 0)
strcpy(buff,"Invalid arguments\n");
else if(divideZero == 1)
strcpy(buff,DIVIDE);
else if ( arguments == 1)
strcpy(buff,"Too few arguments! Atleast two required\n");
else
{ int count = sprintf(buff,"%d",sum);
buff[count] = '\n';
count++;
buff[count] = '\0';
}
return buff;
}
int checkDigit(char * token)
{
int i = 0;
char buff[20];
strcpy(buff, token);
int c = 0;
while(i < strlen(token))
{
c = isdigit(buff[i]);
if(c == 0)
break;
i++;
}
if(c == 0)
return 0;
else return 1;
}
int main()
{
int count = write(STDOUT_FILENO, START, strlen(START));
if(count == -1)
{
perror("error: ");
exit(EXIT_FAILURE);
}
int pipeCheck = pipe(pd);
if(pipeCheck == -1)
{
perror("error");
exit(1);
}
pipeCheck = pipe(pd1);
if(pipeCheck == -1)
{
perror("error");
exit(1);
}
int pid = fork();
if(pid == -1)
{
perror("error");
exit(1);
}
if(signal(SIGCHLD,signalhandler) == SIG_ERR)
{
perror("error");
}
//############Server####################
if(pid == 0)
{
close(pd[1]);
close(pd1[0]);
for(;;)
{
int pipeClose;
char buff[500];
int rcount = read(pd[0],buff, 500);
if( rcount == -1)
{
perror("error");
}
char * temp[20]; //array of pointers
//boolean true false
int doTokenize = 0;
int skip = 0;
int runProgram = 0;
int quit = 0;
int listHelp = 0;
int counterTemp = 0;
//
int countToken = 0;//token to be used later to free space from heap (temp)
char * token;
char * buffArithmetic = (char *)malloc(50 * sizeof(char));
char * buffList = (char *)malloc(1000*sizeof(char));
if((rcount-1) == 0)
token = "a";//garbage value
else
token = strtok(buff," ");
//comparing cases
if(strcasecmp("add",token) == 0)
{
char * tempBuff = methodArithmetic(token, buffArithmetic);
int ccount = write(pd1[1], tempBuff, strlen(buffArithmetic));
if(ccount == -1)
perror("error");
}
else if(strcasecmp("sub",token) == 0)
{
char * tempBuff = methodArithmetic(token, buffArithmetic);
int ccount = write(pd1[1], tempBuff, strlen(buffArithmetic));
if(ccount == -1)
perror("error");
}
else if(strcasecmp("mult",token) == 0)
{
char * tempBuff = methodArithmetic(token, buffArithmetic);
int ccount = write(pd1[1], tempBuff, strlen(buffArithmetic));
if(ccount == -1)
perror("error");
}
else if(strcasecmp("div",token) == 0)
{
char * tempBuff = methodArithmetic(token, buffArithmetic);
int ccount = write(pd1[1], tempBuff, strlen(buffArithmetic));
if(ccount == -1)
perror("error");
}
else if(strcasecmp("list",token) == 0)
{
if(processCounter > 0)
{
token = strtok(NULL," \n");
if(token == NULL)
printActiveArray();
else if(strcmp(token,"all") == 0) //check why tokenizing first and checking caused the problem
{
if((token = strtok(NULL," \n")) == NULL)
printArray();
else write(pd1[1],"Invalid command\n",strlen("Invalid command\n"));
}
else write(pd1[1],"Invalid command\n",strlen("Invalid command\n"));
}
else write(pd1[1],"List is currently Empty\n",strlen("List is currently Empty\n"));
}
else if(strcasecmp("exit",token) == 0)
{
token = strtok(NULL," \n");
if(token!=NULL)
{
write(pd1[1],"Invalid command\n",strlen("Invalid command\n"));
}
else {
int count = kill(pid,SIGTERM);
if(count == -1)
perror("error");
}
}
else if(strcasecmp("help",token) == 0)
{
token = strtok(NULL," \n");
if(token == NULL)
write(pd1[1],HELP,strlen(HELP));
else write(pd1[1],MESSAGE,strlen(MESSAGE));
}
else if(strcasecmp("kill",token) == 0)
{
token = strtok(NULL," \n");
if(token==NULL)
{
write(pd1[1],"Invalid command\n",strlen("Invalid command\n"));
}
else {
char * stoken = token;
token = strtok(NULL," \n");
if(token == NULL)
{
int pid = 0;
int kcount = sscanf(stoken,"%d",&pid);
int check = checkDigit(stoken);
if(check == 0)
{
write(pd1[1],"Invalid arguments\n",strlen("Invalid arguments\n"));
}
else {
kcount = kill(pid,SIGTERM);
if(kcount == -1)
write(pd1[1],"Invalid process ID\n",strlen("Invalid process ID\n"));
else write(pd1[1],"Process Terminated\n",strlen("Process Terminated\n"));
}
}
else write(pd1[1],"Invalid arguments\n",strlen("Invalid arguments\n"));
}
}
else if(strcasecmp("run",token) == 0)
{
if((token = strtok(NULL," \n")) == NULL)
write(pd1[1],MESSAGE,strlen(MESSAGE));
else{
int i = 0;
while(token != NULL)
{ if( i == 0)
{ temp[i] = (char *)malloc(30*sizeof(char));
strcpy(temp[i],token);
i++;
}
temp[i] = (char *)malloc(30*sizeof(char));
strcpy(temp[i],token);
i++;
token = strtok(NULL, " \n");
}
temp[i] = NULL;
counterTemp = i;
skip = 1;
}
}
else
count = write(pd1[1], MESSAGE, strlen(MESSAGE));
//running process
if(skip == 1)
{
int status;
int sd[2];
int serverPipe = pipe(sd);
if(serverPipe == -1 )
{
perror("error");
}
fcntl(sd[1],F_SETFD,FD_CLOEXEC);
int serverPid = fork();
if(serverPid == -1)
perror("error");
if(serverPid == 0)
{
//write(sd[1],"saad",4);
count = execvp(temp[0],&temp[1]);
if(count == -1)
{
close(sd[0]);
perror("error");
write(sd[1],"salman",6);
close(sd[1]);
exit(99);
}
}
else if(serverPid > 0)
{
int waitChild = waitpid(serverPid, &status,WNOHANG);
close(sd[1]);
char tempbuff[50];
int count = read(sd[0],tempbuff,50);
if(count == 0)
{
processArray[processCounter] = (struct Node *)(malloc(50*sizeof(struct Node)));
processArray[processCounter] -> id = serverPid;
strcpy(processArray[processCounter] -> name, temp[0]);
processArray[processCounter] -> active = 1;
processCounter++;
write(pd1[1],"Executing...\n",strlen("Executing...\n"));
}
else
{ printf("%d\n",count);
write(pd1[1],"Invalid program execution\n",strlen("Invalid program execution\n"));
}
close(sd[0]);
}
}
int j = 0;
for(;j<counterTemp;j++)
free(temp[j]);
free(buffArithmetic);
}
}
//############Client##########################
else if(pid > 0)
{
close(pd[0]);
close(pd1[1]);
for(;;)
{
int pipeClose;
char buff[500];
count = read(STDIN_FILENO, buff, 500);
if(count == -1)
{
perror("error");
exit(EXIT_FAILURE);
}
buff[count-1] = '\0';
int pipeCount = write(pd[1], buff, count);
if(pipeCount == -1)
{
perror("error: dd");
exit(EXIT_FAILURE);
}
char resultBuff[1000];
count = read(pd1[0], buff, 1000);
if(count == -1)
{
perror("error: ");
exit(EXIT_FAILURE);
}
write(STDOUT_FILENO,buff,count);
}
}
}