/* pipedemo.c * * 创建管道并使用管道来向自己发送数据 */ #include #include main() { int len, i, apipe[2]; /* two file descriptors */ char buf[BUFSIZ]; /* for reading end */ /* get a pipe */ if ( pipe ( apipe ) == -1 ){ perror("could not make pipe"); exit(1); } printf("Got a pipe! It is file descriptors: { %d %d }\n", apipe[0], apipe[1]); /* read from stdin, write into pipe, read from pipe, print */ while ( fgets(buf, BUFSIZ, stdin) ){ len = strlen( buf ); if ( write( apipe[1], buf, len) != len ){ /* send */ perror("writing to pipe"); /* down */ break; /* pipe */ } for ( i = 0 ; i