none
Mistake in "Creating a Child Process with Redirected Input and Output" example RRS feed

  • General discussion

  • Hi,

    I am currently learning about the WIN32 API, and I think that I have noticed a mistake in this provided example:

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

    The example explains that the Child process should not inherit the 

    g_hChildStd_OUT_Rd

    and

    g_hChildStd_IN_Wr

    handles, otherwise, the ReadFile() function in the child would not return, because the child would have both the input and the output handles for both anonymous pipes.

    When I execute the code, it enters a deadlock on the parent side, in the ReadFile() function (after reading all the useful data from the pipe, it just hangs and the process does not terminate as it should)

    This happens because in the example, the parent thread has access to both handles of both anonymous pipes. A fix would be to close these handles after the creation of the child process, like this:

    if (!bSuccess) ErrorExit(_T("CreateProcess")); else { CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread);

    //Complete the following two lines in the function CreateChildProcess. CloseHandle(g_hChildStd_IN_Rd); CloseHandle(g_hChildStd_OUT_Wr); }


    In this way, the deadlock disappears and the ReadFile() can return when it read everything from the buffer.

    Thanks for these examples and I hope that you will correct this mistake.

    • Moved by litdev Thursday, April 6, 2017 5:28 PM
    • Moved by Dave PatrickMVP Thursday, April 6, 2017 9:48 PM
    Thursday, April 6, 2017 12:59 PM