开发者

How to use a pipe with select after forking and executing?

I'm a noob to linux programming, so please bear with me. In my application, I fork(), then execl() another binary after having setup a single pipe for reading in. After the fork and exec are OK, i do a dup2() for reading in from the stdout of the executed binary. I need my parent application to wait for output from the process it has created and once there is output, read it. I figured I will use select(), and wait for a few milliseconds before trying to see if there is data to be read and if there is, use read(). However my code does not work because select() takes as argument an fd_set, while my pipe is of int converted by 开发者_如何学运维pipe() and dup2(). What can I do to overcome this and is there another alternative? Note, I'm not blocking the parent process until the process ends, but want to read info while the child process runs.


To use select() you must create a struct fd_set and populate it using the FD_ macros. In this way you will inform the function which descriptors you are interested in (note that it is common to be interested in several at once). For example:

fd_set rfds;

FD_ZERO(&rfds);
FD_SET(your_input_fd, &rfds);

int retval = select(your_input_fd + 1, &rfds, NULL, NULL, NULL);

The first argument to select is to be the highest-numbered file descriptor you are interested in, plus one. That, along with example code, is explained here: http://linux.die.net/man/3/fd_set

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜