|
Home > Archive > Unix Programming > April 2004 > child pid
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Aless 2004-04-25, 12:33 pm |
| hi
how do you get the child PID in a signal handler for the SIGCHLD signal?
thanks
--
Alex Lorca
| |
| Marc Rochkind 2004-04-25, 12:34 pm |
|
"Aless" <natre_dev@NetexploraPtoCom.spamRq> wrote in message
news:c6gmrm$ths$1@news1.nivel5.cl...
> hi
>
> how do you get the child PID in a signal handler for the SIGCHLD signal?
>
>
> thanks
>
> --
> Alex Lorca
You can't. The chief purpose of SIGCHLD is so it can be ignored, which
prevents exited children from becoming zombies (unwaited for but inactive
children). If you really want status from a child or to know which specific
one exited, you should use one of the wait system calls (and leave SIGCHLD
alone). It's possible to use them in a non-blocking fashion, so your program
doesn't wait if there are no zombies around.
(On some systems with optional Real-Time Signals, there is a way to get more
information in a signal handler, but even on those systems the standard
doesn't require that information to be supplied for classic signals like
SIGCHLD.)
--
Marc Rochkind
"Advanced UNIX Programming" (publishing April 2004)
www.basepath.com/aup
| |
| Nils O. Selåsdal 2004-04-25, 12:34 pm |
| And Aless said...
> hi
>
> how do you get the child PID in a signal handler for the SIGCHLD signal?
waitpid(2)
| |
| Marc Rochkind 2004-04-25, 1:34 pm |
|
"Marc Rochkind" <rochkind@basepath.com> wrote in message
news:PtCdnVRRXffUfxbd4p2dnA@speakeasy.net...
>
> "Aless" <natre_dev@NetexploraPtoCom.spamRq> wrote in message
> news:c6gmrm$ths$1@news1.nivel5.cl...
>
> You can't. The chief purpose of SIGCHLD is so it can be ignored, which
> prevents exited children from becoming zombies (unwaited for but inactive
> children). If you really want status from a child or to know which
specific
> one exited, you should use one of the wait system calls (and leave SIGCHLD
> alone). It's possible to use them in a non-blocking fashion, so your
program
> doesn't wait if there are no zombies around.
>
> (On some systems with optional Real-Time Signals, there is a way to get
more
> information in a signal handler, but even on those systems the standard
> doesn't require that information to be supplied for classic signals like
> SIGCHLD.)
>
> --
> Marc Rochkind
> "Advanced UNIX Programming" (publishing April 2004)
> www.basepath.com/aup
>
>
Forgot to mention that, of course, you can call one of the wait system calls
INSIDE the signal-handler for SIGCHLD, if you have arranged to catch it.
But, be careful how far you go... there are restrictions in what system
calls and other library functions you can call from within a signal handler
(wait and waitpid are OK).
--Marc
| |
|
| the problem is i'm trying to record the order of the the child process
termination in a array and the time in other. How do i record this
information whithout block the parent process??
if i do something like
pid = waitpid(WAIT_ANY,&status,WNOHANG);
pidarray[i] = pid;
pidttime[i] = time(NULL);
i don't get the correct pids and times...
the libc manual say:
"
Here's an example of how to use `waitpid' to get the status from all
child processes that have terminated, without ever waiting. This
function is designed to be a handler for `SIGCHLD', the signal that
indicates that at least one child process has terminated.
void
sigchld_handler (int signum)
{
int pid, status, serrno;
serrno = errno;
while (1)
{
pid = waitpid (WAIT_ANY, &status, WNOHANG);
if (pid < 0)
{
perror ("waitpid");
break;
}
if (pid == 0)
break;
notice_termination (pid, status);
}
errno = serrno;
}
"
so i try
pidarray[i] = pid;
pidttime[i] = time(NULL);
instead of notice_termination (pid, status)
but i get the "waitpid: No child processes" error...
what should i do?
En el artículo <PtCdnVRRXffUfxbd4p2dnA@speakeasy.net>, Marc Rochkind escribió:
>
> "Aless" <natre_dev@NetexploraPtoCom.spamRq> wrote in message
> news:c6gmrm$ths$1@news1.nivel5.cl...
>
> You can't. The chief purpose of SIGCHLD is so it can be ignored, which
> prevents exited children from becoming zombies (unwaited for but inactive
> children). If you really want status from a child or to know which specific
> one exited, you should use one of the wait system calls (and leave SIGCHLD
> alone). It's possible to use them in a non-blocking fashion, so your program
> doesn't wait if there are no zombies around.
>
> (On some systems with optional Real-Time Signals, there is a way to get more
> information in a signal handler, but even on those systems the standard
> doesn't require that information to be supplied for classic signals like
> SIGCHLD.)
>
--
.''`.
: :' : Alex Lorca
`. `'
| |
| Marc Rochkind 2004-04-25, 2:34 pm |
|
"Aless" <natre_dev@NetexploraPtoCom.spamRq> wrote in message
news:c6gt98$v6f$1@news1.nivel5.cl...
> the problem is i'm trying to record the order of the the child process
> termination in a array and the time in other. How do i record this
> information whithout block the parent process??
>
> if i do something like
>
> pid = waitpid(WAIT_ANY,&status,WNOHANG);
> pidarray[i] = pid;
> pidttime[i] = time(NULL);
>
> i don't get the correct pids and times...
>
>
> the libc manual say:
> "
> Here's an example of how to use `waitpid' to get the status from all
> child processes that have terminated, without ever waiting. This
> function is designed to be a handler for `SIGCHLD', the signal that
> indicates that at least one child process has terminated.
>
> void
> sigchld_handler (int signum)
> {
> int pid, status, serrno;
> serrno = errno;
> while (1)
> {
> pid = waitpid (WAIT_ANY, &status, WNOHANG);
> if (pid < 0)
> {
> perror ("waitpid");
> break;
> }
> if (pid == 0)
> break;
> notice_termination (pid, status);
> }
> errno = serrno;
> }
> "
>
> so i try
> pidarray[i] = pid;
> pidttime[i] = time(NULL);
> instead of notice_termination (pid, status)
> but i get the "waitpid: No child processes" error...
>
> what should i do?
>
A few questions:
1. WAIT_ANY is not a standard symbol. Is it defined to be -1?
2. What's the signal-handling context? I assume SIGCHLD was set to be
caught. Are you sure the SA_NOCLDWAIT flag is clear?
3. Are you sure signum is equal to SIGCHLD?
4. Are you sure the child hasn't been waited for somewhere else in the
process?
5. Assuming the above is OK, rerun the program with 0 instead of WNOHANG and
see what happens. You should not need WNOHANG in this case, as an
unwaited-for child is known to exist.
--
Marc Rochkind
"Advanced UNIX Programming" (publishing April 2004)
www.basepath.com/aup
| |
|
|
it was a problem with the caugth of the signal...
i change the simbols too, but i'm not sure if it was a problem
THANKS!
En el artículo <nY-dndrJ-PKJYRbdRVn-vg@speakeasy.net>, Marc Rochkind escribió:
>
> "Aless" <natre_dev@NetexploraPtoCom.spamRq> wrote in message
> news:c6gt98$v6f$1@news1.nivel5.cl...
>
> A few questions:
>
> 1. WAIT_ANY is not a standard symbol. Is it defined to be -1?
> 2. What's the signal-handling context? I assume SIGCHLD was set to be
> caught. Are you sure the SA_NOCLDWAIT flag is clear?
> 3. Are you sure signum is equal to SIGCHLD?
> 4. Are you sure the child hasn't been waited for somewhere else in the
> process?
> 5. Assuming the above is OK, rerun the program with 0 instead of WNOHANG and
> see what happens. You should not need WNOHANG in this case, as an
> unwaited-for child is known to exist.
>
--
.''`.
: :' : Alex Lorca
`. `'
|
|
|
|
|