-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork.c
More file actions
47 lines (32 loc) · 925 Bytes
/
fork.c
File metadata and controls
47 lines (32 loc) · 925 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
46
47
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include "macros.h"
// fork(), wait()
int main() {
pid_t pid;
// Create Child process
SYSC(pid, fork(), "fork error");
if (pid == 0) {
// Child process
printf("I'm the Child process, PID: %d\n", getpid());
// Exit from child process
exit(EXIT_SUCCESS);
} else {
// Father Process
printf("I'm the Father process, PID: %d\n", getpid());
// Wait Child Process
int status;
pid_t child_pid;
SYSC(child_pid, wait(&status), "wait error");
if (WIFEXITED(status)) {
printf("Child process exited with code: %d\n", WEXITSTATUS(status));
} else {
printf("Child process exit error \n");
}
// Exit from father process
exit(EXIT_SUCCESS);
}
}