Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Sep 1998 07:50:18 +0100
From:      Mark Ovens <marko@uk.radan.com>
To:        Brian Somers <brian@Awfulhak.org>
Cc:        questions@FreeBSD.ORG, freebsd-users@freebsd-uk.eu.org
Subject:   Re: Help needed with fork(), pipe() & dup2()
Message-ID:  <35F776AA.9A256020@uk.radan.com>
References:  <199809100136.CAA05582@woof.lan.awfulhak.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Brian Somers wrote:
> 
> [.....]
> >                       while (gets (line) != NULL)
> >                                {
> >                                       .
> >                                       .
> >                               code to process "line" here
> >                                       .
> >                                       .
> >                                }
> >
> >                       close (fd [0]);
>                         ^^^^^^^^^^^^^^^
> Dodgy - fd[0] has already been closed (unless == STDIN_FILENO).
> 
> >                       }  /* parent */
> >               else
> >                       {  /* child */
> >                       close (fd [0]);
> >                       if (fd [1] != STDOUT_FILENO)
> >                               {
> >                               dup2(fd [1], STDOUT_FILENO)
> >                               close (fd [1]);
> >                               }
> >                       if (execvp ("file", files) < 0)
> >                               error_message ();
> >                       close (fd [1]);
> 
> Ditto, but this one (hopefully) doesn't normally happen.  You're also
> missing an exit() here.

I did wonder about that. I tried commenting out the close() calls but
it made no difference. I'll try adding exit()


> 
> >                       }  /* child */
> >       }
> 
> I don't know enough about the rest of the code to tell if that first
> close may be the problem - but it *might*.  Also, are you sure that
> last error_message() isn't being called ?

Definitely. It's an X program and the error_message function pops up a
message window, and that definitely isn't happening.

I got another reply suggesting that the problem is caused by the code
assuming the order in which the parent & child execute, i.e. the
parent reads the pipe (and sees NULL) before the child has written to
it. This seems a very likely possiblity.

> 
> --
> Brian <brian@Awfulhak.org>, <brian@FreeBSD.org>, <brian@OpenBSD.org>
>       <http://www.Awfulhak.org>;
> Don't _EVER_ lose your sense of humour....

-- 
  When everything's coming your way, you're in the wrong lane.

Mark Ovens, CNC Applications Engineer, Radan Computational Ltd
Sheet Metal CAD/CAM Solutions
mailto:marko@uk.radan.com    http://www.radan.com

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?35F776AA.9A256020>