Non-blocking I/O of advanced I/O

Source: Internet
Author: User

Bytes. Low-speed system calls are a type of system calls that may cause the process to be blocked forever. They include the following calls:

  • If the data of some file types (such as pipelines, terminal devices, and network devices) does not exist, the read operation may block the caller forever.
  • If the data cannot be immediately accepted by the same type of files (because there is no space in the pipeline, network flow control, etc.), the write operation will also cause the caller to block forever.
  • Before a certain condition occurs, opening some types of files will be blocked (for example, opening a terminal device may have to wait until the modem that is connected to it responds; for example, if no other process has enabled the FIFO in read-only mode, wait ).
  • Read and Write the files with the mandatory record lock added.
  • Some ioctl operations.
  • Some inter-process communication functions.

Although reading and writing disk files can block callers for a short period of time, system calls related to disk I/O cannot be considered as "Low Speed ".

Non-blocking I/O allows us to call I/O operations such as open, read, and write, so that these operations will not be blocked forever. If this operation cannot be completed, an error is returned immediately after the call, indicating that the operation will be blocked if it continues to be executed.

There are two ways to specify a non-blocking I/O for a given descriptor:

(1)If you call open to obtain the descriptor, you can specify the O_NONBLOCK flag.Http://www.cnblogs.com/nufangrensheng/p/3497789.html ).

(2)For an opened descriptor, you can call fcntl to enable the O_NONBLOCK File status flag.(Bytes ).

Early versions of System V use the flag O_NDELAY to specify the non-blocking mode. In these versions, if no data is readable, the read return value is 0. The UNIX system often interprets the read return value 0 as the end of the file, and the two are confused. Therefore, POSIX.1 provides a non-blocking sign whose names and semantics are different from those of O_NDELAY. Indeed, in earlier versions of System V, when the returned value is 0 from read, we do not know whether the call is blocked or has reached the end of the file. POSIX.1 requires that if no data is readable for a non-blocking descriptor, read returns-1 and errno is set to EAGAIN. Some platforms derived from System V Support the old O_NDELAY and POSIX.1 O_NONBLOCK. O_NDELAY is only intended for backward compatibility and should not be used in new applications.

4.3BSD provides FNDELAY for fcntl, and its semantics is slightly different. It not only affects the File status mark of the descriptor, but also changes the identifier of the terminal device or socket to non-blocking. Therefore, it not only affects users who share the same file table, it also serves all users of the terminal or socket. In addition, if the operation on a non-blocking descriptor cannot be completed without interruption, 4.3BSD returns EWOULDBLOCK. Currently, the BSD-based system provides the O_NONBLOCK flag of POSIX.1 and defines EWOULDBLOCK as the same as the EAGAIN of POSIX.1. These systems provide non-blocking semantics consistent with other POSIX-compliant systems. Changes to the file status flag affect all users of the same file table item, but are not related to access to the same device through other file table items (see Figure 3-1 http: // response ).

Instance

Program listing 14-1 is a non-blocking I/O instance that reads 500 000 bytes from the standard input and tries to write them to the standard output. The program first sets the standard output to non-blocking, and then outputs it using the for loop. The results of each write call are printed on the standard error. The function clr-fl is similar to set_fl in program listing 3-5 (http://www.cnblogs.com/nufangrensheng/p/3500350.html), but unlike set_fl, it clears one or more flags.

Program list 14-1 long non-blocking write

#include <errno.h><fcntl.h>     buf[  set_fl(,   clr_fl(,     *= read(STDIN_FILENO, buf, =(ntowrite > = =(nwrite > +=-=

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.