-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforkExec.c
35 lines (31 loc) · 937 Bytes
/
forkExec.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("Hello from child process!\n");
execlp("ls", "ls", "-l", NULL); // Execute ls command with arguments
perror("execlp"); // Print error if execlp fails
exit(1); // Exit child process with an error code if execlp fails
} else if (pid > 0) {
// Parent process
printf("Parent waiting for child...\n");
int status;
waitpid(pid, &status, 0); // Wait for child process to finish
// Check the child's exit status
if (WIFEXITED(status)) {
int child_exit_code = WEXITSTATUS(status);
printf("Child process exited with status: %d\n", child_exit_code);
} else {
printf("Child process terminated abnormally.\n");
}
} else {
// Error handling for fork failure
perror("fork");
exit(1);
}
return 0;
}