I've been looking at this small program which performs an "ls | wc",
trying to work out how the blocking semantics works.

I understand that fork() has resulted in two identical processes being
run simultaneously but I can't work out how the parent process is
being forced to execute before the child.

After the fork(), are there two separate copies of pfd[] ?

/* ls | wc */
#include <stdio.h>
#include <unistd.h>

int main(void) 
{
  int pfd[2];
  int pid;

  pipe(pfd);
  pid=fork();
  if (pid==0) 
  {
    /* child */
    close(pfd[1]);
    dup2(pfd[0],0);
    close(pfd[0]);
    execlp("wc", "wc", (char *) 0 );
  }
  else 
  {
    /* parent */
    close(pfd[0]);
    dup2(pfd[1],1);
    close(pfd[1]);
    execlp("ls", "ls", (char *) 0);
  }
  return(0);
}

Reply via email to