gpt4 book ai didi

c++ - 如果 stdin 被另一个进程的管道替换,则 std::getline 中断

转载 作者:行者123 更新时间:2023-11-28 07:43:49 24 4
gpt4 key购买 nike

我有一个简单的示例程序,它从 std::cin 读取并写入 std::cout。如果在 cmd.exe 或 visual studio 调试器中运行,它工作正常。代码(server.cpp):

#include <iostream>
#include <istream>
#include <ostream>
#include <string>

int main(int argc, char* argv[])
{
std::string input;
while (std::getline(std::cin, input))
{
if (input == "dog")
{
std::cout << "cat" << std::endl;
}
else if (input == "white")
{
std::cout << "black" << std::endl;
}
else if (input == "quit")
{
std::cout << "exiting" << std::endl;
return 0;
}
else if (input != "")
{
std::cout << "unknown" << std::endl;
}
}

std::cout << "error" << std::endl;
}

现在我想从另一个写入其标准输入并从其标准输出读取的进程运行它。我创建了两个管道并使用 CreateProcess 启动一个进程,其中一个管道的读取句柄作为 StdInput 句柄,另一个管道的写入句柄作为 Stdouput 句柄。代码(client.cpp):

#include <Windows.h>

#include <cassert>
#include <iostream>
#include <ostream>
#include <string>

namespace
{
class Server
{
public:
Server() :
m_pi()
{
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES)};
sa.bInheritHandle = TRUE;
assert(CreatePipe(&m_ro, &m_wo, &sa, 0));
assert(SetHandleInformation(m_ro, HANDLE_FLAG_INHERIT, 0));
assert(CreatePipe(&m_ri, &m_wi, &sa, 0));
assert(SetHandleInformation(m_ri, HANDLE_FLAG_INHERIT, 0));

STARTUPINFO si = {sizeof(STARTUPINFO)};
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput = m_ri;
si.hStdOutput = m_wo;

assert(CreateProcess(L"..\\Debug\\server.exe", 0, 0, 0, 1, 0, 0, 0, &si, &m_pi));
}

~Server()
{
execute("quit\n");
assert(WaitForSingleObject(m_pi.hProcess, INFINITE) != WAIT_FAILED);

assert(CloseHandle(m_pi.hThread));
assert(CloseHandle(m_pi.hProcess));
assert(CloseHandle(m_wi));
assert(CloseHandle(m_ri));
assert(CloseHandle(m_wo));
assert(CloseHandle(m_ro));
}

std::string execute(std::string const& cmd)
{
DWORD num_bytes;
assert(WriteFile(m_wi, cmd.c_str(), (DWORD)cmd.size(), &num_bytes, 0));

std::string output;

DWORD n = 0;
while (n == 0)
{
Sleep(0);

assert(PeekNamedPipe(m_ro, 0, 0, 0, &n, 0));
if (n > 0)
{
output.resize(n);
assert(ReadFile(m_ro, &output[0], n, &num_bytes, 0));
}
}

return output;
}

private:
HANDLE m_wo, m_ro, m_wi, m_ri;
PROCESS_INFORMATION m_pi;
};

Server g_server;
}

int main(int argc, char* argv[])
{
std::cout << g_server.execute("white\n") << std::endl;
std::cout << g_server.execute("foobar\n") << std::endl;
std::cout << g_server.execute("dog\n") << std::endl;
}

问题是客户端只收到消息“error”,所以服务器的std::cin好像坏了。

我的问题是,我做错了什么?

最佳答案

您正在禁用 child 将用来从 stdin 读取的句柄的继承 - child 需要继承该句柄。而不是:

SetHandleInformation(m_ri, HANDLE_FLAG_INHERIT, 0);

尝试以下操作以禁用服务器进程将用于写入子级 stdin 的句柄继承:

SetHandleInformation(m_wi, HANDLE_FLAG_INHERIT, 0);

关于c++ - 如果 stdin 被另一个进程的管道替换,则 std::getline 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15269090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com