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

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

Does the OS handle it correctly?

Or will I have to call flock()?

5条回答
  •  佛祖请我去吃肉
    2020-12-17 06:11

    Yes of course it will work correctly. It won't crash the OS or the process.

    Whether it makes any sense, depends on the way the application(s) are written an what the file's purpose is.

    If the file is opened by all processes as append-only, each process (notionally) does an atomic seek-to-end before each write; these are guaranteed not to overwrite each others' data (but of course, the order is nondeterministic).

    In any case, if you use a library which potentially splits a single logical write into several write syscalls, expect trouble.

提交回复
热议问题