0

如何正确地让第二个进程等待第一个进程将系数写入文件?

我的任务是使用信号量同步 2 个进程。第一个进程读取数据,写入文件,然后第二个进程读取此数据并找到解决方案,然后写入文件,然后第一个进程从文件中读取此解决方案并将解决方案输出到控制台。

/这里是父文件: /

#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(&sInfo);        

    if(!CreateProcess(ConvertLPCSTRToLPWSTR(SECOND_PROCESS), NULL, NULL, 
            NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo))
    {  
        printf("Second process failed: %d\n", GetLastError());
    }

    if(!CreateProcess(ConvertLPCSTRToLPWSTR(FIRST_PROCESS), NULL, NULL, 
         NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo))
    {
        printf("First process failed: %d\n", GetLastError());      
    }


    return 0;
}

/这里是过程 1 (Lab2_first): /

#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", &equation.a, &equation.b, &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 = &number;
    WriteFile(file, &equation, sizeof(equation), numberOfBytesWritten, 
                                          NULL);

    printf("1: write to file");

    ReleaseSemaphore(semaphore1, 1, NULL);
    WaitForSingleObject(semaphore2, INFINITE);

    ReadFile(file, &equation, sizeof(equation), numberOfBytesWritten, 
                                   NULL);

    if(equation.hasSolution)
    {
        printf("Solution");
    } else {
        printf("No solution");
    }

    char data[10];
    scanf("%s", data);
    return 0;
}

/这是第二个过程(Lab2_second): /

#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 = &number;
    ReadFile(file, &equation, sizeof(equation), numberOfBytesWritten, 
                        NULL);

    // todo

    equation.hasSolution = FALSE;
    WriteFile(file, &equation, sizeof(equation), numberOfBytesWritten, 
                           NULL);

    ReleaseSemaphore(semaphore2, 1, NULL);

    return 0;
}

问题是:为什么当父进程启动时,2nd进程不等到1st release semaphore1(即2nd进程不响应WaitForSingleObject(semaphore1,INFINITE))并从一开始就从文件中读取(这个启动时很清楚:第二个进程立即显示“2:从文件中读取”),但那里什么也没有。如何正确地让第二个进程等待第一个进程将系数写入文件?

4

0 回答 0