#include #include #include #include #include #include const char *message = "1234567"; void *worker( void *_ ) { (void)_; int fd = open( "pipe", O_WRONLY ); assert( fd >= 0 ); assert( write( fd, message, 7 ) == 7 ); assert( write( fd, "-", 1 ) == 1 ); assert( close( fd ) == 0 ); } int main() { char buf[ 8 ] = {}; assert( mkfifo( "pipe", 0644 ) == 0 ); int fd = open( "pipe", O_RDONLY | O_NONBLOCK ); pthread_t thread; pthread_create( &thread, NULL, worker, NULL ); assert( fd >= 0 ); char incoming; int total = 0; while ( 1 ) { int cnt = read( fd, &incoming, 1 ); if ( !cnt ) continue; assert( cnt == 1 ); if ( incoming == '-' ) break; buf[ total ] = incoming; total += cnt; } pthread_join( thread, NULL ); assert( strcmp( buf, message ) == 0 ); return 0; }