title: Multiple child processes with pipe
body: <p>I wrote a C program that is supposed to create a certain number of child processes, each child process having to change 1 letter from a string. The string and the number of child processes are read from the keyboard.</p>
  
  <p>For example: </p>
  
  <p>input: </p>
  
  <pre><code>3 Apples  
  </code></pre>
  
  <p>output: </p>
  
  <pre><code>Applex Appldx Apqldx
  </code></pre>
  
  <p>My problem is: Only the parent changes a letter and the children don't.
  Unsure what I'm doing wrong. Help would be much appreciated, thanks in advance!</p>
  
  <p>EDIT:
  I feel i wasn't clear enough. I want to do it using pipes.
  It should work like this: The parent changes one letter, then the first child takes the string modified by the parent and changes one more letter.
  The second child takes the string modified by the first one (2 letters are already changed) and changes one more and so on. I am new to C and am not quite sure how it all works, especially pipes.
  Also can the children be linked between them through the pipe, or can the only linked to the parent and it has to be something like: first child changes a letter, gives the string back to the parent and then the second child reads from there, modifies letter and gives back.
  If it's like that, is there any way to make sure that this doesn't happen: Apples becomes AppleD and then AppleX and then AppleQ?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  
  
  void error(char* msg)
  {
  fprintf(stderr, "%s\n", msg);
  exit(1);
  }
  
  static void modify(char msg[]) {
      srand(time(NULL));
      int pos1=rand()%((int)strlen(msg));
      srand(time(NULL));
      int pos2=rand()%26;
      srand(time(NULL));
      int big=rand()%2;
      if(big==1) {
          msg[pos1]=(char)(((int)'A')+pos2);
      }
      else {
          msg[pos1]=(char)(((int)'a')+pos2);
      }
      return;
  }
  
  int main(int argc, char *argv[])
  {
  if(argc!=3) {
      error("Wrong number of arguments\n");
  }
  int nrch;
  nrch=atoi(argv[1]);
  char* msg=argv[2];
  
  printf("Parent: erhalten: %s\n", msg);
  int i=0;
  modify(argv[2]);
  printf("Parent: weiter: %s\n", msg);
  srand(time(NULL));
  pid_t pids[10];
  int fd[2];
  
  /* Start children. */
  for (i = 0; i &lt; nrch; ++i) {
      if (pipe(fd) == -1) {
          error("Canâ€™t create the pipe");
      }
      if ((pids[i] = fork()) &lt; 0) {
              error("Can't fork process");
      } 
      else if (pids[i] == 0) {
          //dup2(fd[1], 1);
          //close(fd[0]);
          printf("child%d: erhalten: %s\n", (i+1), msg);
              modify(msg);
          printf("child%d: weiter: %s\n", (i+1), msg);
              exit(0);
      }
  }
  
  /* Wait for children to exit. */
  int status;
  pid_t pid;
  while (nrch &gt; 0) {
      pid = wait(&amp;status);
      printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
      --nrch; 
  }   
  
  }
  </code></pre>