Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
三年前关闭。
这是一项任务,所以我不会问具体的问题。
在这部分作业中,我需要使用pipes to“pipe”sort-r to ls-l。
我知道如何执行execl,但我无法理解使用pipe将“execl的输出”传递给另一个execl意味着什么。
有人能重读一些关于如何做到这一点的读物吗?
我试过用谷歌搜索很多不同的东西,但我就是不明白。
我也试过看不同的管道解释,但对我来说都没有意义。
如果有人能提供一些好的阅读资料,帮助那些不知道管道是如何工作的人(除了用|命令行传递信息)理解管道,我将不胜感激。
提前谢谢。

最佳答案

基本上你可以看到像生产者和消费者这样的管道。
看这个简单的例子。在这段代码中,父进程在管道中写入一些字符串,子进程读取并在输出中打印它。
你想要做的是接近这个,父期望第一个命令,子执行第二个命令。但是,您可以使子命令执行多个命令,从而使它更高级。
other example

    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    /*

 Read characters from the pipe and echo them to stdout. */

    void
    read_from_pipe (int file)
    {
      FILE *stream;
      int c;
      stream = fdopen (file, "r");
      while ((c = fgetc (stream)) != EOF)
        putchar (c);
      fclose (stream);
    }

    /* Write some random text to the pipe. */

    void
    write_to_pipe (int file)
    {
      FILE *stream;
      stream = fdopen (file, "w");
      fprintf (stream, "hello, world!\n");
      fprintf (stream, "goodbye, world!\n");
      fclose (stream);
    }

    int
    main (void)
    {
      pid_t pid;
      int mypipe[2];

      /* Create the pipe. */
      if (pipe (mypipe))
        {
          fprintf (stderr, "Pipe failed.\n");
          return EXIT_FAILURE;
        }
      /* Create the child process. */
      pid = fork ();
      if (pid == (pid_t) 0)
        {
          /* This is the child process.
             Close other end first. */
          close (mypipe[1]);
          read_from_pipe (mypipe[0]);
          return EXIT_SUCCESS;
        }
      else if (pid < (pid_t) 0)
        {
          /* The fork failed. */
          fprintf (stderr, "Fork failed.\n");
          return EXIT_FAILURE;
        }
      else
        {
          /* This is the parent process.
             Close other end first. */
          close (mypipe[0]);
          write_to_pipe (mypipe[1]);
          return EXIT_SUCCESS;
        }
    }

关于c - 模拟“ls -l |使用C中的管道对-r进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34987096/

10-11 22:58
查看更多