title: How do I set semaphores properly with my two processes program?
body: <p>How can I properly make the 2nd process wait for the 1st process to write the coefficients to a file?</p>
  
  <p>My task is to synchronize 2 processes using semaphores. The 1st process reads the data, writes to the file, then the 2nd process reads this data and finds the solution, which then writes to the file, and then the 1st process reads this solution from the file and outputs the solution to the console.</p>
  
  <p>/<em>Here is the parent file:</em>/</p>
  
  <pre><code>#include "stdafx.h"                            
  
  const char* FIRST_SEMAPHORE = "Semaphore1";
  const char* SECOND_SEMAPHORE = "Semaphore2";
  
  const char* FIRST_PROCESS = "Lab2_first.exe";
  const char* SECOND_PROCESS = "Lab2_second.exe";
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      STARTUPINFO sInfo;
      PROCESS_INFORMATION pInfo;
  
      CreateSemaphore(NULL, 0, 1, (LPCWSTR) FIRST_SEMAPHORE);      
      CreateSemaphore(NULL, 0, 1, (LPCWSTR) SECOND_SEMAPHORE);
  
      GetStartupInfo(&amp;sInfo);        
  
      if(!CreateProcess(ConvertLPCSTRToLPWSTR(SECOND_PROCESS), NULL, NULL, 
              NULL, FALSE, 0, NULL, NULL, &amp;sInfo, &amp;pInfo))
      {  
          printf("Second process failed: %d\n", GetLastError());
      }
  
      if(!CreateProcess(ConvertLPCSTRToLPWSTR(FIRST_PROCESS), NULL, NULL, 
           NULL, FALSE, 0, NULL, NULL, &amp;sInfo, &amp;pInfo))
      {
          printf("First process failed: %d\n", GetLastError());      
      }
  
  
      return 0;
  }
  </code></pre>
  
  <p>/<em>Here is process 1 (Lab2_first):</em>/</p>
  
  <pre><code>#include "stdafx.h"
  #include "windows.h"
  
  const char* FIRST_SEMAPHORE = "Semaphore1";
  const char* SECOND_SEMAPHORE = "Semaphore2";
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      HANDLE semaphore1, semaphore2;
      semaphore1 = OpenSemaphore(EVENT_ALL_ACCESS, TRUE, 
            ConvertLPCSTRToLPWSTR(FIRST_SEMAPHORE));
      semaphore2 = OpenSemaphore(EVENT_ALL_ACCESS, TRUE, 
              ConvertLPCSTRToLPWSTR(SECOND_SEMAPHORE));
  
      Equation equation;
      printf("Enter coefficients: ");
      scanf("%d %d %d", &amp;equation.a, &amp;equation.b, &amp;equation.c);
  
      HANDLE file = CreateFile(ConvertLPCSTRToLPWSTR("equation"), 
            GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 
               FILE_ATTRIBUTE_NORMAL, 0);
  
      DWORD number = sizeof(equation);
      LPDWORD numberOfBytesWritten = &amp;number;
      WriteFile(file, &amp;equation, sizeof(equation), numberOfBytesWritten, 
                                            NULL);
  
      printf("1: write to file");
  
      ReleaseSemaphore(semaphore1, 1, NULL);
      WaitForSingleObject(semaphore2, INFINITE);
  
      ReadFile(file, &amp;equation, sizeof(equation), numberOfBytesWritten, 
                                     NULL);
  
      if(equation.hasSolution)
      {
          printf("Solution");
      } else {
          printf("No solution");
      }
  
      char data[10];
      scanf("%s", data);
      return 0;
  }
  </code></pre>
  
  <p>/<em>Here is the 2nd process (Lab2_second):</em>/</p>
  
  <pre><code>#include "stdafx.h"
  #include "windows.h"
  
  const char* FIRST_SEMAPHORE = "Semaphore1";
  const char* SECOND_SEMAPHORE = "Semaphore2";
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      HANDLE semaphore1, semaphore2;
      semaphore1 = OpenSemaphore(EVENT_ALL_ACCESS, TRUE, `enter code 
                             here`ConvertLPCSTRToLPWSTR(FIRST_SEMAPHORE));
      semaphore2 = OpenSemaphore(EVENT_ALL_ACCESS, TRUE, 
                              ConvertLPCSTRToLPWSTR(SECOND_SEMAPHORE));
  
      WaitForSingleObject(semaphore1, INFINITE);
  
      printf("2: read from file");
  
      HANDLE file = CreateFile(ConvertLPCSTRToLPWSTR("equation"), 
                         GENERIC_WRITE, FILE_SHARE_READ, NULL, 
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  
      Equation equation;
      DWORD number = sizeof(equation);
      LPDWORD numberOfBytesWritten = &amp;number;
      ReadFile(file, &amp;equation, sizeof(equation), numberOfBytesWritten, 
                          NULL);
  
      // todo
  
      equation.hasSolution = FALSE;
      WriteFile(file, &amp;equation, sizeof(equation), numberOfBytesWritten, 
                             NULL);
  
      ReleaseSemaphore(semaphore2, 1, NULL);
  
      return 0;
  }
  </code></pre>
  
  <p>The question is: why when the parent starts, the 2nd process does not wait until the 1st release semaphore1 (that is, the 2nd process does not react to WaitForSingleObject (semaphore1, INFINITE)) and reads from the file from the very start (this is clear during startup: the 2nd process immediately displays "2: read from file"), but there is nothing there. How can I properly make the 2nd process wait for the 1st process to write the coefficients to a file?</p>