title: Wrong printf in fork
body: <p>I apologize yet for my english and I hope you'll be able to understand.
  So I have this problem with a printf in my program which wait the end of father and some before printf. So I have always got the father who printf all his message and then the son printf all his message whereas it should be crossed..</p>
  
  <p>From what I saw, I read that I have to had fflush but it's not solve my problem.
  Thx for your help.
  Here is my code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/sem.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/shm.h&gt;
  
  int cpt = 0;
  
  void fils(int *cpt){
    int i;
    for(i = 0; i &lt; 3; i++){
      (*cpt)++;
      printf("son : %d\n", *cpt);
    }
  }
  
  void pere(int *cpt){
    int i;
    for(i = 0; i &lt; 3; i++){
      (*cpt)--;
      printf("father : %d\n", *cpt);
      fflush(stdout);
    }
  }
  
  int main (int argc, char*argv[]){
    key_t key;
    int memShared;
    int *cpt = NULL;
  
    key = ftok(argv[0],'p');
    memShared = shmget(key, sizeof(int), IPC_CREAT|IPC_EXCL|0666);
    cpt = shmat ( memShared, cpt, 0);
    *cpt = 0;
  
    fflush(stdout);
    switch (fork()){
      case -1: perror("erreur de création du fils");
           exit(99);
      case  0: fils(cpt);
           exit(1);
      default: pere(cpt);
           wait(NULL);
    }
     shmctl(memShared, IPC_RMID, NULL);
  
    return 0;
  }
  </code></pre>
  
  <p>and i always have this in the terminal</p>
  
  <p>father : -1
  father : -1
  father : -2
  son : 0
  son : -1
  son : 0</p>
  
  <p>i though it should be crossed for example father : -1 son : 0 father : -1 ...
  thanks for your help.</p>