Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Sep 1998 02:36:01 +0100
From:      Brian Somers <brian@Awfulhak.org>
To:        Mark Ovens <marko@uk.radan.com>
Cc:        questions@FreeBSD.ORG, freebsd-users@freebsd-uk.eu.org
Subject:   Re: Help needed with fork(), pipe() & dup2() 
Message-ID:  <199809100136.CAA05582@woof.lan.awfulhak.org>
In-Reply-To: Your message of "Wed, 09 Sep 1998 21:08:08 -0000." <35F6EE38.7DC97F21@uk.radan.com> 

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