execv怎么会从管道输出

execv怎么会从管道输出

本文介绍了execv怎么会从管道输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指的是旧的作业问题: / *实施的/ usr / bin中/ PS -ef |在/ usr /斌/更多的* /
使用管道。

referring to the old homework question : /* implementing "/usr/bin/ps -ef | /usr/bin/more" */using pipes.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
  int fds[2];
  int child[2];
  char *argv[3];
  pipe(fds);
  if (fork()== 0) {
    close(fds[1]);
    close(STDIN_FILENO); dup(fds[0]); /* redirect standard input to fds[1] */
    argv[0] = "/bin/more";
    argv[1] = NULL;           /* check how the argv array is set */
    execv(argv[0], argv);// here how execv reads from stdin ??
    exit(0);


  }
  if (fork() == 0) {
    close(fds[0]);
    close(STDOUT_FILENO); dup(fds[1]);  /* redirect standard output to fds[0] */
    argv[0] = "/bin/ps";
    argv[1] = "-e"; argv[2] = NULL;
    execv(argv[0], argv);
    exit(0);

  }

  close(fds[1]);
  wait(&child[0]);
  wait(&child[0]);
}

在FD重定向到标准输出后,如何做execv从中读取。它是内置在execv,它从标准输入执行命令前读取?我不能让这个概念。

After redirecting the fd to standard output, how does execv reads from it. Is it inbuilt in execv that it reads from standard input before executing the command? I am unable to get this concept.

推荐答案

您的问题是基于一个错误的premise - execv 不从任何地方阅读,也不需要。这是更多从读标准输入它继承了整个呼叫 execv 。究其原因更多标准输入读是因为它是一个过滤器,最喜欢的滤镜,则默认为从<$阅读C $ C>标准输入如果没有在命令行上指定的另一个输入源。 (否则,的/ usr / bin中/ PS -ef |。在/ usr /斌/多是行不通的)

Your question is based on a false premise -- execv doesn't read from anywhere, nor does it need to. It is more that reads from the stdin it inherits across the call to execv. The reason more reads from stdin is because it's a filter and, like most filters, it defaults to reading from stdin if another input source isn't specified on the command line. (Otherwise, /usr/bin/ps -ef | /usr/bin/more wouldn't work.)

这篇关于execv怎么会从管道输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:50