-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait.cpp
More file actions
29 lines (24 loc) · 677 Bytes
/
wait.cpp
File metadata and controls
29 lines (24 loc) · 677 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
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/wait.h>
int main() {
pid_t p;
printf("Before fork\n");
p = fork();
if (p < 0) {
// Error handling for fork failure
perror("Fork failed");
return 1;
}
if (p == 0) { // Child process
printf("I am the child having ID %d\n", getpid());
printf("My parent's ID is %d\n", getppid());
} else { // Parent process
wait(NULL); // Wait for child process to complete
printf("My child's ID is %d\n", p);
printf("I am the parent having ID %d\n", getpid());
}
printf("Common\n");
return 0;
}