Why does the waitid block not end until the child completes the action?

void *stack;
stack = malloc(STACK_SIZE);
if (-1 == clone(child_thread, stack + STACK_SIZE, 0, NULL)) {                                                                                                                                                                           
    perror("clone failed:");
}   
while(waitid(P_ALL, 0, NULL, WEXITED) != 0){ 
    perror("waitid failed:");
    sleep(1);
}   

The manual says:

If the child has already changed state, then these calls are returned immediately. Otherwise, they are blocked until the child changes state.

But in fact, he immediately returns:

waitid failed:: No child processes
waitid failed:: No child processes
...

Any tips?

0
source share
2 answers

You are using PID parameters. See further on the manual page:

The following parameters for Linux are intended for use with children created using clone (2); they cannot be used with waitid ():

   __WCLONE
          Wait  for "clone" children only.  If omitted then wait for "non-
          clone" children only.  (A "clone" child is one which delivers no
          signal, or a signal other than SIGCHLD to its parent upon termi-
          nation.)  This option is ignored if __WALL is also specified.

   __WALL (Since Linux 2.4) Wait for  all  children,  regardless  of  type
          ("clone" or "non-clone").

   __WNOTHREAD
          (Since  Linux  2.4) Do not wait for children of other threads in
          the same thread group. This was the default before Linux 2.4.
+1
source

, , waitid :

#include <sys/types.h>
#include <sys/wait.h>

...

siginfo_t signalInfo;
waitid(P_ALL, 0, &signalInfo, WEXITED | WSTOPPED | WNOWAIT | WNOHANG);

signalInfo : , , :

signalInfo.si_signo : For Signal Number
signalInfo.si_code : Usually SIGCHLD
signalInfo.si_errno) : Any error code set
signalInfo.si_status : For exit code of the child code

. WNOWAIT . / . , waitid WNOWAIT.

: . man- waitid .

+1

Source: https://habr.com/ru/post/1661428/


All Articles