[comp.unix.internals] fork

pen@lysator.liu.se (Peter Eriksson) (10/18/90)

I'm experiencing some strange things with the code segment below. I have to
add the call to usleep() (or use semaphore signalling from the child to the
parent) in order to have the 'fd' available in the child. Apparently something
goes wrong if I close it too fast in the parent. I just don't understand why
this is so. Any suggestions?

    fd = accept(...);
    if (fork())
    {
      /* usleep(100000); */
      close(fd);
      return;
    }

    dup2(fd, 0);
    execl(...);

And, is there any portable way to code a wait in the parent to be sure
that the child has begun executing? (I'm using SysV semaphores right now,
which works real nice, but how common are those? How many versions of
Unix has those in the kernel "by default"?)

--
Peter Eriksson                                              pen@lysator.liu.se
Lysator Computer Club                             ...!uunet!lysator.liu.se!pen
University of Linkoping, Sweden                               "Seize the day!"

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (10/25/90)

In article <364@lysator.liu.se> pen@lysator.liu.se (Peter Eriksson) writes:
>
>
>And, is there any portable way to code a wait in the parent to be sure
>that the child has begun executing? 

Not the world's most efficient solution, but you can open a pipe before
you fork, close both ends in the child, close the write end in the parent 
and then read one byte from the read end. The parent will sleep in the read 
until the child closes the write end of the pipe.






--
Dave Eisen                      	    Home: (415) 323-9757
dkeisen@Gang-of-Four.Stanford.EDU           Office: (415) 967-5644
1447 N. Shoreline Blvd.
Mountain View, CA 94043