1
问题是,该程序将不会输出预期的START
和END
,除非我将其从shell中删除,否则它也不会退出。Unix管道实验挂起
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
void check(bool b, const char *cp) {
if (!b) {
perror(cp);
exit(EXIT_FAILURE);
}
}
int main(void) {
int fd[2];
check(pipe(fd) != -1, "pipe");
pid_t pid = fork();
check(pid != -1, "fork");
if (pid == 0) {
check(dup2(1, fd[1]) != -1, "dup2");
check(execlp("ls", "ls", NULL) != -1, "execlp");
} else {
char cs[10000];
read(fd[0], cs, sizeof cs);
printf("START\n%s\nEND\n", cs);
}
return EXIT_SUCCESS;
}