child pid
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > child pid




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    child pid  
Aless


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-25-04 05:33 PM

hi

how do you get the child PID in a signal handler for the SIGCHLD signal?


thanks

--
Alex Lorca





[ Post a follow-up to this message ]



    Re: child pid  
Marc Rochkind


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-25-04 05: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







[ Post a follow-up to this message ]



    Re: child pid  
Nils O. Selåsdal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-25-04 05:34 PM

And Aless said...

> hi
>
> how do you get the child PID in a signal handler for the SIGCHLD signal?
waitpid(2)






[ Post a follow-up to this message ]



    Re: child pid  
Marc Rochkind


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-25-04 06: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







[ Post a follow-up to this message ]



    Re: child pid  
Aless


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-25-04 06:34 PM

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 specifi
c
> 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 progr
am
> doesn't wait if there are no zombies around.
>
> (On some systems with optional Real-Time Signals, there is a way to get mo
re
> 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
`. `'





[ Post a follow-up to this message ]



    Re: child pid  
Marc Rochkind


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-25-04 07: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







[ Post a follow-up to this message ]



    Re: child pid  
Aless


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-25-04 09:35 PM


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 a
nd
> see what happens. You should not need WNOHANG in this case, as an
> unwaited-for child is known to exist.
>


--
.''`.
: :' :   Alex Lorca
`. `'





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:10 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register