pipe创建函数:
#include <unistd.h> /* Create a one-way communication channel (pipe). If successful, two file descriptors are stored in PIPEDES; bytes written on PIPEDES[1] can be read from PIPEDES[0]. Returns 0 if successful, -1 if not. */ int pipe (int __pipedes[2]);
下面是一个测试实例,有些未说明函数,如Pipe,是pipe的一个包裹函数,二者参数及用法一致,仅仅是在包裹函数中添加了出错信息。
void client(int, int); void server(int, int); int main(int argc, char **argv) { int pipe1[2], pipe2[2]; pid_t childpid; Pipe(pipe1); /* create two pipes */ Pipe(pipe2); if ( (childpid = Fork()) == 0) /* child */ { Close(pipe1[1]); Close(pipe2[0]); server(pipe1[0], pipe2[1]); // 子线程作为服务器端 exit(0); } /* 4parent */ Close(pipe1[0]); Close(pipe2[1]); client(pipe2[0], pipe1[1]); Waitpid(childpid, NULL, 0); /* wait for child to terminate */ exit(0); }
/** * 从客户端读取文件名,打开文件并将文件内容返回给客户端 * @param readfd 管道读描述符 * @param writefd 管道写描述符 */ void server(int readfd, int writefd) { int fd; ssize_t n; char buff[MAXLINE + 1]; /* 4read pathname from IPC channel */ if ( (n = Read(readfd, buff, MAXLINE)) == 0) err_quit("end-of-file while reading pathname"); buff[n] = '