title: read() write() into pipe() via dup2() with stdin and stdout
body: <p>I need to simulate the Linux command "<code>cal -3</code>", which displays the calendar for 3 months side by side. What I need right now is to get my implementation, using pipes, working. I've been told that I can't use <code>fork()</code>, but rather I should use <code>dup2()</code>, <code>write()</code>, <code>read()</code> and <code>close()</code> to call <code>system("myCustomCommand")</code> three times. Right now my program does not display the calendar side by side. </p>
  
  <p>I am trying to use pipes and ran into a problem. Here is what I am trying: </p>
  
  <pre><code>int pfd[2];
  int p; //for pipe
  int d; //for dup2
  const int BSIZE = 256;
  char buf[BSIZE];
  
  p = pipe(pfd);
  if (p == -1) { perror("pipe"); exit(EXIT_FAILURE); }
  if (p == 0)
  {
      d = dup2(pfd[1], 0);
      close(pfd[1]);
      nbytes = read (pfd[1], buf , BSIZE);
      close(pfd[0]);
      exit(EXIT_SUCCESS);
  }
  else
  {
      close(pfd[0]);
      write(pfd[1], "test\n", BSIZE);
      close(pfd[1]);
      exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>Unfortunately, this code does not display anything. Could you please help me out with this?</p>