From owner-freebsd-questions Wed Sep 9 18:40:20 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id SAA28966 for freebsd-questions-outgoing; Wed, 9 Sep 1998 18:40:20 -0700 (PDT) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from awfulhak.org (awfulhak.force9.co.uk [195.166.136.63]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id SAA28904 for ; Wed, 9 Sep 1998 18:40:12 -0700 (PDT) (envelope-from brian@Awfulhak.org) Received: from woof.lan.awfulhak.org (root@woof.lan.awfulhak.org [172.16.0.7]) by awfulhak.org (8.8.8/8.8.8) with ESMTP id CAA28814; Thu, 10 Sep 1998 02:37:29 +0100 (BST) (envelope-from brian@Awfulhak.org) Received: from woof.lan.awfulhak.org (brian@localhost [127.0.0.1]) by woof.lan.awfulhak.org (8.9.1/8.9.1) with ESMTP id CAA05582; Thu, 10 Sep 1998 02:36:02 +0100 (BST) (envelope-from brian@woof.lan.awfulhak.org) Message-Id: <199809100136.CAA05582@woof.lan.awfulhak.org> X-Mailer: exmh version 2.0.2 2/24/98 To: Mark Ovens cc: questions@FreeBSD.ORG, freebsd-users@freebsd-uk.eu.org Subject: Re: Help needed with fork(), pipe() & dup2() In-reply-to: Your message of "Wed, 09 Sep 1998 21:08:08 -0000." <35F6EE38.7DC97F21@uk.radan.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Thu, 10 Sep 1998 02:36:01 +0100 From: Brian Somers Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG [.....] > void foo() > { > char line [MAX_LINE]; > int fd [2]; > pid_t pid; > > pipe(fd); > if ((pid = fork ()) < 0) > error_message(); > else > if (pid > 0) > { /* parent */ > close (fd [1]); > if (fd [0] != STDIN_FILENO) > { > dup2(fd [0], STDIN_FILENO) > close (fd [0]); > } > /* read file names from pipe */ > 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. > } /* 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 ? -- Brian , , 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