How do I wait for the child process ?

C

codefixer

Hi,

I have a situation where I have to handle the following scenario.
The main() must wait for child to complete or the main() must kill the
child after 3 seconds and exit.

/* Assume everythign is declared */

time(&start);
while ( childpid != wait(&status) )
{
time(&end);
diff = difftime(end,start);
fprintf(stderr, "\n%ld",diff);
if (diff >= 3.00)
{
/* Kill child */
exit(0);
}
}
}


Even though the child is taking more than 3 seconds, this doesn't seem
to work. Any idea why ?

Thanks.
 
W

Walter Roberson

I have a situation where I have to handle the following scenario.
The main() must wait for child to complete or the main() must kill the
child after 3 seconds and exit.
/* Assume everythign is declared */
time(&start);
while ( childpid != wait(&status) )

Wait is unconditional.
{
time(&end);
diff = difftime(end,start);

So all you are calculating there is the time until you did get a status
report. That's not at all the same as limiting the time you want to
wait for a report.

To limit the time you want to wait for a report, you may wish
to investigate alarm().

Also, you may wish to take into account the fact that under
some circumstances, the report you get will not be that the
child has completed, but rather that the child has stopped
(e.g., SIGSTOP such as if the user ^Z'd in a bourne shell
derivative.)
 
M

Mark McIntyre

Hi,

I have a situation where I have to handle the following scenario.
The main() must wait for child to complete or the main() must kill the
child after 3 seconds and exit.

wait and its friends are not part of C, but are typically part of the
unix OS. You will get a better response in comp.unix.programmer.
 
M

Mark McIntyre

On 28 Mar 2005 19:03:05 GMT, in comp.lang.c ,
Wait is unconditional.

and offtopic here.
So all you are calculating there is the time until you did get a status
report. That's not at all the same as limiting the time you want to
wait for a report.

you have absolutely no way to know that, since you don't even know
what wait() does on that guy's OS. I grant you, it may do what it does
on unix but then again it may not.

Please, do redirect people to the right group, and if you /must/
answer, make it clear that you're guessing.
 
C

codefixer

Mark said:
wait and its friends are not part of C, but are typically part of the
unix OS. You will get a better response in comp.unix.programmer.

OK Thanks. I will try it there.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----
 
W

Walter Roberson

On 28 Mar 2005 19:03:05 GMT, in comp.lang.c ,
(e-mail address removed)-cnrc.gc.ca (Walter Roberson) wrote:
you have absolutely no way to know that, since you don't even know
what wait() does on that guy's OS.

I doubt there has ever been an OS written that would look forward
into a user's program for an occurance of a time difference, and modify
the behaviour it assigns to wait() so as to wait no longer than the
tested difference.

Please, do redirect people to the right group, and if you /must/
answer, make it clear that you're guessing.

When you answer enough Usenet questions, you develop a good sense of
Read Programmer's Mind. "guess" doesn't come into it.
 
K

Keith Thompson

I doubt there has ever been an OS written that would look forward
into a user's program for an occurance of a time difference, and modify
the behaviour it assigns to wait() so as to wait no longer than the
tested difference.



When you answer enough Usenet questions, you develop a good sense of
Read Programmer's Mind. "guess" doesn't come into it.

Sure, but the folks who have the knowledge necessary to confirm that
your well-informed response is correct (or to point out any errors you
might have made) hang out in comp.unix.programmer, not in comp.lang.c.
(As we've already discussed at length in the endless topicality
debates that make up too much of the traffic here.)
 
W

Walter Roberson

[email protected] (Walter Roberson) said:
Sure, but the folks who have the knowledge necessary to confirm that
your well-informed response is correct (or to point out any errors you
might have made) hang out in comp.unix.programmer, not in comp.lang.c.

By saying that the appropriate folk are in comp.unix.programmer then
you are implicitly agreeing that the question was one about unix (or
work-alike systems.) If so, then my answer was certainly not a
"guess".

Mark's point was that we weren't given enough explicit information to
even know that it was a unix or unix-like OS, and thus that my
unix-like answer was a "guess" about the host OS behaviour. He is, of
course correct... in about the same sense as supposing that water put
on to boil might freeze instead.
 
K

Keith Thompson

[email protected] (Walter Roberson) said:
By saying that the appropriate folk are in comp.unix.programmer then
you are implicitly agreeing that the question was one about unix (or
work-alike systems.) If so, then my answer was certainly not a
"guess".

Mark said it was a guess. I didn't, and I agree with you that the OP
was almost certainly asking about a Unix system. My point was simply
that it was off-topic, and should have been posted to
comp.unix.programmer and *not* to comp.lang.c.
 
M

Mark McIntyre

On 28 Mar 2005 22:29:56 GMT, in comp.lang.c ,
I doubt there has ever been an OS written that would look forward
into a user's program for an occurance of a time difference, and modify
the behaviour it assigns to wait() so as to wait no longer than the
tested difference.

I've no idea what you're trying to say. The point is, wait() need not
have anything to do with waiting for children. It could wait for a
signal, wait for user input, wait for some time interval to pass. I've
worked with several osen that didn't even have the concept of wait().
When you answer enough Usenet questions, you develop a good sense of
Read Programmer's Mind. "guess" doesn't come into it.

Axiomatically, if answering an offtopic question in CLC, you're
guessing.
 
M

Mark McIntyre

On 29 Mar 2005 01:21:54 GMT, in comp.lang.c ,
By saying that the appropriate folk are in comp.unix.programmer then
you are implicitly agreeing that the question was one about unix (or
work-alike systems.) If so, then my answer was certainly not a
"guess".

"if so", I'd agree. Did the OP state he was on unix? If not, you were
guessing.
Mark's point was that we weren't given enough explicit information to
even know that it was a unix or unix-like OS, and thus that my
unix-like answer was a "guess" about the host OS behaviour.

No, my point was that anyone can say anything they like about unix
system calls in CLC, and not get called up for their error. This is
because the experts hang out over in CUP. So as far as I'm concerned,
your answer was an uncorroborative guess.
He is, of
course correct... in about the same sense as supposing that water put
on to boil might freeze instead.

In about the same sense as it makes to answer unix questions here.
 
W

Walter Roberson

No, my point was that anyone can say anything they like about unix
system calls in CLC, and not get called up for their error. This is
because the experts hang out over in CUP. So as far as I'm concerned,
your answer was an uncorroborative guess.

POSIX 1003.1-1990, section 3.2.1.2

The wait() function shall suspend execution of the calling process
until status information for one of its terminated child processes
is available, or until a signal whose action is either to
execute a signal-catching function or to terminate the process
is delivered. If status information is available prior to the call
to wait(), return shall be immediate.


As I said, it was not a guess.

You are, of course, free to consider anything you want to be "a guess",
but if your expressed rule is that anything not specified in one of the
C standards is "a guess", then unless you want to be hypocritical, then
you should be qualifying even remarks such as "The Sun rose here today"
as "a guess", and you should be qualifying as a "guess" the very
existance of the newsgroups you redirect people to. "I'm guessing that
there might be such a thing as other newsgroups, and I'm guessing that
one of them might be named comp.unix.programming, and I'm guessing that
this topic might be appropriate there." For that matter, you should, if
you are to be consistant, be treating the very existance of the people
you are replying to as being "a guess" -- and you are only guessing
that someone actually posted the message, rather than the universe
spontaneously generating the message. But then, the existance of the
universe is another "guess" too, since it's existance and properties is
not specified in one of the C standards.
 
M

Mark McIntyre

On 29 Mar 2005 20:29:03 GMT, in comp.lang.c ,
POSIX 1003.1-1990, section 3.2.1.2

The wait() function shall suspend execution of the calling process

So what? Posix isn't part of C, and for all I know, you made up this
reference - after all, anyone can type in some random text. You're
merely strengthening my argument.
As I said, it was not a guess.

I'm sure it wasn't, but in the context of CLC, it was unprovable.
You are, of course, free to consider anything you want to be "a guess",
but if your expressed rule is that anything not specified in one of the
C standards is "a guess",

in the context of CLC.
then unless you want to be hypocritical, then
you should be qualifying even remarks such as "The Sun rose here today"
as "a guess",

When we start discussing astronomy here, I'll be saying exactly that.
Along with other, more pithy, remarks.

Next.
and you should be qualifying as a "guess" the very
existance of the newsgroups you redirect people to.

idiot.
 
K

Keith Thompson

Mark McIntyre said:
On 28 Mar 2005 22:29:56 GMT, in comp.lang.c ,
(e-mail address removed)-cnrc.gc.ca (Walter Roberson) wrote: [...]
When you answer enough Usenet questions, you develop a good sense of
Read Programmer's Mind. "guess" doesn't come into it.

Axiomatically, if answering an offtopic question in CLC, you're
guessing.

There is no such axiom. Perhaps the word "guessing" doesn't mean what
you think it means.

Mark, I'm afraid you're being ridiculous. The subject header is "How
do I wait for the child process ?", and the posted code clearly used
POSIX-specific functions and terminology (childpid, wait(&status),
etc.). Anyone familiar with POSIX and/or Unix-like systems does not
need to "guess" to know what's being talked about.

Both the question and the answer happen to be off-topic.
 
M

Mark McIntyre

There is no such axiom. Perhaps the word "guessing" doesn't mean what
you think it means.

I assure you it does!
Mark, I'm afraid you're being ridiculous.

I agree completely. On the other hand, Walter is being a right royal
prat. In my original post, I merely noted that he should perhaps have
redirected the OP to the right group. I did however use sarcasm, and I
should have remembered that few outside Europe understand it.
The subject header is "How
do I wait for the child process ?", and the posted code clearly used
POSIX-specific functions and terminology (childpid, wait(&status),
etc.). Anyone familiar with POSIX and/or Unix-like systems does not
need to "guess" to know what's being talked about.

Thats true but wholly irrelevant. My point, which you understand
perfectly, is that Walter's answer could quite easily have been a
complete guess, and nobody in CLC could have properly called him on it
as Posix is offtopic here.
Both the question and the answer happen to be off-topic.

Precisely.
 
A

Alan Balmer

POSIX 1003.1-1990, section 3.2.1.2

More to the point - you don't know if the OP's system is POSIX, or
even Unix, for that matter. Other systems have child processes, and
even wait() functions. In any case, whether your assumptions are
correct or not, the OP should have been directed to a more appropriate
forum ("Go to a group appropriate for your system. if it's a Unix
system, try comp.unix.programmer") for one or more peer-reviewed
answers. You can pop over there and answer it for him, if you want,
and he would have the comfort of knowing that someone else would
correct you if you were wrong, and the advantage of having more than
one viewpoint.
 
W

Walter Roberson

On the other hand, Walter is being a right royal
prat.

To be consistant, you should be saying that you -guess- that I'm
being a right royal prat.
In my original post, I merely noted that he should perhaps have
redirected the OP to the right group. I did however use sarcasm, and I
should have remembered that few outside Europe understand it.

What is this Europe you speak of? I do not find "Europe" indexed
anywhere in X3.159-1989, and for all I know you are making up it's
existance.
Thats true but wholly irrelevant. My point, which you understand
perfectly, is that Walter's answer could quite easily have been a
complete guess, and nobody in CLC could have properly called him on it
as Posix is offtopic here.

Is that "could have" in the sense of "not allowed to", or in the
sense of "would not have the knowledge in order to do so" ?
 
A

Alan Balmer

he posted code clearly used
POSIX-specific functions and terminology (childpid, wait(&status),
etc.).

?
I've used a couple of non-Unix systems that used the "pid"
terminology. "Process identifier" is a fairly obvious name, when it
doesn't mean proportional, integral, derivative. And I've used DOS
compilers which provided a wait() function.

Heck, there are lots of OS's which I don't know anything about, which
the OP might be using.

Odds are high that the OP was working with a Unix system, but there's
no need to make that assumption, since he probably knows.
 
W

Walter Roberson

So what? Posix isn't part of C, and for all I know, you made up this
reference - after all, anyone can type in some random text.

Well, I don't know, Mark -- what are your standards of proof for
matters that are not specified in one of the C standards?

I could fax you a copy of the page, but you wouldn't know whether I had
it specially printed out and was just making the whole thing up.

I could point you to online man pages, but you wouldn't know whether I
had secretly bought out control of those domains and changed the text,
and you wouldn't know whether I had perhaps started conspiring years
ago to slip in misinformation about wait() into references.

I could, for all you know, be a member of the equivilent of the
Illuminati, except dedicated to perpetuating the misinformation that
wait() is unconditional. I could have secret control over the various
opensource implimentations of wait(), and have, as in "Reflections on
Trust", perverted gcc and all other known compilers and "dists" so that
the source could would make it -appear- that wait() was unconditional
but in actuallity make it sometimes unconditional.

You never know, I could be a one-man army, sworn to a secret order
of extra-terrestrials, and publically stating basic falsehoods
about wait() could be part of a holistic plan to bring down
civilization, myself driven to join by my addiction to Borganzola cheese.

So it's probably better that you don't trust anything I say. I guess.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top