Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 11 Sep 1998 00:15:59 +0100
From:      Mark Ovens <marko@uk.radan.com>
To:        Leo Papandreou <leo@talcom.net>
Cc:        questions@FreeBSD.ORG, freebsd-users@freebsd-uk.eu.org
Subject:   Re: Help needed with fork(), pipe() & dup2()
Message-ID:  <35F85DAF.2B23FC42@uk.radan.com>
References:  <35F6EE38.7DC97F21@uk.radan.com> <19980909182820.16714@talcom.net> <35F77884.E4FEAA21@uk.radan.com> <19980910173817.42597@talcom.net>

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?35F85DAF.2B23FC42>