Program hanging when read()ing from pipe
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 > Program hanging when read()ing from pipe




Pages (3): [1] 2 3 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Program hanging when read()ing from pipe  
Julien


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


 
02-04-06 10:52 PM

Hello,

I'm writing a program in C which uses a pipe()/fork()/exec() mechanism
to execute a program as a child and make it communicate with the parent
process. At some point, the child process is supposed to stop but
read()
doesn't seem to get an EOF and the parent hangs.
'ps' reports that the child process is defunct.
If I execute the child from a terminal and do exactly the same things
as
the master process does, it exits normally.
What can I do to get things to work properly ?

Thanks in advance for your help.






[ Post a follow-up to this message ]



    Re: Program hanging when read()ing from pipe  
Nils O. Selåsdal


Report This Message To A Moderator Edit/Delete Message


 
02-04-06 10:52 PM

Julien wrote:
> Hello,
>
> I'm writing a program in C which uses a pipe()/fork()/exec() mechanism
> to execute a program as a child and make it communicate with the parent
> process. At some point, the child process is supposed to stop but
> read()
> doesn't seem to get an EOF and the parent hangs.
> 'ps' reports that the child process is defunct.
Oh - means it exited. You need call wait/waitpid/or similar
on the child (do it in a signal handler for SIGCHLD). Processes
stick around untill you call wait on them
> If I execute the child from a terminal and do exactly the same things
> as
>  the master process does, it exits normally.
> What can I do to get things to work properly ?
>
> Thanks in advance for your help.
Did you remember to close the writing end (or atleast the one you don't
use) in the child, and close the reading end in the parent ?





[ Post a follow-up to this message ]



    Re: Program hanging when read()ing from pipe  
Ian Collins


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


 
02-04-06 10:52 PM

Julien wrote:
> Hello,
>
> I'm writing a program in C which uses a pipe()/fork()/exec() mechanism
> to execute a program as a child and make it communicate with the parent
> process. At some point, the child process is supposed to stop but
> read()
> doesn't seem to get an EOF and the parent hangs.
> 'ps' reports that the child process is defunct.

Which order are things supposed to die?  If the child is defunct, the
parent must have terminated.


--
Ian Collins.





[ Post a follow-up to this message ]



    Re: Program hanging when read()ing from pipe  
Barry Margolin


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


 
02-04-06 10:52 PM

In article <1139090292.579903@drone2-svc-skyt.qsi.net.nz>,
Ian Collins <ian-news@hotmail.com> wrote:

> Julien wrote: 
>
> Which order are things supposed to die?  If the child is defunct, the
> parent must have terminated.

No, it's the other way around.  Defunct means the child has died, but
the parent hasn't yet called one of the wait() functions to get its
termination status.

--
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: Program hanging when read()ing from pipe  
Ian Collins


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


 
02-04-06 10:52 PM

Barry Margolin wrote: 
>
>
> No, it's the other way around.  Defunct means the child has died, but
> the parent hasn't yet called one of the wait() functions to get its
> termination status.
>
Doesn't this also happen if the parent has terminated before the child
and can't call one of the wait() functions, or does init clean up?

--
Ian Collins.





[ Post a follow-up to this message ]



    Re: Program hanging when read()ing from pipe  
Barry Margolin


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


 
02-05-06 01:47 AM

In article <1139094474.89209@drone2-svc-skyt.qsi.net.nz>,
Ian Collins <ian-news@hotmail.com> wrote:

> Barry Margolin wrote: 
> Doesn't this also happen if the parent has terminated before the child
> and can't call one of the wait() functions, or does init clean up?

When the parent terminates first, init becomes the parent.  Then the
definition becomes the same -- when the child terminates, it will become
a zombie for the brief period until init calls wait().

--
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: Program hanging when read()ing from pipe  
Michael Paoli


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


 
02-05-06 07:47 AM

Julien wrote:
> 'ps' reports that the child process is defunct.
http://groups.google.com/group/comp...br />
ce&hl=en
Message-ID: <d85eb83f.0312180842.21c629d3@posting.google.com>
ps(1)
wait(2)
etc.






[ Post a follow-up to this message ]



    Re: Program hanging when read()ing from pipe  
Julien


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


 
02-05-06 10:06 PM

Thanks for the answers.

[vbcol=seagreen] 


Yes, and I can read and write through the pipes. I actually get what I
expect from the external program that is launched as child.

I did some rewriting and the child process now ends normally (not
defunct anymore) ; waitpid returns the child process' pid.
The while(r=read(...)){...} loop still hangs however because there is
nothing to read in the pipe's buffer. Why doesn't it get EOF ?

I don't know if that has something to do with the problem, but before
terminating, the child spawns another process, which runs in the
background and has init as parent.






[ Post a follow-up to this message ]



    Re: Program hanging when read()ing from pipe  
Mr. Uh Clem


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


 
02-05-06 10:06 PM

Julien wrote:
> Thanks for the answers.
>
> 
>
>
> Yes, and I can read and write through the pipes. I actually get what I
> expect from the external program that is launched as child.
>
> I did some rewriting and the child process now ends normally (not
> defunct anymore) ; waitpid returns the child process' pid.
> The while(r=read(...)){...} loop still hangs however because there is
> nothing to read in the pipe's buffer. Why doesn't it get EOF ?
>
> I don't know if that has something to do with the problem, but before
> terminating, the child spawns another process, which runs in the
> background and has init as parent.
>

If the grand child inherits a copy of the pipe to the parent,
the pipe will still have an end open when the child exits, so
the parent will not see EOF.   (You can have this same sort of
problem if a parent is spawning multiple children.  A subsequent
child can inherit the parent's end of a pipe to a previous
child, making it impossible for the parent to close() the
pipe to the previous child and have it get EOF.)

--
Clem
"If you push something hard enough, it will fall over."
- Fudd's first law of opposition





[ Post a follow-up to this message ]



    Re: Program hanging when read()ing from pipe  
Julien


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


 
02-06-06 01:04 PM

> If the grand child inherits a copy of the pipe to the parent,
> the pipe will still have an end open when the child exits, so
> the parent will not see EOF.   (You can have this same sort of
> problem if a parent is spawning multiple children.  A subsequent
> child can inherit the parent's end of a pipe to a previous
> child, making it impossible for the parent to close() the
> pipe to the previous child and have it get EOF.)

Is there anyway to check that ? It would be strange since the grand
child is supposed to be in the background, so I guess it doesn't need
any stdio.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:18 PM.      Post New Thread    Post A Reply      
Pages (3): [1] 2 3 »   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