首页 > 解决方案 > C Linux Programming Book: Chapter Pipes

问题描述

I'm currently reading the book "The Linux Programming Interface". The example explaining pipes contains a piece of code which reads data from a pipe (inside the for loop, under the case 0 statement):

numRead = read(pfd[0], buf, BUF_SIZE)

My question is: what is pfd[0]? I know it's a pipe file descriptor array, it was declared as int pfd[2], so an array of integers of two elements. But what is the element pfd[0]? I cannot see it be initialized anywhere. If I print it then it's an integer (rather large) like 22302.

标签: clinuxpipe

解决方案


pipe函数具有以下签名:

int pipe(int pipefd[2]);

当您调用它时,它会将两个文件描述符写入您传递给它的数组中。

上述语句中的第一个文件描述符pipefd[0]用于读取管道的输出。

上述语句中的第二个文件描述符pipefd[1]用于写入管道。


推荐阅读