title: passing command line arguments to a child process and count them
body: <p>I want the parent process to take the arguments to main() and send the characters in them one at a time to the child process through a pipe starting with argv[1] and continue through the rest of the arguments.(one call to write for each character).</p>
  
  <p>I want the child process to count the characters sent to it by the parent process and print out the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever.</p>
  
  <p>What am i doing wrong? do i need to use exec()?</p>
  
  <p>output that isnt correct:</p>
  
  <pre><code>    ~ $ gc a03
  gcc -Wall -g a03.c -o a03
  ~ $ ./a03 abcd ef ghi
  
  child: counted 12 characters
  ~ $
  </code></pre>
  
  <p>here is the program..</p>
  
  <pre><code>#include &lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char *argv[])
  {
  
      int length = 0;
      int i, count;
  
      int     fdest[2];          // for pipe
      pid_t   pid;              //process IDs
      char    buffer[BUFSIZ];
  
  
  
      if (pipe(fdest) &lt; 0)          /* attempt to create pipe */
          printf("pipe error");
  
      if ((pid = fork()) &lt; 0)  /* attempt to create child / parent process */
  
      {
          printf("fork error");
      } 
  
  
      /* parent process */
      else if (pid &gt; 0) {      
          close(fdest[0]);
  
          for(i = 1; i &lt; argc; i++)    /* write to pipe */
          {
              write(fdest[1], argv[i], strlen(argv[1]));
          }
  
          wait(0);
  
      } else {   
  
          /* child Process */
          close(fdest[1]);
  
          for(i = 0; i &lt; argc; i++)
          {
              length +=( strlen(argv[i])); /* get length of arguments */
          }
  
          count = read(fdest[0], buffer, length);
          printf("\nchild: counted %d characters\n", count);
  
      }
  
      exit(0);
  
  }
  </code></pre>