title: two processes printf overlapping in one terminal in c
body: <p>I need to write a program that create process multithreaded which is creating another process multithreaded and the problem that I having is there are some point time in the running of the two processes there are overlapping each other in printing to terminal.</p>
  <p>for example :</p>
  <pre><code>//father process
  printf(&quot;I'm father process\n&quot;);
  
  //child process
  fprintf(stderr,&quot;I'm child process\n&quot;);
  </code></pre>
  <p>the outcome  :</p>
  <pre><code>I'I'mm fachtheril proprcesscoess
  </code></pre>
  <p>is there anyway I can make sure that will not happen ?</p>
  <p>the father and the child processes are connected with anonymous pipe if it's help.</p>
  <hr />
  <p>add editing :</p>
  <p>my main thread in the father process creating the vessels (threads) that each one have it's own id and announce this vessel is starting to work.
  then the thread itself need to use the pipe and sent his id to the child process. when the child process gets the id though the pipe he announce that the id arrived. but before that the child process initialize other thread that called crane with different purpose that also announce when they are created.</p>
  <p>*each thread has it's own semaphore.
  *after each announce the thread enter to sleep between 5-3000 milliseconds.</p>
  <p>so the problem it's happing only in the beginning when the main thread of the father process start creating the vessel and the main thread in the child process creating the cranes, there printing is overlapping or start one sentence stop start another one and return to the first one. though all the printing have a /n and command in one line.</p>
  <p>I will try to add  minimal reproducible example of my code:</p>
  <p>father process:</p>
  <p>main thread:</p>
  <pre><code>    /* create Input the pipe  */
  if (!CreatePipe(&amp;InputReadHandle, &amp;InputWriteHandle, &amp;sa, 0)) {
      fprintf(stderr, &quot;Main::Create Pipe Failed @ father\n&quot;);
      return 1;
  }
  /* create Output the pipe */
  if (!CreatePipe(&amp;OutputReadHandle, &amp;OutputWriteHandle, &amp;sa, 0)) {
      fprintf(stderr, &quot;Main::Create Pipe Failed @ father\n&quot;);
      return 1;
  }
  
  /* establish the START_INFO structure for the child process */
  GetStartupInfo(&amp;si);
  si.hStdError = GetStdHandle(STD_OUTPUT_HANDLE);
  
  /* redirect the standard input/output to the read end of the pipe */
  si.hStdOutput = OutputWriteHandle;
  si.hStdInput = InputReadHandle;
  si.dwFlags = STARTF_USESTDHANDLES;
  
  /* we do not want the child to inherit the write end of the pipe */
  SetHandleInformation(InputWriteHandle, HANDLE_FLAG_INHERIT, 0);
  
  
  wcscpy(ProcessName, L&quot;..\\..\\child\\Debug\\child.exe&quot;);
  
  
  /* create the child process */
  if (!CreateProcess(NULL,
      ProcessName,
      NULL,
      NULL,
      TRUE, /* inherit handles */
      0,
      NULL,
      NULL,
      &amp;si,
      &amp;pi))
  {
      fprintf(stderr, &quot;Main::Process Creation Failed @ father\n&quot;);
      return -1;
  }
  
  
  /* father now wants to write to the pipe */
  if (!WriteFile(InputWriteHandle, &amp;numOfVessels, BUFFER_SIZE, &amp;written, NULL))
      fprintf(stderr, &quot;Main::Error writing to pipe\n&quot;);
  </code></pre>
  <p>.
  .
  .</p>
  <pre><code>// Create all vessel Threads. Report if Error occurred!
  for (int i = 0; i &lt; numOfVessels; i++)
  {
      vesselsID[i] = i+1;
      vesselsSem[i] = CreateSemaphore(NULL, 0, 1, NULL);
      if (vesselsSem[i] == NULL)
      {
          fprintf(stderr, &quot;Main::Unexpected Error in Vessles Semaphore %d Creation\n&quot;, i);
          return FALSE;
      }
      vesselsArr[i] = CreateThread(NULL, 0, Vessel, &amp;vesselsID[i], 0, &amp;ThreadId);
      if (vesselsArr[i] == NULL) {
          fprintf(stderr,&quot;Main::Unexpected Error in Thread %d Creation\n&quot;, i);
          exit(1);
      }
  }
  
  //wait to all thread(vessel) to finish.
  WaitForMultipleObjects(numOfVessels, vesselsArr, TRUE, INFINITE);
  </code></pre>
  <p>vessel thread :</p>
  <pre><code>DWORD WINAPI Vessel(PVOID Param)
  {
  int id = *(int*)Param;
  
  printf(&quot;%s Vessel %d - starts sailing @ father\n&quot;, getTime(),id);
  
  Sleep(random());//Simulate a process Sailing
  
  //sent to child .
      // make sure only one vessel at the time enter to the canal.
  WaitForSingleObject(mutex, INFINITE);
  
  printf( &quot;%s Vessel %d - senting to child @father\n&quot;, getTime(), id);
  
  Sleep(random());//Simulate a process  
  
  //send the id vessel to child port through pipe.
  if (!WriteFile(InputWriteHandle, &amp;id, BUFFER_SIZE, &amp;written, NULL))
      fprintf(stderr, &quot;Error writing to pipe @ father\n&quot;);
  
  // the vessel has been sent and can be release .
  if (!ReleaseMutex(mutex))
  {
      fprintf(stderr, &quot; Unexpected error mutex.V()\n&quot;);
  }
  }
  </code></pre>
  <p>child process:
  main thread:</p>
  <pre><code>    for (int i = 0; i &lt; numOfCrane; i++)
  {
      adtArr[i].craneID = i + 1;
  
      craneSem[i] = CreateSemaphore(NULL, 0, 1, NULL);
      if (craneSem[i] == NULL)
      {
          fprintf(stderr, &quot;Main::Unexpected Error in Vessles Semaphore %d Creation @child\n&quot;, i);
          return FALSE;
      }
  
  
      craneArr[i] = CreateThread(NULL, 0, Crane, &amp;adtArr[i].craneID, 0, &amp;ThreadId);
      if (craneArr[i] == NULL) {
          fprintf(stderr, &quot;main::Unexpected Error in Thread %d Creation @child \n&quot;, i);
          exit(1);
      }
      adtArr[i].cargo = 0;
      adtArr[i].vesselID = 0;
  
      
      fprintf(stderr, &quot;%s Crane %d created @child  \n&quot;, getTime(), adtArr[i].craneID);
  }
  </code></pre>
  <p>.
  .
  .</p>
  <pre><code>//read the vessel from pipe
  for (int i = 0; i &lt; numOfVessels; i++)
      {   
          //if readfile is empty then it's wait.
          if (ReadFile(ReadHandle, buffer, BUFFER_SIZE, &amp;read, NULL))
          {
              vesselsID[(*buffer) - 1] = (*buffer);
              vesselsSem[(*buffer) - 1] = CreateSemaphore(NULL, 0, 1, NULL);
              if (vesselsSem[(*buffer) - 1] == NULL)
              {
                  fprintf(stderr, &quot;Main::Unexpected Error in Vessles Semaphore %d Creation\n&quot;, (*buffer));
                  return FALSE;
              }
              vesselsArr[(*buffer) - 1] = CreateThread(NULL, 0, Vessel, &amp;vesselsID[(*buffer) - 1], 0, &amp;ThreadId);
              if (vesselsArr[(*buffer) - 1] == NULL) {
                  fprintf(stderr, &quot;main::Unexpected Error in Thread %d Creation \n&quot;, (*buffer));
                  exit(1);
              }
              barrier[i] = (*buffer); // need to write abinormal behavier
  
              
              fprintf(stderr, &quot;%s Vessel %d - arrirved @ child \n&quot;, getTime(), *buffer);
              Sleep(random());
          }
      }
  </code></pre>
  <p>I hope I did well to explain myself.</p>