c - 如何使用管道运行命令?

标签 c unix pipe file-descriptor

我正在尝试使用 execvp 运行 ls|wc。所以我创建了一个管道,然后创建了一个子管道。我关闭父/子中适当的(读/写)端,然后将另一端映射到标准输出/标准输入。然后我在父级中使用 execvp 运行 ls ,在子级中使用 wc 。当我运行该程序时,它显示

 wc:standard input:bad file descriptor.
 0 0 0
 wc: -:Bad file descriptor

这是我的代码:

int main()
{
//int nbBytes = 0; //stream length
int pfd_1[2]; //file descriptor 
//char buffer[MAX_FILE_LENGTH]; 
char* arg[MAX_FILE_LENGTH];
pid_t processPid;

//Create a pipe

if(pipe(pfd_1) == -1)
{
    printf("Error in creating pipe");
    return 0;
}

//Create a child
processPid = fork();

if(processPid == -1)
{
    printf("Erro in fork");
    exit(1);
}   
else if(processPid == 0) //Child
{               
    //redirect read end file descriptor to standard input
    dup2(pfd_1[0],0);
    //Close the write end
    if(close(pfd_1[1] == -1))
    {
        printf("Error in closing the write end file descriptor");
        exit(1);
    }
    arg[0] = "wc";
    //arg[1] = "-l";
    arg[1] = '\0';

    if(execvp(arg[0],arg) == -1)
    {
        printf("Error in executing ls");
    }       

}
else //Parent
{               
    //redirect standard output to the file descriptor
    dup2(pfd_1[1],1);
    //Close the read end
    if(close(pfd_1[0] == -1))
    {
        printf("Error in closing the read end from parent");
        exit(1);
    }
    //Command 
    arg[0] = "ls";
    arg[1] = "/proc/1/status";
    arg[2] = '\0';

    if(execvp(arg[0],arg) == -1)
    {
        printf("Error in executing ls");
    }       
}

}

知道可能出了什么问题吗?为什么它会将标准输入视为错误的文件描述符?我的理解是,由于 stdin 和读取结束文件描述符是别名,因此 wc -l 会读取来自父进程的任何输出。我需要执行 scanf 才能从标准输入中读取数据吗?

最佳答案

问题出在这一行:

if(close(pfd_1[1] == -1))

您正在关闭pfd_1[1] == -1的结果,它必然等于0(因为它们永远不会相等)。正确的行可能是:

if (close(pfd_1[1]) == -1)

请注意,您稍后会在尝试关闭父进程中的读取端时再次执行此操作。

关于c - 如何使用管道运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14300880/

相关文章:

用于查找最坏可能路径的 c 程序的控制流程图

shell - 在Shell脚本中的ssh session 中评估和分配变量

regex - 在 sed 中使用正则表达式查找和替换

c - 管道时,为什么在使用 dup2() 之前必须关闭() 管道的另一端?

Java无法读取写入标准输出的有符号负字节

c - 在字符串中打印不正确的字符数

c - 在 Linux 终端中设置字体格式

c - 程序集与 C 可执行文件大小

unix - 如何创建路径别名?

bash - 计算时bash中的变量错误