Can't read data from redirected io

I've written a relatively simply piece of code that polls a pipe that was redirected as stdout in a child process. But when the poll function returns and says data is ready, the read function always returns zero. Is there any reason that stands out as to why this isn't working? When I wrote directly to the pipe from the child process to the parent process, it worked fine. I think I'm just redirecting the io incorrectly. Here's the parent process code: Note: all macros account for errors and print them to an error log. pollData.fd = out.fd[ READ_INDEX ]; pollData.events = POLL_IN; pollData.revents = 0; printf( "Polling pipe for data...\n" ); if( poll( &pollData, 1, 5000 ) > 0 ) { int bytesRead = 0, trys = TRYS; printf( "Data has been received: " ); Read: bytesRead = read( out.fd[ READ_INDEX ] , out.buf, BUF_SIZE ); if( bytesRead > 0 ) printf( "%s" , out.buf ); else if( bytesRead < 0 ) printf( "Error receiving data.\n" ); else { trys--; printf( "No data was read.\n" ); sleep( SLEEP ); if( trys > 0 ) goto Read; } } else LOG( ERR_POLLING, 0 ); Here's the child process code: Note: FD_REPLACE uses dup2 and closes the old handle. FD_CLOSE_PACK( out.fd[ READ_INDEX ], err.fd[ READ_INDEX ], in.fd[ WRITE_INDEX ] ); FD_REPLACE( fileno( stdout ) , out.fd[ WRITE_INDEX ] , Child_Fail ); FD_REPLACE( fileno( stderr ) , err.fd[ WRITE_INDEX ] , Child_Fail ); FD_REPLACE( fileno( stdin ) , in.fd[ READ_INDEX ] , Child_Fail ); printf( "This is a message from the test program!\n Hope it works!\n" ); sleep( 10 ); Child_Fail: FD_CLOSE_PACK( out.fd[ WRITE_INDEX ], err.fd[ WRITE_INDEX ], in.fd[ READ_INDEX ] ); return -1; Here's one of the macros: #define FD_REPLACE( old_fd, fd , addr ) if( dup2( old_fd, fd ) < 0 ) { LOG( ERR_DUP2, #old_fd " with " #fd ); goto addr; } Here's the output from the terminal: Polling pipe for data... Data has been received: No data was read. No data was read. No data was read. No data was read. No data was read. Now closing...
What is the value of pollData.revents immediately after poll returns? Does it have the POLLIN flag set?

以上就是Can't read data from redirected io的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏