子进程之间的管道空白行问题

时间:2017-09-24 17:00:09

标签: pipe

我有以下代码。它应该根据命令的数量循环和创建管道。但由于我无法使其正常工作,我目前只使用管道2命令进行测试,每个命令都在单独的进程中运行。问题是底部留有空白链接。我已经读过你必须关闭父母的管道,但我不能让它工作。

int pipeCount = commands- 1;
int fd[pipeCount][2];
createPipes(pipeCount, fd);

for (int i = 0; i < commands; i++) {
    pid_t pid;
    pid = fork();

    if (pid < 0) {
        perror("fork");
        exit(1);
    } else if (pid == 0) {
        dupPipes //This function duplicates the pipes to STDIN and STDOUT & closes the pipes correctly
        execCommand //Executes the commands properly
    } else {
        waitpid(-1, NULL, 0);
    }
}

此代码工作正常,问题是底部是空白行,无论我在哪里尝试关闭父管道。然后程序停止运行,它不能复制子进程中的管道。

INPUT:

  

myShell $ ls | tr a-z A-Z

输出:

stuff
stuff2
<BLANK LINE>

0 个答案:

没有答案
相关问题