[comp.unix.questions] parent & child reading stdin

marlon@daisy.UUCP (Marlon Pearson) (05/02/90)

:In article <46@daisy.UUCP> marlon@daisy.UUCP (Marlon Pearson) writes:
:: My understanding is that you can't guarantee who gets which bytes when
:: you have two processes reading from the stdin at the same time. Is it a 
:: bad idea if I have a parent process that will read the first N bytes and then
:: fork and exec a child process and let it read the rest of stdin? I have tried
:: this with out doing any dups or pipes or such for passing bytes from the
:: parent to the child. The child inherits the stdin from its parent and reads
:: it fine. As I said, it seems to work and makes sense but is it the "correct"
:: way of doing it.

:It better be correct, since the shells do that all day long.

:The distinction to be made is that, since "correct" programs are using the
:fork as a synchronization point, they are NOT, in fact, "two processes
:reading from stdin at the same time."

:Though I don't know what you mean by your antepenultimate sentence.  In
:what way are you using stdin to pass bytes from the parent to the child?
:That doesn't fit in with the rest of what you are asking.

:Larry Wall

Larry,

Thanks for the response.
As to the third from the end sentence. I understand that it is a fairly common
practice when passing info between a parent and child to set up a pipe or pipes
where one process writes to the pipe and the other reads from it. So, I thought
this may be an alternative to what I current have working. The parent would read
from stdin and after it had consumed the first N bytes it would simply pass the
remaining byte stream to the child via writing to the pipe. The child would read
from the pipe instead of stdin.

Marlon


-- 
===========================================
Marlon
{nsc,atari,pyramid,uunet}!daisy!marlon

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (05/04/90)

In article <48@daisy.UUCP> marlon@daisy.com (Marlon Pearson) writes:
: As to the third from the end sentence. I understand that it is a fairly
: common practice when passing info between a parent and child to set up
: a pipe or pipes where one process writes to the pipe and the other
: reads from it. So, I thought this may be an alternative to what I
: current have working. The parent would read from stdin and after it had
: consumed the first N bytes it would simply pass the remaining byte
: stream to the child via writing to the pipe. The child would read from
: the pipe instead of stdin.

That would only be possible if the stdin was already connected to a pipe and,
somehow, the parent had access to the write end of the stdin pipe, which is
generally not the case.  Pipes are two ended and directional, and stdin
will be connected only to the read end.  Writing on stdin is normally frowned
upon.

To do what you want you'll have to close stdin and set up a pipe of your own
with the read end on descriptor 0.  After you fork, that'll be the child's
stdin.  The parent hangs on to the other end of the pipe, which has its own
descriptor, and writes to that end.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov