-
Notifications
You must be signed in to change notification settings - Fork 39
/
exec.c
77 lines (68 loc) · 1.64 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
extern char **environ;
static void
die(char *msg)
{
printf("--- %s error ---\n", msg);
exit(1);
}
int
main(int argc, char *argv[])
{
puts("<<execl>>");
if (fork() != 0) { /* parent */
wait(NULL);
}
else { /* child */
execl("./args", "PROGNAME", "called by execl", NULL);
die("execl");
}
puts("<<execlp>>");
if (fork() != 0) { /* parent */
wait(NULL);
}
else { /* child */
execlp("./args", "PROGNAME", "called by execlp", NULL);
die("execlp");
}
puts("<<execle>>");
if (fork() != 0) { /* parent */
wait(NULL);
}
else { /* child */
execle("./args", "PROGNAME", "called by execle", NULL, environ);
die("execle");
}
puts("<<execv>>");
if (fork() != 0) { /* parent */
wait(NULL);
}
else { /* child */
char *args[4] = { "PROGNAME", "called by execv", NULL };
execv("./args", args);
die("execv");
}
puts("<<execvp>>");
if (fork() != 0) { /* parent */
wait(NULL);
}
else { /* child */
char *args[4] = { "PROGNAME", "called by execvp", NULL };
execvp("./args", args);
die("execvp");
}
puts("<<execve>>");
if (fork() != 0) { /* parent */
wait(NULL);
}
else { /* child */
char *args[4] = { "PROGNAME", "called by execve", NULL };
execve("./args", args, environ);
die("execve");
}
exit(0);
}