-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMywaitpid(linux).txt
49 lines (48 loc) · 1.33 KB
/
Mywaitpid(linux).txt
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
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
pid_t id=fork();
if(id==0)
{//child
while(1)
{
printf("this is child id:%d\n",getpid());
sleep(4);
}
}
else
{//father
printf("i am father i am waitting...\n ");
int status = 0; //[*]
// do
// {
//pid_t ret = wait(&status);
pid_t ret=wait(&status); //[*] pay attention to this "id"
// if(ret==0)
// {
// printf("fatherproc is running...\n");
// sleep(1);
//}
if(ret>0)
{
printf("waitting success!\n");
sleep(1);
if(WIFEXITED(status))
{
printf("child is exited! exitcode:%d\n",WEXITSTATUS(status));
return 0;
}
}
else
{
perror("waitfalse!");
return 0;
}
// }while(1);
}
return 0;
}