I faced a nasty problem:
Suppose I have one program, let's name it HelloProgram which code is very simple:
void print_bullshit() {
int i;
for (i = 0; i < 10; ++i) {
printf("hello!");
sleep(3);
}
printf("bye!");
}
And I have another program, let's name it Redirector, which code is a bit complicated:
#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;
}
Some error-checks are not stated there, that's to make code more simple. I still can't understand this thing:
When Redirector is used with HelloProgram to redirect it's output, all the 'Hello' text is given to the console screen only after 3 * 10 (== number of iterations) == 30 seconds,
what the hell is that? I supposed it would be some kind of parallel, so I got each 'Hello' string displayed on the console after every 3 second.
Help me, please.