如何在我的两个进程程序中正确设置信号量?

时间:2018-12-20 07:36:36

标签: c++ windows process synchronizing

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

我的任务是使用信号量同步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;
}

问题是:为什么当父进程启动时,第二个进程不等到第一个发行版semaphore1(即,第二个进程不对WaitForSingleObject(semaphore1,INFINITE)做出反应)并从文件中读取文件。 start(在启动过程中很明显:第二个进程立即显示“ 2:从文件读取”),但是没有任何内容。如何使第二进程等待第一进程将系数写入文件?

0 个答案:

没有答案