nik
nik

Reputation: 23

Child processes lost while reaping in C on linux platform

Is there any limitations on reaping number of child processes ? let's say my system is running a parent process and 500+ child processes.

Parent is doing a waitpid(-1,status,0) in a blocking mode. I do see sometimes waitpid returns -1.

if 500 child finishes at the same time and reports their status to the parent, is there a case a child processes can be missed ?

Upvotes: 0

Views: 340

Answers (1)

ikegami
ikegami

Reputation: 385839

When a system call returns an error (such as when waitpid returns -1), consult errno (usually via perror) if you need to determine what error occurred.

According to man 2 waitpid on my system, the possible errors are pretty limited:

  • ECHILD: The process specified by pid does not exist or is not a child of the calling process. (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.)

  • EINTR: WNOHANG was not set and an unblocked signal or a SIGCHLD was caught; see signal(7).

  • EINVAL: The options argument was invalid.

Additionally, EFAULT could be returned if you pass a bad address for the second argument. It appears to be the case based on the code you said you used.[1]

waitpid(-1,status,0)

should be

waitpid(-1,&status,0)

If you misspoke or if you're still getting an error after fixing this problem, two possibilities are left:

  • The process has no children. Any children it might have created have already been reaped.
  • You setup a signal handler, and a signal came in while you were waiting for a child to end. Just call waitpid again.

  1. ALWAYS enable your compiler's warnings, and address them as if they were errors! With gcc, I use -Wall -Wextra -pedantic.

Upvotes: 1

Related Questions