尝试将 pipe(2) 与 sort unix 工具一起使用但无法正常工作

trying to use pipe(2) with the sort unix tool but not working

我一直在努力寻找我做错了什么,但我似乎找不到问题所在。当我编译下面的代码时,出现 I/O 错误。

e.g: /usr/bin/sort: read failed: -: Input/output error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


int main(int argc, char **argv, char **envp)
{
        int fd[2];
        pid_t pid;

        pipe(fd);

        pid = fork();
        if (pid == -1) {
                exit(EXIT_FAILURE);
        }

        if (pid == 0) { /* child */
                char *exe[]= { "/usr/bin/sort", NULL };
                close(fd[0]);
                execve("/usr/bin/sort", exe, envp);

        }
        else {
                char *a[] = { "zilda", "andrew", "bartholomeu", NULL };
                int i;

                close(fd[1]);

                for (i = 0; a[i]; i++)
                        printf("%s\n", a[i]);
        }

        return 0;
}

dup2(fd[0], 0) 在 child 中。 parent 中的 dup2(fd[1], 1)。 关闭另一个 fd.