【发布时间】:2014-08-10 11:43:54
【问题描述】:
大约 5 年前,我有一个完全正确且有效的程序。那时我停止使用它,我升级了操作系统,时间过去了,灰尘覆盖了代码,最后我把它挖出来发现它不再与子进程通信了。
这里是代码(简化了,但它显示了问题):
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <iostream>
#include <string>
#include <cassert>
int main()
{
std::cout << "Creating the pipe for reading from the process" << std::endl;
const std::string pipe_name = "/tmp/proc_comm";
{
int res = mkfifo(pipe_name.c_str(),0777);
assert(res==0);
}
std::cout << "Launching subprocess" << std::endl;
FILE *cmd_handle = popen(("espeak -x -q -z 1> "+pipe_name+" 2> /dev/null").c_str(), "w");
assert(cmd_handle!=0);
std::cout << "Opening the pipe" << std::endl;
int pipe_id = open(pipe_name.c_str(),O_RDONLY);
assert(pipe_id!=-1);
const std::string message = "hello\n";
std::cout << "Sending the message" << std::endl;
if (!fwrite(message.c_str(),sizeof(char),message.length(),cmd_handle))
assert(0);
if (ferror(cmd_handle))
assert(0);
if (fflush(cmd_handle)!=0)
assert(0);
fd_set output_set;
FD_ZERO(&output_set);
FD_SET(pipe_id,&output_set);
static timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
std::cout << "Selecting the pipe for reading" << std::endl;
const int inputs = select(pipe_id+1, // max of pipe ids + 1
&output_set,0,0,&timeout);
if (inputs==-1) // error
assert(0);
else if (inputs==0) // nothing to read
assert(0); // HERE (*)
else
{
// we can only read from our pipe
assert(inputs==1);
assert(FD_ISSET(pipe_id,&output_set));
const int bufsize = 20000;
char char_buf[bufsize];
memset(char_buf,0,sizeof(char_buf));
std::cout << "Reading from the pipe" << std::endl;
const int count = read(pipe_id,char_buf,bufsize-1);
if (count==-1)
assert(0);
std::cout << "Read " << count << std::endl;
}
return 0;
}
它编译、运行、创建并打开用于读取进程的管道、启动子进程、发送消息但没有可从进程读取的内容(HERE (*) 行)。
那么你现在如何从流程中读取?如果可能的话,我想保留一般的工作流程,即使用管道进行读取,并使用进程句柄进行写入处理。
【问题讨论】:
-
你怎么知道子进程在你超时之前发送了一些东西?
-
@jxh,我可以在 bash 中运行该进程并评估超时。 10秒绰绰有余。除此之外它还有效:-)
标签: c++ linux unix process pipe