开发者

VC++ ReadFile Unexpectedly Blocks While Reading a Closed Anonymous Pipe

开发者 https://www.devze.com 2023-04-12 23:12 出处:网络
I am trying to create a child process running any exe command and redirect all its stdio and stderr to my parent process through an anonymous pipe. But when my parent process tries to do a ReadFile()

I am trying to create a child process running any exe command and redirect all its stdio and stderr to my parent process through an anonymous pipe. But when my parent process tries to do a ReadFile() on the READ end of the anonymous pipe after the child has terminated, it just blocks instead of returning an error.

Here is the code:

#include "windows.h"
//==================================================
void createChildPipes(PHANDLE phRe开发者_JS百科ad, PHANDLE phWrite){
  SECURITY_ATTRIBUTES sa={sizeof(SECURITY_ATTRIBUTES) ,NULL,TRUE};
  if ( !CreatePipe(phRead, phWrite, &sa, 2048) ) { //...  }
}
//==================================================
void setupStartupInfo(LPSTARTUPINFO lpsi, HANDLE hStdOutput, HANDLE hStdError) {
  lpsi->cb = sizeof(STARTUPINFO);
  lpsi->lpReserved=NULL;
  lpsi->lpDesktop=NULL;
  lpsi->lpTitle=NULL;
  lpsi->dwX=0;
  lpsi->dwY=0;
  lpsi->dwXSize=200;
  lpsi->dwYSize=500;
  lpsi->dwFlags=STARTF_USESTDHANDLES;
  lpsi->cbReserved2=0;
  lpsi->lpReserved2=NULL;
  lpsi->hStdInput=GetStdHandle(STD_INPUT_HANDLE);
  lpsi->hStdError= hStdError;
  lpsi->hStdOutput=hStdOutput;
}
//==================================================
void createChildProcess(PHANDLE phOutRead, PHANDLE phOutWrite, PHANDLE phErrRead, PHANDLE phErrWrite) {
  TCHAR name[]=_T("cl.exe");
  createChildPipes(phOutRead, phOutWrite);
  STARTUPINFO si;
  setupStartupInfo(&si, *phOutWrite, *phOutWrite);
  PROCESS_INFORMATION pi;
  if (!CreateProcess(NULL, name, NULL,  NULL,   true, 0,   NULL,  NULL,  &si,  &pi)) { //...}
}
//==================================================
void _tmain(int argc, _TCHAR* argv[]){
  HANDLE hOutRead, hOutWrite, hErrRead, hErrWrite;
  createChildProcess(&hOutRead, &hOutWrite, &hErrRead, &hErrWrite) ;
  char buf[10];

  BOOL readState;
  for (;;) {
    DWORD dwBytesRead;
    memset(buf, '\0', sizeof(buf));
    readState=ReadFile(hOutRead, buf, sizeof(buf)-2 , &dwBytesRead, NULL);
    printf("%s", buf);
    if (!readState)
      break;
  } 

}

This is what my code is doing. My _tmain() creates a child process (I am using the VC++ cl.exe command as a test) and redirects its stdio and stderr to the write HANDLE of an anonymous pipe. My parent process reads from the read HANDLE of the same pipe.

These are printed by my _tmain() showing that the pipe can communicate across the parent-child processes. These are what we expect to see if we type cl.exe in the command line without any arguments. Note the peculiar behaviour of cl.exe that the first 2 lines are from the stderr of cl.exe and the last line is from the stdout.

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

Now about the problem, the line:

readState=ReadFile(hOutRead, buf, sizeof(buf)-2 , &dwBytesRead, NULL);

in _tmain() blocks after the child cl.exe process terminated. But, I am expecting the ReadFile() call to return with an ERROR_BROKEN_PIPE error status and exit the parent loop instead of blocking the ReadFile() call.

Why the ReadFile() blocks when reading an anonymous pipe?


You forgot to close the pipe handles in the parent process, so the pipe is not completely closed yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号