c - 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.
解决方案
该pipe
函数具有以下签名:
int pipe(int pipefd[2]);
当您调用它时,它会将两个文件描述符写入您传递给它的数组中。
上述语句中的第一个文件描述符pipefd[0]
用于读取管道的输出。
上述语句中的第二个文件描述符pipefd[1]
用于写入管道。
推荐阅读
- arrays - Presto SQL - 如何获得数组的所有可能组合?
- r - dplyr::mutate 时间戳
- git - 配置 git-lfs 以跟踪超过一定大小的文件
- php - array_push() 期望参数 1 是带有 json 文件的数组
- c# - 使用“new”的 C# 结构仍在堆栈或堆上?
- react-native - 是否有任何应用程序可以为 Windows 操作系统构建反应原生 ui
- pandas - Pandas Chanining:有绩效福利吗?
- javascript - 数组中的字符串包含公共子字符串 javascript
- javascript - 通过 onclick 向 javascript 发送变量时未捕获的引用错误
- node.js - angular 7 到 angular 8 迁移后 chartjs 的错误