-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaitdemo2.c
42 lines (34 loc) · 829 Bytes
/
waitdemo2.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
#include <stdio.h>
#include <stdlib.h>
#define DELAY 3
main()
{
int newpid;
void child_code(), parent_code();
printf("before : mypid is %d \n", getpid());
if( (newpid = fork()) == -1 )
perror("fork");
else if(newpid == 0)
child_code(DELAY);
else
parent_code(newpid);
}
void child_code(int delay)
{
printf("child %d here. will sleep for %d seconds \n",getpid(),delay);
sleep(delay);
printf(" child done. about to exit\n");
exit(17);
}
void parent_code(int childpid)
{
int wait_rv;
int child_status;
int high_8, low_7, bit_7;
wait_rv = wait(&child_status);
printf(" done waiting for %d. Wait returned : %d \n", childpid,wait_rv);
high_8 = child_status >> 8;
low_7 = child_status & 0x7F;
bit_7 = child_status & 0x80;
printf("status : exit = %d, sig = %d, core = %d \n", high_8,low_7,bit_7);
}