I need to handle SIGCHLD
properly. How can I use it with my existing code? at the moment I cant wait for the child process unless I use 0
instead of WNOHANG|WUNTRACED
.
status = 0;
pid_t child, endID;
if(amp == 1)
signal( SIGCHLD, SIG_IGN );
child = fork();
if (child < 0) {
perror("fork() error\n");
exit(EXIT_FAILURE);
} else if (child == 0) {
// do sth here
perror("error\n");
} else {
//sleep(1)
If I remove sleep
then parent is executed 1st.. why?
waitpid()
return immediately because (a) the child has not died and (b) you said 'do not wait for the child to die'. You would normally wrap thewaitpid()
in a loop. Also, it is not clear why you're waiting forvars->pid
when the child pid is only available inchild
. It seems a little odd that you passvars->status
rather than&vars->status
or&status
. The variablesstatus
andendID
are unused. – Butylenewaitpid()
,WNOHANG
, andSIGCHLD
. – Butylene