-
Notifications
You must be signed in to change notification settings - Fork 3
/
fork_and_exec.c
63 lines (50 loc) · 1.51 KB
/
fork_and_exec.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
/* ns_run.c
Copyright 2013, Michael Kerrisk
Licensed under GNU General Public License v2 or later
This program is similar is a simple demonstration
of fork and exec functionality.
*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
/* A simple error-handling function: print an error message based
on the value in 'errno' and terminate the calling process */
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
static void
usage(char *pname)
{
fprintf(stderr, "Usage: %s cmd [arg...]\n", pname);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int fd, opt;
pid_t pid;
if (argc <= optind)
usage(argv[0]);
pid = fork();
if (pid == -1)
errExit("fork");
if (pid != 0) { /* Parent */
printf("Grand-Parent\n--(PID=%ld)\n",
(long) getppid());
printf("Parent\n----(PID=%ld) created\nChild\n------(PID=%ld)\n",
(long) getpid(), (long) pid);
printf("(((( Parent waiting on it's child ))))\n");
if (waitpid(-1, NULL, 0) == -1) /* Wait for child */
errExit("waitpid");
exit(EXIT_SUCCESS);
}
/* Child falls through to code below */
printf("\nChild (PID=%ld) exists with Parent (PID=%ld)\n.....\n",
(long) getpid(), (long) getppid());
sleep(3);
execvp(argv[optind], &argv[optind]);
errExit("execvp");
}