-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparentChildSync.c
56 lines (48 loc) · 1023 Bytes
/
parentChildSync.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
/******************
author : bsm
description: synhronization between parent and child process
***************/
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid;
char *message;
int n, exit_code;
printf("starting fork process\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork process failed");
exit(1);
case 0:
message = "This is the child process";
n = 5;
exit_code = 37;
break;
default:
message = "This is the parent process";
n = 3;
exit_code = 0;
break;
}
for(; n > 0; n--) {
puts(message);
sleep(1);
}
if (pid != 0) {
int stat_val;
pid_t child_pid;
child_pid = wait(&stat_val);
printf("Child has finished: PID = %d\n", child_pid);
if(WIFEXITED(stat_val))
printf("Child exited with code %d\n", WEXITSTATUS(stat_val));
else
printf("Child terminated abnormally\n");
}
exit(exit_code);
}