-
Notifications
You must be signed in to change notification settings - Fork 25
/
fork1.c
45 lines (43 loc) · 1.21 KB
/
fork1.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
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
pid_t pid;
int status = 0;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
//execlp("/bin/sleep", "sleep", "100", (char*) NULL);
//execlp("/bin/ls", "ls", NULL);
execlp("/usr/bin/grep", "/usr/bin/grep", "asfasfd", "asdfadf", (char*) NULL);
}
else { /* parent process */
/* parent wait for child complete */
printf("Creating child %d\n", pid);
pid_t pid1;
do {
pid1 = wait (&status);
if (pid1 == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
printf ("Child %d complete with status %d\n", pid1, status);
exit(0);
}
}