-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.c
More file actions
45 lines (34 loc) · 863 Bytes
/
exec.c
File metadata and controls
45 lines (34 loc) · 863 Bytes
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 <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdlib.h>
#include "macros.h"
// fork(), exec(), waitpid()
int main() {
pid_t pid;
int retvalue;
SYSC(pid, fork(), "fork error");
if (pid == 0) {
// Child Process
// Exec ls -l
char *args[] = {"ls", "-l", NULL};
execvp("ls", args);
// Exec error
perror("exec");
return 1;
} else {
// Father Process
// Wait Child Process
int status;
pid_t child_pid;
SYSC(child_pid, waitpid(pid, &status, 0), "waitpid error");
if (WIFEXITED(status)) {
printf("Child process exited with code: %d\n", WEXITSTATUS(status));
} else {
printf("Child process exit error \n");
}
}
return 0;
}