The effect of blocking and non-blocking on the partial system calls of Linux files

Source: Internet
Author: User

1. Basic Concepts

The so-called blocking, that is, the kernel will suspend the process if the condition is not met (I/O may need to be generated) when it is called on the file operation I/O system. Non-blocking will return immediately if the discovery condition is not satisfied. Also need to pay attention to the non-blocking is not polling, or it is not much different from blocking, it just call unsuccessful directly return, not to see when will meet the conditions, but have you to choose what to do next, the system is no longer responsible

2.read/write blocking and non-blocking understanding

Read/write system calls, and does not go directly to read and write files, but just to manipulate the memory page of the file (at this time the page is virtual memory), for read if you find the data you want to read and write in the page, copy the data directly from the page to the user cache, if the page to be read is not found, You can only read the page content from disk and cache it in memory. The so-called read process, in fact, the file system to do is to lock the page, and then construct a read request, and send the request to the underlying IO subsystem. The read system call in the Linux kernel is blocked by default because write calls are non-blocking, since write only writes the user-state data to the cache page to return

3. Impact of network sockets blocking/non-blocking on read data (1) in case of blocking

Under blocking conditions, the behavior of READ/RECV/MSGRCV
1. If no data is found in the network buffer will always wait
2. When data is found, the data is read into the user-specified buffer (if the read data is smaller than the specified size, read does not block at this time, but returns immediately)

应为read的原则是在不超过指定长度的时候有多少读多少,没有数据就返回。所以一般情况下,如果我们想要读取我们想要的字节量,就得循环read
(2) In the case of non-blocking

Behavior of read in non-blocking situations
1. If no data is found, return directly
2. If the data is found, then it is how much to read, so after reading it is necessary to determine whether the read operation again, to see the number of bytes we want

对于读的阻塞与非阻塞区别就在于没有数据可读时,是否立即返回
4. Impact on write data for network sockets blocking/non-blocking

The nature of the write operation is not to send the operation, but to copy the user-state data to the bottom of the system, and then by the system to help send. Send,write returns success, only indicates that the data has been copied to the underlying buffer, and does not indicate that the data is being sent out, or that the other port has received data

(1) In case of obstruction

Write will return the data after it has been sent, which is different from reading. When we read the data, we do not know whether the sender has the data to be sent, if the wait can cause a dead loop, so in order to avoid this kind of thing, we read the contents of the current cache, we return, do not care whether read enough we want to read the number of bytes. and write, because the length of the need to write is known, so it will always write enough to specify the bytes to return

(2) In case of non-blocking

In non-blocking cases, it is the strategy of how much you can write, and what is different from reading is how much reading is transmitted by the network to the standard. However, it is standard to write how much is caused by local network congestion, which is how much to write at a time for non-blocking situations, which can cause partial writes

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The effect of blocking and non-blocking on the partial system calls of Linux files

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.