linux下,需要子进程启动别的进程跟父进程通信,如何实现?
需求:写一个服务器A,通过配置启动N个进程B,需要进程A跟进程B通信,进程B如果异常退出,进程A可以启动进程B。。
windows下已经实现了,linux下如果实现呢?
进程B的写:cout
进程B的读:getline
如何跟B进行通信呢,在B不修改的情况下
伪码:
bool CProcess::Execute(const string& Command)
{
bool bRet = true;
m_pid = -1;
int pipeFDs[2];
if (pipe(pipeFDs) < 0)
{
LOG_ERROR << "pipe err!!" << endl;
}
m_pid = fork();
if (m_pid == 0)
{
LOG_DEBUG << "child process :" << Command.c_str() << endl;
//子进程
if (execl(Command.c_str(), "test", NULL) < 0)
{
LOG_ERROR << "execl faild!!" << endl;
}
exit(0);
}
else if (m_pid == -1)
{
//出错
LOG_ERROR << "fork err!!" << endl;
}
else
{
m_hPipes[Read] = pipeFDs[Read];
m_hPipes[Write] = pipeFDs[Write];
//父进程
LOG_DEBUG << "parent process" << endl;
}
return bRet;
}