-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-exec.c
88 lines (79 loc) · 2.22 KB
/
03-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
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int f;
// execl
f = fork();
if (f == 0) {
printf("execl --> ls -a\n");
int status_code = execl("/bin/ls", "ls", "-a", (char*)NULL);
if (status_code == -1) {
printf("Child process did not terminate correctly!\n");
return 1;
}
}
wait(NULL);
// execv
f = fork();
if (f == 0) {
printf("execv --> ls -a\n");
char* argument_list[] = {"ls", "-a", NULL};
int status_code = execv("/bin/ls", argument_list);
if (status_code == -1) {
printf("Child process did not terminate correctly!\n");
return 1;
}
}
wait(NULL);
// execlp
f = fork();
if (f == 0) {
printf("execlp --> ls -a\n");
int status_code = execlp("ls", "ls", "-a", (char*)NULL);
if (status_code == -1) {
printf("Child process did not terminate correctly!\n");
return 1;
}
}
wait(NULL);
// execvp
f = fork();
if (f == 0) {
printf("execvp --> ls -a\n");
char* argument_list[] = {"ls", "-a", NULL};
int status_code = execvp("ls", argument_list);
if (status_code == -1) {
printf("Child process did not terminate correctly!\n");
return 1;
}
}
wait(NULL);
// execle
f = fork();
if (f == 0) {
printf("execle --> ls -a\n");
char* const env_list[] = {"A=BC", "D=EF", NULL};
int status_code = execle("/bin/ls", "ls", "-a", (char*)NULL, env_list);
if (status_code == -1) {
printf("Child process did not terminate correctly!\n");
return 1;
}
}
wait(NULL);
// execve
f = fork();
if (f == 0) {
printf("execve --> ls -a\n");
char* argument_list[] = {"ls", "-a", NULL};
char* const env_list[] = {"A=BC", "D=EF", NULL};
int status_code = execve("/bin/ls", argument_list, env_list);
if (status_code == -1) {
printf("Child process did not terminate correctly!\n");
return 1;
}
}
wait(NULL);
printf("Parent process terminated successfully!\n");
return 0;
}