Manera fácil de abandonar a los niños en exec

Is there a simple, standard way to abandon all children before execing a new process image? Similar to setting a file descriptor to close on exec, it would be nice to make all your children be adopted by init. You can ignore SIGCHLD which would (on some platforms) give a partial solution to the problem raised aquí, but that's certainly not portable. I don't really see a use case for this behavior, but I'm curious to know if there is a standard mechanism.

preguntado el 27 de noviembre de 13 a las 07:11

1 Respuestas

That's an very interesting question.

While the signal handlers themselves are set back to intelligent defaults when you exec (and I think pending signals are cleared), you are still that same process ID (PID) in the hierarchy so you will still es dueño the children that are running.

That means that future SIGCHLD signals will find their way to you as those children exit.

One way to avoid this is to, rather than exec, perform a sneaky switcheroo. By that, I mean, fork first, then have the parent process simply exit (so the current children are adopted by init).

The child process resulting from the fork has a totally different PID so has only a sibling relationship with the other children at that point. It can exec your new program and carry on, safe in the knowledge it is childless.

respondido 27 nov., 13:08

SIG_IGN is typically retained across the exec, but can easily be reset. But some platforms apparently have differing semantics when SIGCHLD is ignored. I believe there are some platforms in which the child is a child of init immediately after the fork rather than a child of the process doing the fork. - Guillermo Pursell

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.