 |
|
 |
|
|
 |
couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 12:16 AM
If my process handles just one child, do I have the need for WNOHANG in
waitpid? I currently wrote the function below, but I'm thinking that I
only need waitpid there because I want WUNTRACED since I want to detect
SIGSTOP signals. Is this true?
static void sigchild(int sig)
{
pid_t pid; int stat;
for (;;) {
pid = waitpid(-1, &stat, WNOHANG | WUNTRACED);
if (pid <= 0)
break;
if (WIFEXITED(stat) ) {
died = 1;
}
if (WIFSTOPPED(stat)) {
stopped = 1;
}
}
}
How can I recognize the child has received a SIGCONT?
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 12:16 AM
On Sep 15, 9:19 am, dbas...@yahoo.com.br (Daniel C. Bastos) wrote:
> for (;;) {
> pid = waitpid(-1, &stat, WNOHANG | WUNTRACED);
> if (pid <= 0)
> break;
>
> if (WIFEXITED(stat) ) {
> died = 1;
> }
>
> if (WIFSTOPPED(stat)) {
> stopped = 1;
> }
> }
Your code really doesn't make any sense. The 'WNOHANG' flag says "I
don't want to wait until something happens". Your "for(;;)" says "I
want to wait forever no matter what happens". Your "if (pid <= 0)
break;" says "I want to stop if there's nothing to do". These three
things are mutually inconsistent, so whatever it is you're trying to
do, this definitely isn't the way to do it.
DS
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 12:16 AM
David Schwartz <davids@webmaster.com> wrote in
news:1189900585.777967.160140@y42g2000hsy.googlegroups.com:
> On Sep 15, 9:19 am, dbas...@yahoo.com.br (Daniel C. Bastos)
> wrote:
>
>
> Your code really doesn't make any sense. The 'WNOHANG' flag says
> "I don't want to wait until something happens". Your "for(;;)"
> says "I want to wait forever no matter what happens". Your "if
> (pid <= 0) break;" says "I want to stop if there's nothing to
> do". These three things are mutually inconsistent, so whatever
> it is you're trying to do, this definitely isn't the way to do
> it.
>
How would you recommend expressing: "I want to reap all children
until there are no more children to be reaped, and then exit the
signal handler"?
MV
--
I do not want replies; please follow-up to the group.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 06:31 AM
In article <Xns99ACCB833B025jpmvrealtime@198.186.190.155>,
Martin Vuille <jpmv27@yahoo.com> wrote:
> David Schwartz <davids@webmaster.com> wrote in
> news:1189900585.777967.160140@y42g2000hsy.googlegroups.com:
>
I think this is wrong. If he has children that are still running, he
doesn't want to block until they exit. He just wants to reap all the
children that have exited at the time the signal handler runs.
[vbcol=seagreen]
> How would you recommend expressing: "I want to reap all children
> until there are no more children to be reaped, and then exit the
> signal handler"?
But you said in your original message that your process only has one
child. In that case it should be safe for your SIGCHLD handler to just
call waitpid() once, without WNOHANG.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 06:31 AM
On Sep 15, 5:00 pm, Martin Vuille <jpm...@yahoo.com> wrote:
> How would you recommend expressing: "I want to reap all children
> until there are no more children to be reaped, and then exit the
> signal handler"?
if (fork()!=0) _exit(0);
Or did you mean all children that need to be reaped *now*?
DS
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 06:31 AM
Barry Margolin <barmar@alum.mit.edu> wrote in
news:barmar-F5C977.22281115092007@comcast.dca.giganews.com:
> In article <Xns99ACCB833B025jpmvrealtime@198.186.190.155>,
> Martin Vuille <jpmv27@yahoo.com> wrote:
>
>
>
> But you said in your original message that your process only has
> one child. In that case it should be safe for your SIGCHLD
> handler to just call waitpid() once, without WNOHANG.
>
I was not the OP. I'm just someone who has used similar code, and was
curious about a better way to do things.
MV
--
I do not want replies; please follow-up to the group.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 06:31 AM
David Schwartz <davids@webmaster.com> wrote in
news:1189910332.248328.141800@19g2000hsx.googlegroups.com:
> On Sep 15, 5:00 pm, Martin Vuille <jpm...@yahoo.com> wrote:
>
>
> if (fork()!=0) _exit(0);
>
> Or did you mean all children that need to be reaped *now*?
>
I must be really confused because I don't see how the above allows a
parent process to spawn children and then monitor their state (i.e.,
to know when they exit) and reap them so that there are no zombies
left around.
MV
--
I do not want replies; please follow-up to the group.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 06:31 AM
Barry Margolin <barmar@alum.mit.edu> writes:
> In article <Xns99ACCB833B025jpmvrealtime@198.186.190.155>,
> Martin Vuille <jpmv27@yahoo.com> wrote:
>
>
> I think this is wrong. If he has children that are still running, he
> doesn't want to block until they exit. He just wants to reap all the
> children that have exited at the time the signal handler runs.
That's correct, although this does not apply to my case because I am in
need of only one child; but I had this function sort of written this way
for a long time, and I just copied it over to this program. But if later
I decided to handle more children, then the work of reaping them all
will be done already.
Barry Margolin's reasoning above is the same that Richard Stevens shows
in UNIX Network programming when he shows that if too many children all
exist at about the same time, a call to wait() will not suffice to avoid
zombie processes.
>
> But you said in your original message that your process only has one
> child. In that case it should be safe for your SIGCHLD handler to just
> call waitpid() once, without WNOHANG.
Okay; that's a ``yes'' to my question --- now snipped without a trace.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 06:31 AM
dbast0s@yahoo.com.br (Daniel C. Bastos) writes:
> Barry Margolin <barmar@alum.mit.edu> writes:
>
>
> That's correct, although this does not apply to my case because I am in
> need of only one child; but I had this function sort of written this way
> for a long time, and I just copied it over to this program. But if later
> I decided to handle more children, then the work of reaping them all
> will be done already.
>
> Barry Margolin's reasoning above is the same that Richard Stevens shows
> in UNIX Network programming when he shows that if too many children all
> exist at about the same time, a call to wait() will not suffice to avoid
> zombie processes.
>
>
> Okay; that's a ``yes'' to my question --- now snipped without a trace.
Actually that's a ``no'' to my first question.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: couple of questions on waitpid and children |
 |
 |
|
|
09-16-07 12:26 PM
On 2007-09-16 00:00, Martin Vuille <jpmv27@yahoo.com> wrote:
> David Schwartz <davids@webmaster.com> wrote in
> news:1189900585.777967.160140@y42g2000hsy.googlegroups.com:
>
>
> How would you recommend expressing: "I want to reap all children
> until there are no more children to be reaped, and then exit the
> signal handler"?
I think
while ((pid = waitpid(-1, &stat, WNOHANG | WUNTRACED)) > 0) {
if (WIFEXITED(stat) ) {
died = 1;
}
if (WIFSTOPPED(stat)) {
stopped = 1;
}
}
is slightly more readable.
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 04:21 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
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
|
 |
|
 |
|