neodreamerus 发表于 2015-11-12 15:05

Linux 多线程的问题

多个线程同时在打印输出到stdout,
比如 线程1 打印 ABC,(只用一次write 或 printf)
线程2 打印 abc

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

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

ubuntu_k 发表于 2015-11-12 15:58

失望了,这个也没人回答。你还是自己测试吧,也不难,我也在等大神救命呢

neodreamerus 发表于 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.

大笨蛋才会放弃 发表于 2015-11-19 08:56

是原子的是原子的是原子的

路旁街角 发表于 2015-12-04 15:12

这好像是可重入的问题
页: [1]
查看完整版本: Linux 多线程的问题