免费注册 查看新帖 |

Chinaunix

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

[系统] Linux 多线程的问题 [复制链接]

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015元宵节徽章
日期:2015-03-06 15:52:30
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-11-12 15:05 |只看该作者 |倒序浏览
多个线程同时在打印输出到stdout,
比如 线程1 打印 ABC,(只用一次write 或 printf)
线程2 打印 abc

最后输出结果会出现AaBbCc的可能吗?

换句话说, write(fd, buf, count) 是原子的吗?

论坛徽章:
0
2 [报告]
发表于 2015-11-12 15:58 |只看该作者
失望了,这个也没人回答。你还是自己测试吧,也不难,我也在等大神救命呢

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015元宵节徽章
日期:2015-03-06 15:52:30
3 [报告]
发表于 2015-11-12 18:08 |只看该作者
按照Posix标准应该是atomic的,但是linux 3.1.4之前不是的

   According to POSIX.1-2008/SUSv4 Section XSI 2.9.7 ("Thread
       Interactions with Regular File Operations"):

           All of the following functions shall be atomic with respect to
           each other in the effects specified in POSIX.1-2008 when they
           operate on regular files or symbolic links: ...

       Among the APIs subsequently listed are write() and writev(2).  And
       among the effects that should be atomic across threads (and
       processes) are updates of the file offset.  However, on Linux before
       version 3.14, this was not the case: if two processes that share an
       open file description (see open(2)) perform a write() (or writev(2))
       at the same time, then the I/O operations were not atomic with
       respect updating the file offset, with the result that the blocks of
       data output by the two processes might (incorrectly) overlap.  This
       problem was fixed in Linux 3.14.

论坛徽章:
0
4 [报告]
发表于 2015-11-19 08:56 |只看该作者
是原子的是原子的是原子的

论坛徽章:
0
5 [报告]
发表于 2015-12-04 15:12 |只看该作者
这好像是可重入的问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP