From owner-freebsd-questions Thu Sep 10 16:19:00 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id QAA23272 for freebsd-questions-outgoing; Thu, 10 Sep 1998 16:19:00 -0700 (PDT) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from post.mail.demon.net (post-20.mail.demon.net [194.217.242.27]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id QAA23257 for ; Thu, 10 Sep 1998 16:18:45 -0700 (PDT) (envelope-from marko@uk.radan.com) Received: from [158.152.75.22] (helo=uk.radan.com) by post.mail.demon.net with smtp (Exim 2.02 #1) id 0zHFyy-0003sA-00; Thu, 10 Sep 1998 23:18:33 +0000 Organisation: Radan Computational Ltd., Bath, UK. Phone: +44-1225-320320 Fax: +44-1225-320311 Received: from beavis.uk.radan.com (beavis [193.114.228.122]) by uk.radan.com (8.6.10/8.6.10) with SMTP id AAA00309; Fri, 11 Sep 1998 00:17:57 +0100 Received: from uk.radan.com (rasnt-1) by beavis.uk.radan.com (4.1/SMI-4.1) id AA18262; Fri, 11 Sep 98 00:17:53 BST Message-Id: <35F85DAF.2B23FC42@uk.radan.com> Date: Fri, 11 Sep 1998 00:15:59 +0100 From: Mark Ovens X-Mailer: Mozilla 4.05 [en] (Win95; I) Mime-Version: 1.0 To: Leo Papandreou Cc: questions@FreeBSD.ORG, freebsd-users@freebsd-uk.eu.org Subject: Re: Help needed with fork(), pipe() & dup2() References: <35F6EE38.7DC97F21@uk.radan.com> <19980909182820.16714@talcom.net> <35F77884.E4FEAA21@uk.radan.com> <19980910173817.42597@talcom.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Leo Papandreou wrote: > > > Ah, that makes sense. In order to prove this could I add sleep() or > > usleep() before the while(gets())? Tried it...doesn't work :-( > > I'd try this, yes. > > I dont remember the code you posted but It also just occurred to me > that files() will not work as expected if the close-on-exec flag is > set. close-on-exec flag?? please explain. Perhaps I should explain the context a bit further. The program is a clone of Sun's Open Look file manager and foo() is called everytime you change directory (by double clicking on a dir icon). This means that the pipe is created everytime you change dir, i.e. it isn't persistent for the duration the prog is running. arg 2 in execvp() is a global char* that holds a list of filenames (delimited by \n and starts with "file", the name of the prog, and is terminated by a NULL) that the prog can't identify so it passes them to file(1) to try to id them. What puzzles me is that the Linux binary works OK under FreeBSD (2.2.7) Linux emulation. I'm building it with gcc 2.8.1. I've re-quoted the code below.... Thanks for your help with this Leo. > 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]); > > } /* 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]); > } /* child */ > } > To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message