如果写入系统调用是由2个不同的进程同时呼吁相同的文件,会发生什么 [英] What happens if a write system call is called on same file by 2 different processes simultaneously

查看:112
本文介绍了如果写入系统调用是由2个不同的进程同时呼吁相同的文件,会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否正确操作系统处理呢?

Does the OS handle it correctly?

或将我叫羊群()?

推荐答案

虽然OS不会崩溃,并且文件系统不会被损坏,调用的write()不会保证的广告是原子,除非有关的文件描述符是管道,和数据的写入量 PIPE_MAX 字节或更少。 标准:

Although the OS won't crash, and the filesystem won't be corrupted, calls to write() are NOT guarenteed to be atomic, unless the file descriptor in question is a pipe, and the amount of data to be written is PIPE_MAX bytes or less. The relevant part of the standard:

这是试图写入管道或FIFO有几个主要特点:

An attempt to write to a pipe or FIFO has several major characteristics:


      
  • 原子/非原子:写是原子,如果写在一个操作全量不与其他任何过程数据交错。当有多个作家将数据发送到一个单一的阅读器,这非常有用。应用程序需要知道一个写请求可以是多大预计将原子进行。这个最大称为{PIPE_BUF}。 IEEE标准1003.1-2001的此卷不说无论是写请求超过{} PIPE_BUF字节是原子,但需要的{} PIPE_BUF或更少的字节写入应是原子的。

  •   

[...]

因此​​,在原则上,你必须同时锁定的作家,或者你写入的数据可能会被混淆和乱序(甚至在同一写),或者你可能有多个写互相覆盖。然而,有一个例外 - 如果你通过 O_APPEND ,你写将得到有效的原子:

As such, in principle, you must lock with simultaneous writers, or your written data may get mixed up and out of order (even within the same write) or you may have multiple writes overwriting each other. However, there is an exception - if you pass O_APPEND, your writes will be effectively atomic:

如果文件状态标志的O_APPEND标志被设置,文件偏移应被设置成之前每个写入该文件的结束,没有居间文件修改操作须改变文件偏移和写操作之间发生。

If the O_APPEND flag of the file status flags is set, the file offset shall be set to the end of the file prior to each write and no intervening file modification operation shall occur between changing the file offset and the write operation.

虽然这不是相对于非必要的原子 - O_APPEND 写,或同时读取,如果所有的作家使用 O_APPEND ,你做了,你应该没问题前不知何故同步。

Although this is not necessarily atomic with respect to non-O_APPEND writes, or simultaneous reads, if all writers use O_APPEND, and you synchronize somehow before doing a read, you should be okay.

这篇关于如果写入系统调用是由2个不同的进程同时呼吁相同的文件,会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆