c - 执行 ls | c 程序中的 wc linux 命令使用 2 个进程通过管道进行通信

标签 c linux process pipe dup

我目前在进行以下练习时遇到问题:

我想“模仿”管道命令行 ls | wc 在 Linux bash 中使用以下程序。 我所做的是:

  • 创建管道
  • 创建读者子级和编写器子级
  • 编写器子进程关闭管道的读取端并将其标准输出重定向到管道的写入端
  • 读者 child 关闭管道的写入端并使管道的读取端成为他的标准输入
  • 两个子进程都执行 exec,编写器执行 ls 程序,通过管道将输出传递给读取器,读取器在该输出上执行 wc 程序。<

当我这样做时ls | wc 在 Linux 终端中我得到以下结果:

8      8     101

但是如果我执行我的程序,我会得到以下结果:

0      0      0

这是我的程序:

#include <stdlib.h> 
#include <errno.h> 
#include <stdio.h>
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <string.h> 
#include <sys/wait.h>
#include <libgen.h>
#include <signal.h>
#include <errno.h>

int main(void){
        int mypipe[2];
        pid_t pid1, pid2;

        if (pipe(mypipe)<0)
                perror ("pipe error"), exit(1);
        if ((pid1=fork())<0)
                perror ("fork error"), exit(1);

        else if (pid1==0) {
                //reader child
                close (mypipe[1]);
                if (dup2(mypipe[0], STDIN_FILENO)!=STDIN_FILENO)
                        perror ("dup2 error"), exit(1);
                close (mypipe[0]); 
                if (execlp("wc", "wc", NULL)<0)
                        perror("execlp1 error"), exit(1);
                else {  //pid >0, parent
                        if ((pid2=fork())<0)
                                perror ("fork error"), exit(2);
                        else if (pid2==0) {     
                                //writer child
                                close(mypipe[0]);
                                if (dup2(mypipe[1], STDOUT_FILENO) != STDOUT_FILENO)
                                        perror("dup2 error"), exit(1);
                                close (mypipe[1]);
                                if (execlp("ls", "ls", NULL)<0)
                                        perror ("execlp error"), exit(1);
                        }
                        else {  //parent
                                close(mypipe[0]);
                                close(mypipe[1]);

                                waitpid(pid1, NULL, 0);
                                waitpid(pid2, NULL, 0);
                                exit(0);
                        }
                }
        }
return 0;
}

我做错了什么?预先感谢您的回答!

最佳答案

你的代码很困惑。您有许多不相关的 header ,并重复 #include <errno.h> 。在 main()函数,你有:

int main(void)
{
    int mypipe[2];
    pid_t pid1, pid2;

    if (pipe(mypipe) < 0)
        perror("pipe error"), exit(1);
    if ((pid1 = fork()) < 0)
        perror("fork error"), exit(1);
    else if (pid1 == 0)
    {   
        // reader child
        close(mypipe[1]);
        if (dup2(mypipe[0], STDIN_FILENO) != STDIN_FILENO)
            perror("dup2 error"), exit(1);
        close(mypipe[0]);
        if (execlp("wc", "wc", NULL) < 0)
            perror("execlp1 error"), exit(1);
        else            // pid >0, parent
        {   
            …
        }
    }
    return 0;
}

else if (pid1 == 0)子句由子句执行。它关闭管道的写入端,将读取端复制到标准输入并关闭管道的读取端。然后它会execlp()wc 。仅当代码执行失败时wcelse子句被执行,然后只有管道的读取端保持打开状态。同时,原来的进程就会退出。这会关闭管道,因此 wc命令未收到任何输入,并报告 0 0 0结果。

您需要重写代码。父进程应该等待,直到它的两个子进程都执行。特别是在调试时,您不应该忽略子进程的退出状态,并且应该报告它。

这是一些有效的代码。请注意,它避免了茂密的决策树——它是 if 的线性序列。/else if/…/else代码。一般来说,这比一组复杂的多级条件更容易理解。

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    int fd[2];
    pid_t pid1, pid2;

    if (pipe(fd) < 0)
        perror("pipe error"), exit(1);
    else if ((pid1 = fork()) < 0)
        perror("fork error"), exit(1);
    else if (pid1 == 0)
    {
        /* ls */
        dup2(fd[1], STDOUT_FILENO);
        close(fd[0]);
        close(fd[1]);
        execlp("ls", "ls", (char *)0);
        perror("exec ls");
        exit(1);
    }
    else if ((pid2 = fork()) < 0)
        perror("fork error"), exit(1);
    else if (pid2 == 0)
    {
        /* wc */
        dup2(fd[0], STDIN_FILENO);
        close(fd[0]);
        close(fd[1]);
        execlp("wc", "wc", (char *)0);
        perror("exec wc");
        exit(1);
    }
    else
    {
        close(fd[0]);
        close(fd[1]);
        int status1;
        int status2;
        int corpse1 = waitpid(pid1, &status1, 0);
        int corpse2 = waitpid(pid2, &status2, 0);
        printf("ls: pid = %d, corpse = %d, exit status = 0x%.4X\n", pid1, corpse1, status1);
        printf("ls: pid = %d, corpse = %d, exit status = 0x%.4X\n", pid2, corpse2, status2);
    }
    return 0;
}

程序的示例运行pipe41在我的机器上制作:

$ pipe41
     175     175    1954
ls: pid = 44770, corpse = 44770, exit status = 0x0000
ls: pid = 44771, corpse = 44771, exit status = 0x0000
$ ls | wc
     175     175    1954
$

关于c - 执行 ls | c 程序中的 wc linux 命令使用 2 个进程通过管道进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70690887/

相关文章:

c++ - 如何创建一个指针数组并使用 **P 将其指向 NULL?

linux - 查找重复行之间的平均时间/距离

linux - 在运行特定 vdso 时收到通知

Scala 在不同的工作目录上运行进程

c - 如何在C中制作定时器?

c - 在一个单词中获取字节地址的最安全的跨平台方法是什么?

linux - 通过排除其父文件夹为多个目录创建单个 tar 文件

C# 如何从进程中检索代码然后运行它?

linux - 我如何知道进程的最后安排时间

c++ - 在 Windows 7 中交叉编译 C 和 C++ 应用程序,在 linux 下使用 MinGW