Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 11 Sep 1998 01:54:52 +0100
From:      Brian Somers <brian@Awfulhak.org>
To:        Mark Ovens <marko@uk.radan.com>
Cc:        Brian Somers <brian@Awfulhak.org>, questions@FreeBSD.ORG, freebsd-users@freebsd-uk.eu.org
Subject:   Re: Help needed with fork(), pipe() & dup2() 
Message-ID:  <199809110054.BAA05212@woof.lan.awfulhak.org>
In-Reply-To: Your message of "Thu, 10 Sep 1998 07:50:18 BST." <35F776AA.9A256020@uk.radan.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
[.....]
> 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.

No.  The gets() will block until either the other end of the pipe is 
close()d (at which point 0 is returned from the underlying read()) or 
until the other end of the pipe is written to.

The parent should not be mucking around with STDIN and gets() at all.
In my experience, way to many library calls use unexpected routines 
like err() and abort().  Besides, gets() produces warnings for a 
reason.

Instead, change the parent if() block to

  FILE *in;
  int len;

  close(fd[1]);
  if ((in = fdopen(fd[0], "r")) == NULL) {
     close(fd[0]);   /* child gets SIGPIPE */
     vomit();
  }
  line[sizoef line - 1] = '\0';
  while (fgets(line, sizeof line - 1, in)) {
    len = strlen(line);
    if (len && line[len-1] == '\n')
      line[--len] = '\0';
    .
    .
    .
  }
  fclose(in);
  close(fd[0]);

> -- 
>   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

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



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?199809110054.BAA05212>