status = 0;
pid_t child, endID;
if(amp == 1)
signal( SIGCHLD, SIG_IGN );
< br /> child = fork();
if (child <0) {
perror("fork() error ");
exit(EXIT_FAILURE);< br />
} else if (child == 0) {
// do sth here
perror("error ");
} else {
//sleep(1)
If I delete sleep then the father is executed first… why?
static void
child_handler(int sig)
{
pid_t pid;
int status;
/* EEEEXTEERMINAAATE! */
while((pid = waitpid(-1, &status, WNOHANG))> 0)
;
}
/* Establish handler. */< br />struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;
sigaction(SIGCHLD, &sa, NULL);
Of course, this is meaningless. If parents just ignore SIGCHLD, the children will harvest silently and will not become zombies.
Quoting TLPI:
Explicitly setting the disposition of SIGCHLD to SIG_IGN causes any
child process that subsequently terminates to be immediately removed
from the system < strong>instead of being converted into a zombie.
So something like this should do the trick for you:
signal(SIGCHLD, SIG_IGN); /* Silently (and portably) reap children. */
I need to handle SIGCHL correctly D. How to use it in existing code. Currently I cannot wait for the child process unless I use 0 instead of WNOHANG | WUNTRACED.
status = 0; < br /> pid_t child, endID;
if(amp == 1)
signal( SIGCHLD, SIG_IGN );
child = fork();
if (child <0) {
perror("fork() error ");
exit(EXIT_FAILURE);
} else if (child == 0 ) {
// do sth here
perror("error ");
} else {
//sleep(1)
< p>If I delete the sleep then the father is executed first…why?
This is the beginning (but read below):
static void
child_handler(int sig)
{
pid_t pid;
int status;
/* EEEEXTEERMINAAATE! */
while((pid = waitpid (-1, &status, WNOHANG))> 0)
;
}
/* Establish handler. */
struct sigaction sa;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;
sigaction(SIGCHLD, &sa, NULL);
Of course, this is meaningless. If parents just ignore SIGCHLD, children will harvest silently and will not become zombies.
Quoting TLPI:
< /p>
Explicitly setting the disposition of SIGCHLD to SIG_IGN causes any
child process that subsequently terminates to be immediately removed
from the system instead of being converted into a zombie.
So something like this should do the trick for you:
signal(SIGCHLD, SIG_IGN); /* Silently ( and portably) reap children. */