我遇到了一个棘手的问题:
假设我有一个程序,我们把它命名为 HelloProgram 代码很简单:
void print_bullshit() {
int i;
for (i = 0; i < 10; ++i) {
printf("hello!");
sleep(3);
}
printf("bye!");
}
而且我还有一个程序,我们把它命名为Redirector,代码有点复杂:
#define READ_END 0
#define WRITE_END 1
int pipe_fd[2];
void child_proc(const char* prg_name) {
close(pipe_fd[READ_END]);
dup2(pipe_fd[WRITE_END], STDOUT_FILENO));
execl("/bin/sh", "sh", "-c", prg_name, NULL);
//error
exit(1);
}
void parent_proc() {
close(pipe_fd[WRITE_END]);
char buffer[256];
while (read(pipe_fd[READ_END], buffer, sizeof(buffer)) > 0) {
printf(buffer);
}
wait(NULL);
}
int main(int argc, const char* argv[]) {
pipe(pipe_fd);
pid_t chpid = fork();
if (chpid != 0) {
parent_proc();
} else {
child_proc(argv[1]);
}
return 0;
}
那里没有说明一些错误检查,这是为了使代码更简单。我仍然无法理解这件事:
当 Redirector 与 HelloProgram 一起使用以重定向其输出时,所有“Hello”文本仅在 3 * 10(== 迭代次数)== 30 秒后才会显示到控制台屏幕,
这他妈到底是什么?我想它会是某种并行的,所以我每 3 秒后在控制台上显示每个“Hello”字符串。
请帮帮我。