cwindowspipeanonymous

What is the best way to close a nonstandard anonymous pipe handle for child process on Windows?


On Linux, the pseudo-code looks roughly like this:

setup_env();
pid = fork();
if (pid == 0) {
// we are the child
    closeUnusedPipeEnd();
    setup_child();
    execve();
}
closeOtherUnusedPipeEnd();

posix_spawn uses file_actions to register what the child process would do in spawnp, which boils down to a clone() instead of fork().

However, I do not find Windows examples for how to close the unused end of the anonymous pipe in the child process. Is my only option to do this inside the child process, for example via stdin parsing or environment variables? Or what is considered best practice in Windows for anonymous pipes?

The pipe handles are assumed to be created from kernel32.CreateNamedPipeW and kernel32.CreateFileW and read handle for overlapped IO (FILE_FLAG_OVERLAPPED).


Solution

  • One should use kernel32.SetHandleInformation() with HANDLE_FLAG_INHERIT to only inherit the pipe end used in the child process.