i've written relatively piece of code polls pipe redirected stdout in child process. when poll function returns , says data ready, read function returns zero.
is there reason stands out why isn't working? when wrote directly pipe child process parent process, worked fine. think i'm redirecting io incorrectly.
here's parent process code:
note: macros account errors , print them error log.
polldata.fd = out.fd[ read_index ]; polldata.events = poll_in; polldata.revents = 0; printf( "polling pipe 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 read.\n" ); sleep( sleep ); if( trys > 0 ) goto read; } } else log( err_polling, 0 );
here's child process code: note: fd_replace uses dup2 , closes 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 message test program!\n hope 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 1 of macros:
#define fd_replace( old_fd, fd , addr ) if( dup2( old_fd, fd ) < 0 ) { log( err_dup2, #old_fd " " #fd ); goto addr; }
here's output terminal:
polling pipe data... data has been received: no data read. no data read. no data read. no data read. no data read. closing...
i got working. replacing file descriptors incorrectly. replacing wrong descriptor , doing before closed old one. code posted above shows old macro. new macro ( without error checking ):
#define fd_replace( old_fd, fd, addr ) close( old_fd ); dup2( fd, old_fd );
Comments
Post a Comment