免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1671 | 回复: 1
打印 上一主题 下一主题

多进程COW问题求教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-16 16:31 |显示全部楼层 |倒序浏览
本帖最后由 ppm10103 于 2012-03-16 16:34 编辑


#include "apue.h"
#include "apueerror.h"

//./a.out > temp.out 会产生两次before fork
int glob = 6; /* external variable in initialized data */
char buf[] = "a write to stdout\n";
int main(void) {
    int var; /* automatic variable on the stack */
    pid_t pid;
    var = 88;
    if (write(STDOUT_FILENO, buf, sizeof(buf) - 1) != sizeof(buf) - 1)
        err_sys("write error");
    printf("before fork\n"); /* we don't flush stdout */
    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) { /* child */
        glob++; /* modify variables */
        var++;
    } else {
        sleep(2); /* parent */
    }
    printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
    exit(0);
}







为什么
./a.out 只会产生一次 before fork
./a.out > temp.out 会产生两次before fork   

为什么重定向才会有两次呢?

源代码在附件中

sourcecode.rar

2.7 KB, 下载次数: 4

程序源码

论坛徽章:
0
2 [报告]
发表于 2012-03-16 17:31 |显示全部楼层
多谢指点了,就是因为没想明白为什么文本是全缓冲。

现在知道了,块设备是全缓冲。

不同设备的缓冲策略不同
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP