What happens if a write system call is called on same file by 2 different processes simultaneously

前端 未结 5 1145
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 05:53

Does the OS handle it correctly?

Or will I have to call flock()?

5条回答
  •  抹茶落季
    2020-12-17 06:04

    write (and writev, too) guarantee atomicity.

    Which means if two threads or processes write simultaneously, you do not have a guarantee which one writes first. But you do have the guarantee that anything that is in one syscall will not be intermingled with data from the other one.

    Insofar it will always work correctly, but not necessarily in the way you expect (if you assume that process A comes before process B).

提交回复
热议问题