//标准I/O是缓冲的,如果标准输出到终端设备,则它是⾏行缓冲,否则它是全
//缓冲
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
const char * msg ="hehe,ni shi ge hao ren \n";
int g_val = 0;
int main()
{
int val = 0;
printf("before fork:\n");
//本来是行缓冲,但是重定向的时候变为全缓存,所以当他被重定向到文件中
//before fork 会被打印两次
write(1,msg,strlen(msg)); //wu huan chong
pid_t id = fork();
if(id < 0)
{
// perror("fork");
printf("%d %s \n",errno,strerror(errno));
exit(1);
}
else if(id == 0)
{//child
printf("i am child pid:%d ppid:%d\n",getpid(),getppid());
val ++;
g_val++;
printf("chirld:%d %d %p %p \n",val,g_val,&val,&g_val);
}
else
{//father
printf("i am father pid:%d ppid:%d\n",getpid(),getppid());
printf("father:%d %d %p %p \n",val,g_val,&val,&g_val);
sleep(1);
}
return 0;
}
关于重定向和缓存区
进程与缓冲输出
最新推荐文章于 2025-06-12 00:05:58 发布
本文探讨了标准I/O缓冲机制,并通过一个C语言示例程序演示了进程创建与标准输出缓冲行为之间的交互作用。该程序使用fork()创建子进程并打印变量前后的变化,展示了行缓冲与全缓冲的区别。
848

被折叠的 条评论
为什么被折叠?



