当前位置:   article > 正文

关于 dup2(fd[0],STDOUT_FILENO) 的一些波折_fd != stdout_fileno

fd != stdout_fileno

在看 unix 环境高级编程 的时候,在管道这一部分,还没有看到后面的代码时,一直被一个问题困扰着。先看代码

  1. //15-6
  2. #include "apue.h"
  3. #include <sys/wait.h>
  4. #define DEF_PAGER "/bin/more" /* default pager program */
  5. int
  6. main(int argc, char *argv[])
  7. {
  8. int n;
  9. int fd[2];
  10. pid_t pid;
  11. char *pager, *argv0;
  12. char line[MAXLINE];
  13. FILE *fp;
  14. if (argc != 2)
  15. err_quit("usage: a.out <pathname>");
  16. if ((fp = fopen(argv[1], "r")) == NULL)
  17. err_sys("can't open %s", argv[1]);
  18. if (pipe(fd) < 0)
  19. err_sys("pipe error");
  20. if ((pid = fork()) < 0) {
  21. err_sys("fork error");
  22. } else if (pid > 0) { /* parent */
  23. close(fd[0]); /* close read end */
  24. /* parent copies argv[1] to pipe */
  25. while (fgets(line, MAXLINE, fp) != NULL) {
  26. n = strlen(line);
  27. if (write(fd[1], line, n) != n) //这里只是把 line 中的字符串写入到管道的写端。还是很正常的
  28. err_sys(&
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/462676
推荐阅读
相关标签
  

闽ICP备14008679号