【发布时间】:2016-02-02 20:07:03
【问题描述】:
问题是这个程序不会输出预期的START和END,也不会退出,除非我从shell中杀死它。
#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;
}
【问题讨论】: