【问题标题】:linux terminal command with a pipe in c code带有管道的linux终端命令在c代码中
【发布时间】:2017-01-31 21:35:10
【问题描述】:

我正在尝试使用 c 代码中的简单管道执行 Linux 命令“ls -l | tail -n 2”。

我添加了您的提示,现在可以使用,但输出并不完全符合预期。它在一行而不是两行中打印输出,并等待用户输入关闭。 这是新代码:

#include "stdio.h"
#include "unistd.h"
#include "stdlib.h"
#include "sys/wait.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void main()
{
    char line[100];
    pid_t pid;
    int fd[2];
    int status;
    char* ls_arguments[] = {"ls", "-l", NULL};
    char* tail_arguments[] = {"tail", "-n", "2", NULL};
    pipe(fd);
    pid = fork();
    if(pid == 0)//ls client
    {
        close(1);
        dup(fd[1]);
        close(fd[0]);
        execvp("ls", ls_arguments);
    }
    pid = fork();
    if(pid == 0)//tail client
    {
        close(0);
    close(fd[1]);
        dup(fd[0]);
        execvp("tail", tail_arguments);
    }
    wait(pid, 0, WNOHANG);
    close(fd[0]);
    close(fd[1]);
}

这应该运行“ls -l”命令并输出到管道,下一个“tail”客户端会将其作为输入并运行“tail -n 2”命令并打印出最终输出,但终端会打印没有。有什么帮助吗?

【问题讨论】:

  • 将代码放在execvp() 之后是没有意义的,因为除非出错,否则它永远不会返回。
  • 您需要在ls 客户端中close(fd[0])tail 客户端中的close(fd[1]),然后才能分别调用exec
  • 并且父进程需要关闭它们。
  • 但是如果我在 ls 客户端中关闭管道,它会在尾部客户端中关闭,对吗?
  • 没有。在一个进程中关闭 fd 对其他进程没有影响。在所有进程关闭它之前,管道不会消失。

标签: c linux command pipe


【解决方案1】:

首先,没有wait这样的功能,这是man所说的:

#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *status);

pid_t waitpid(pid_t pid, int *status, int options);

我认为你的意思是使用waitpid

然后,您的子进程没有完成,因为管道仍在某处打开:在父进程中。实际上,您应该首先关闭描述符,然后等待您的子进程。我会写:

  close(fd[0]);
  close(fd[1]);
  wait(NULL); // Wait for the first child to finish
  wait(NULL); // Wait fot the second one
  return 0;
}

代替:

  wait(pid, 0, WNOHANG);
  close(fd[0]);
  close(fd[1]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-03
    • 2012-06-18
    • 2022-08-05
    • 2017-04-16
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多