linux process blocking and non-blocking operations

Before we get to read and write methods to achieve full-featured, we reach the last point is to decide when to make the process of sleep. Sometimes unix semantics required for proper operation without blocking, even if it can not fully proceed.

 

Sometimes calling process to inform you that he did not want blocked, regardless of its I / O whether to continue. Definite non-blocking I / O is indicated by filp-> f_flags the O_NONBLOCK flag. This flag is defined in <linux / fcntl.h> , was

<Linux / fs.h> contains an automatic flag named after the "Open - non-blocking.", Because it can be specified (in the open and from

In the beginning there can only be specified) If you browse the source code, you will find some references to a O_NDELAY flag; this is an alternative O_NONBLOCK's name, is compatible with System V code is accepted by default this flag is cleared. , because normal behavior of a process waiting for data only sleep in the case of a blocking operation, which is the default, the following behavior should be implemented to meet the standard syntax:

 

  • If a process calls read but no data is available (yet), this process must be blocked. This process is awakened when data reaches immediately, and the data is returned to the caller, even if less than requested in the count parameter to the method of number.
  • If a process calls write and there is no space in the buffer, this process must be blocked, and it must be in a different queue and used as read in. When some data is written to a hardware device, and

Output buffer space becomes idle, the process is awakened and the write call succeeds, although the data may only be partially written only if there is no space in the buffer to count bytes requested.

 

This assumes that there are two input and output buffer; Indeed, nearly every device driver has an input buffer is required in order to avoid loss of data arriving when no contrast, when writing data is not lost when reading, because. If the system call

 

Can not receive a data byte, they remain in the user space of the buffer. Even so, the output buffer almost always useful to squeeze more performance from the hardware.

 

Output buffer implemented in the driving performance as obtained from reducing the number of context switches and user-level / kernel-level switching. Not an output buffer (assuming a slow devices), each system call receiving one or several of such character, and when a write process in sleep, another process to run (it was a context switch). when the first process is awakened, it resumes (another context switch), write returns (kernel / user conversion), and this process again issuing a system call to write the additional data (user / kernel conversion); this call blocks and the cycle continues to increase output buffer may allow a drive receiving large block of data in each write call, there is a corresponding increase in performance. If this buffer is large enough to write in the first call attempt is successful - after being buffered data will be pushed to the device - does not have to control the user needs to return space to write the second or third call to select a suitable value to the output buffer is clearly device specific.

 

We do not use an input buffer in the scull, since the data when issuing read already available. Similarly, no output buffer, since the data is simply copied to the device association and memory areas. Essentially, this device is a buffer, so implement additional buffer may be redundant. we will see the use of a buffer in Chapter 10.

 

If you specify O_NONBLOCK, read and write behavior is different. In this case, the call simply returns -EAGAIN (( "try it agin") If a process calls read when no data is available, or if and when there is no buffer it calls write when space.

 

As you might expect, non-blocking operations return immediately, to allow this application polling data applications when using the stdio functions handle non-blocking file, you must be careful, because they are easy to mistake a non-blocking returns EOF. They always you must check errno.

 

Naturally, O_NONBLOCK also open meaningful this method occurs when this call really blocked for a long time; for example, when you open (as read access) a person did not write (yet) FIFO, or to access a disk file using a suspension lock. often, open a device or success or failure, there is no need to wait for external events. sometimes, however, open the device requires a long initialization, and you may choose to support O_NONBLOCK in your open method, by returning -EAGAIN immediately, if this flag is set. after the start of the initialization process equipment. the driver may also implement a blocking open to support access strategy, by way similar to file locks and we will see such a realization in "blocking open as an alternative to EBUSY" section, later in this chapter.

 

Some drivers may also implement special semantics for O_NONBLOCK; for example, a tape device is often open blocked until insert a tape if the tape drive using the O_NONBLOCK open, this open immediately successful, regardless of whether or not the media.

 

Only read, write, and open file operations by non-blocking flag is affected.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11141836.html