【发布时间】:2021-12-30 16:37:14
【问题描述】:
例如,我们有$(ls):
int pfd[2];
pipe(pfd);
switch (fork()) {
case 0:
close(pfd[0]);
dup2(pfd[1], 1 /*stdout*/);
exec('ls', 'ls');
case -1;
// Report the error...
break;
default;
break;
}
wait(nullptr); // Wait until the process is done (it is better to use the waitpid() version)
// And now we can read from pfd[0]
这段代码非常概念化,但我说的对吗?子进程完成后是否可以从管道的写入端提取数据?剩下的就是将子字符串 ($(ls)) 替换为另一个(ls 本身的结果)。如果我错了,请纠正我。
即使pfd[0] 是一个有效的文件描述符,它指向一个带有ls 执行结果的缓冲区,我们如何安全地从中读取?
【问题讨论】:
标签: c++ c linux posix system-calls