1
我正在玩管道,并从here采取以下代码;一旦我明白了,我看到一个数据块缓存的问题,我添加了一个电话到sleep()
,这是不存在的原始代码:冲洗管道的缓冲区
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
FILE *ps_pipe;
FILE *grep_pipe;
int bytes_read;
int nbytes = 100;
char *my_string;
char buffer[100];
/* Open our two pipes */
ps_pipe = popen ("ls", "r");
grep_pipe = popen ("sort", "w");
/* Check that pipes are non-null, therefore open */
if ((!ps_pipe) || (!grep_pipe))
{
fprintf (stderr,
"One or both pipes failed.\n");
return EXIT_FAILURE;
}
bytes_read = 0;
while (fgets(buffer, sizeof(buffer), ps_pipe))
{
fprintf(grep_pipe, "%s", buffer);
bytes_read += strlen(buffer);
}
printf("Total bytes read = %d\n", bytes_read);
sleep(2);
/* Close ps_pipe, checking for errors */
if (pclose(ps_pipe) != 0)
{
fprintf(stderr, "Could not run 'ps', or other error.\n");
}
/* Close grep_pipe, cehcking for errors */
if (pclose(grep_pipe) != 0)
{
fprintf(stderr, "Could not run 'grep', or other error.\n");
} /* Exit! */
return 0;
}
EDIT [这是错的,见下面的回答]:这样,一旦程序从其主函数返回,我确信管道的缓冲区被刷新。
但是,我仍然不明白原因:为什么管道的内核缓冲区会刷新到标准输出?前者与后者有什么关系? [编辑:这也是错误的,但留给上下文]
谢谢你澄清它。 – HeyJude