One such program that uses a wait function like this is this one:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int pid,fd[2]; int n; char line[20];
if(pipe(fd)<0) {
printf("Error creating pipe");
} else {
pid=fork();
if(pid<0) {
printf("Error while forking");
} else {
if(pid>0) {
close(fd[0]);
write(fd[1],"Hello\n",6);
while(wait((int *)0)!=pid);
} else {
close(fd[1]);
n=read(fd[0],line,20);
if(n<0)
printf("Error reading a file");
write(1,line,n);
}
}
}
return 0;
}
See man wait(2).
wait((int *)0)
calls waitpid(-1, (int *)0, 0)
. The man page states:
If status is not NULL, wait() and waitpid() store status information in the int to which it points.
Here, status
is NULL (0). Thus, your call to wait
waits for a state change in any child process, and does not return a status. The call merely checks to see if a state change occurred for a specific child process (pid
in your case).
stager's answer is correct. Though it should be noted that the cast is entirely unnecessary since according to the standard, 0 used in a pointer context is the NULL pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With