-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathson.c
78 lines (61 loc) · 1.45 KB
/
son.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
char *gets(char *str);
int main(int argc, char* argv[]){
char input[50];
char command[50];
char *inputptr;
pid_t wpid, pid, parent_pid;
int status = 0;
char *noktalivirgul;
inputptr = input;
printf("todo:>");
gets(inputptr);
while(strcmp(inputptr, "quit")!= 0) {
sprintf(command, "\"%s\"", input);
noktalivirgul = strstr(inputptr, ";");
if(noktalivirgul != NULL)
{
char *p = strtok(inputptr, ";");
while(p != NULL)
{
printf("WE NEED CHILDS NOW \n");
pid = fork();
if(pid < 0)
{
fprintf(stderr, "Fork failed");
}
else if(pid == 0)
{
pid = getpid();
printf("I AM THE CHILD AND MY PID: %d\n",pid );
printf("WE ARE DOING >>> %s\n",p);
system(p);
p = strtok(NULL, ";");
status = 0;
}
else
{
printf("parent_pid = %d\n", pid);
waitpid(pid, &status,0);
exit(0);
}
}
printf("todo:>");
gets(inputptr);
}
else
{
printf("I DO NOT NEED CHILD I AM PARENT AND DOING >>> %s\n",input);
system(input);
printf("todo:>");
gets(inputptr);
}
}
return 0;
}