title: How to fork() n child processes doing diferrent functions?
body: <p>This is my code:</p>
  
  <pre><code>for (c = 0; c &lt; PROCESSES; c++) {
      pid[c] = fork();
      switch (pid[c]) {
          case -1:
              perror("Faild fork!\n");
              break;
          case 0:
              printf("C = %d\n", c);
              if (c == 0) {
                  printf("MY ID0 %d\n", getpid());
                  customers();
                  break;
              }
              if (c == 1) {
                  printf("MY ID1 %d\n", getpid());
                  cashier();
                  break;
              }
              if (c == 2) {
                  printf("MY ID2 %d\n", getpid());
                  sales();
                  break;
              }
              if (c == 3) {
                  printf("MY ID3 %d\n", getpid());
                  warehouse();
                  break;
              }
              break;
  
          default:
              if (c == (PROCESSES - 1)) {
  
                  for (j = 0; j &lt; PROCESSES; j++) {
                      w = wait(&amp;state);
                      if (w == -1) {
                          perror("Erro na espera!\n");
                      }
                      printf("Terminar processo %d\n", w);
                  }
                  sleep(2);
                  printf("Fim da simulação.\n\n");
                  free_shm_sem();
              }
  
      }
  }
  </code></pre>
  
  <p>What I was expecting to happen:</p>
  
  <pre><code>C = 0
  MY ID0 3904
  C = 1
  MY ID1 3905
  C = 2
  MY ID2 3906
  C = 3
  MY ID3 3907
  </code></pre>
  
  <p>what happened:</p>
  
  <pre><code>C = 0
  MY ID0 3904
  C = 1
  MY ID1 3905
  C = 2
  MY ID2 3906
  C = 3
  MY ID3 3907
  C = 3
  MY ID3 3911
  </code></pre>
  
  <p>If I change to:</p>
  
  <pre><code>            if (c == 0) {
                  printf("MY ID0 %d\n", getpid());
                  customers();
                  break;
              }
              if (c == 1) {
                  printf("MY ID1 %d\n", getpid());
                  sales();
                  break;
              }
              if (c == 2) {
                  printf("MY ID2 %d\n", getpid());
                  cashier();
                  break;
              }
              if (c == 3) {
                  printf("MY ID3 %d\n", getpid());
                  warehouse();
                  break;
              }
  </code></pre>
  
  <p>The output became:</p>
  
  <pre><code>C = 0
  C = 2
  C = 1
  MY ID0 3960
  MY ID2 3962
  C = 3
  MY ID1 3961
  MY ID3 3963
  C = 2
  MY ID2 3967
  C = 3
  MY ID3 3968
  </code></pre>
  
  <p>Why c changes its value and some times passes through the same place twice?</p>
  
  <p>This isnt the right way of fork() n childs doing diferent processes?</p>
  
  <p>P.S. Sorry my English is bad. I hope you can understand what I said.</p>