- 论坛徽章:
- 0
|
想实验下printf的缓冲,于是写了下面一小段代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main (int argc, char* argv[])
{
int pipe_fd[2];
if (-1 == pipe(pipe_fd))
{
printf("make pipe error!\n");
exit(1);
}
if (-1 == dup2(pipe_fd[0], STDIN_FILENO) || -1 == dup2(pipe_fd[1], STDOUT_FILENO))
{
printf("redirct error!\n");
exit(1);
}
while ((-1 == close(pipe_fd[0])) && (errno == EINTR));
while ((-1 == close(pipe_fd[1])) && (errno == EINTR));
int i = 1;
int tmp;
for (; i < 10; i++)
{
printf("%d", i);
fflush(stdout); //fflush out to unblock scanf
scanf("%d",&tmp);
fprintf(stderr, "%d\n", tmp);
}
return 1;
}
|
但这段代码运行时还是阻塞在了scanf中,为什么呢? |
|