[LCP]abt IPC

Bill Rausch bill at numerical.com
Wed Aug 29 03:00:04 UTC 2001


At 12:33 PM +0530 8/25/01, $rinath wrote:
>         if((childpid = fork()) == -1)
>         {
>                 perror("fork");
>                 exit(1);
>         }
>
>         if(childpid == 0)
>         {
>                 /* Child process closes up input side of pipe */
>                 close(fd[0]);
>
>                 /* Send "string" through the output side of pipe */
>                 write(fd[1], string, (strlen(string)+1));
>                 exit(0);
>         }
>         else
>         {
>                 /* Parent process closes up output side of pipe */
>                 close(fd[1]);
>
>                 /* Read in a string from the pipe */
>                 nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
>                 printf("Received string: %s", readbuffer);
>         }
>        
>         return(0);
>}
>
>i am just learning pipes & IPC.. see the code above
>if fork is successful, childpid is zero and only the code under if 
>should execute.
>in the above if-else loop, the code under if and else both seem to 
>execute. what
>is the flow here ??

They do both execute, just that part is in one process and part is in 
another.  At the moment that the fork completes, you have two 
programs running. It doesn't matter which one executes first in your 
program (and that is non-deterministic so it is good that it doesn't 
matter) because when the parent hits the read() on the pipe it will 
stop and wait until there is something to read or the other end of 
the pipe gets closed. Once the child has written something then the 
read() can continue.

-- 
  Bill Rausch, Software Development, Unix, Mac, Windows
  Numerical Applications, Inc.  509-943-0861   bill at numerical.com



More information about the linuxCprogramming mailing list