Linux File open mode

Source: Internet
Author: User

 File Openint Open (const char *Pathname, int Flags, mode_t Mode);
Normal mode (Canonical mode)O_sync and O_direct are not set in flags.

In this way, read is plugged in (blocking call), and is returned when the disk data is read, and write is a blocking call, except that write writes data to the page cache. is returned, no action is written to the disk. The kernel writes the data from page cache back to disk based on a certain policy.
The synchronization mode (synchronous mode) sets the O_sync.

The write operation differs from the normal method in the read operation.

The write operation waits for the data to be completely written to disk after it is returned. Write operations in this mode will have a large performance reduction. However, it improves the reliability of the system and does not result in data loss.
Direct I/O mode settings o_direct.

In this way, the read and write operations are no longer passed through the page cache, but are read and written directly to the disk. Some applications need to maintain their own caches and can use this pattern, such as a database system.

non-clogging mode (nonblocking I/O) settings o_nonblock. The mode. Read and write are non-clogging (nonblocking) modes.

If there is no data available in the system, read returns immediately instead of waiting; Assuming that the data cannot be sent immediately, it is returned instantly.


This mode is generally used in conjunction with some kind of I/O event notification mechanism, such as SELECT, poll, Epoll, etc.
The memory-mapped pattern (mapping mode) is opened by void *mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset).
asynchronous modes (asynchronous mode) The POSIX standard defines the interface for file asynchronous access mode (Aio_read,aio_write, etc.).

Async mode. Read/write will have the required information, such as a file. memory address, etc.), and return immediately without waiting for the actual IO operation. After the actual IO is complete. There will be an event notification. This mode is similar to the DMA of a hardware device. Only the IO instructions are issued, and the actual IO operation is completed asynchronously.
socket Read and write clogging mode recv: Blocked until data is received in the network buffer. send: Assuming that the network buffer has enough space, send copies the data back to the buffer. Assume that there is not enough space (such as network congestion control, the other party does not confirm the previous message, etc.), blocked.



non-clogging mode recv: Assume that there is data received in the network buffer and return immediately. The return value Eagain or Ewouldblock.

Send : assume that there is enough space in the network buffer. Send returns after the data has been copied to the buffer. Suppose there is not enough space. Returns eagain or ewouldblock.
The send operation succeeded. Does not mean that the data has been sent to the receiver, just means that the data has been placed in the network buffer.


Different opening methods have different application scenarios. In the choice of time must be based on actual needs to determine. Webserver that support high concurrency will use non-clogging mode to handle sockets.
Linux is the most common way to start, and other ways must be added gradually in order to handle a variety of usage scenarios.

Of course, not to rule out some of Daniel's brains.

Linux File open mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.