Talk about Linux five IO models

Source: Internet
Author: User
Tags epoll readable

1 Concept Description

Before interpreting, there are a few concepts to be explained:

User space and kernel space

Process switching

Blocking of processes

File descriptor

Cache IO

1.1 User space and kernel space

Now that the operating system is using virtual memory, the 32-bit operating system, its addressing space (virtual storage space) is 4G (2 of 32). 操作系统的核心是内核,独立于普通的应用程序,可以访问受保护的内存空间,也有访问底层硬件设备的所有权限. In order to ensure that the user process can not directly manipulate the kernel (kernel), to ensure the security of the kernel 操作系统将虚拟空间划分为两部分,一部分为内核空间,一部分为用户空间 . For the Linux operating system, the highest 1G bytes (from the virtual address 0xc0000000 to 0xFFFFFFFF) for the kernel to use, called the kernel space, and the lower 3G bytes (from the virtual address 0x00000000 to 0xBFFFFFFF) for each process to use, Called User space.

1.2 Process switching

To control the execution of a process, the kernel must have the ability to suspend a process that is running on the CPU and resume execution of a previously suspended process. This behavior is called process switching. So it can be said that any process that runs under the support of the operating system kernel is closely related to the kernel.

The process of moving from one process to another runs through the following changes:

  1. Save the processor context, including program counters and other registers.

  2. Update PCB information.

  3. The PCB of the process is moved into the appropriate queue, such as ready, in an event blocking queue.

  4. Select another process to execute and update its PCB.

  5. Update the data structure of memory management.

  6. Restore the processing machine context.

Note: In short, it is very resource-intensive, specific reference to this article: process switching.

1.3 Blocking of the process

The executing process, because some expected events did not occur, such as requesting system resources failed, waiting for the completion of an operation, new data has not arrived or no new work to do, etc., the system automatically executes the blocking primitive (block), making itself from the running state into a blocking state. It can be seen that the blocking of a process is an active behavior of the process itself, and therefore it is possible to turn it into a blocking state only if the process is in a running state (acquiring the CPU). 当进程进入阻塞状态,是不占用CPU资源的.

1.4 File Descriptor FD

File descriptor is a term in computer science 是一个用于表述指向文件的引用的抽象化概念 .

The file descriptor is formally a non-negative integer. In fact, 它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表 . When a program opens an existing file or creates a new file, the kernel returns a file descriptor to the process. In programming, some of the underlying programming often revolves around file descriptors. However, the concept of file descriptors is often applied only to operating systems such as UNIX and Linux.

1.5 Cache IO

缓存 IO 又被称作标准 IO,大多数文件系统的默认 IO 操作都是缓存 IO。 In the Linux cache IO mechanism, the operating system caches the IO data in the file system's page cache, that is, 数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间 .

Disadvantages of Cache IO:

数据在传输过程中需要在应用程序地址空间和内核进行多次数据拷贝操作, the CPU and memory overhead associated with these data copy operations are very large.

2 Linux IO Model

网络IO的本质是socket的读取,socket在linux系统被抽象为流,IO可以理解为对流的操作。 Just now, for an IO access (read for example), 数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间 . So, when a read operation occurs, it goes through two stages:

  1. First stage: Waiting for data preparation (waiting for the.

  2. Phase two: Copy the data from the kernel into the process (Copying the data from the kernel-the process).

For the socket stream,

  1. The first step: usually involves waiting for a packet of data on the network to arrive and then being copied to a buffer in the kernel.

  2. Step Two: Copy the data from the kernel buffer to the application process buffer.

Network applications need to deal with nothing more than two major types of problems 网络IO,数据计算 . Compared with the latter, the latency of network IO brings the performance bottleneck to the application more than the latter. The network IO model is roughly as follows:

  • Synchronization model (synchronous IO)

    • Blocking io (bloking io)
    • Non-blocking IO (non-blocking io)
    • Multiplexed io (multiplexing io)
    • Signal-driven IO (signal-driven io)
  • Asynchronous IO (Asynchronous IO)

注:由于signal driven IO在实际中并不常用,所以我这只提及剩下的四种IO Model。

Before going into the various models of Linux IO, let's explore the simple matrix of the basic Linux IO model. As shown in the following:


Enter a picture description

Each IO model has its own usage pattern, which has its own advantages for a particular application. This section will briefly describe each of them. 常见的IO模型有阻塞、非阻塞、IO多路复用,异步. Illustrate these four concepts in a vivid example. I went shopping with my girlfriend at the weekend, I was hungry at noon, we were ready to go to dinner. Weekend people, eat need to queue, my girlfriend and I have the following options.

2.1 Synchronous Blocking IO (blocking IO) 2.1.1 Scenario Description

My girlfriend and I after the meal, do not know when to do a good job, had to sit in the restaurant and so on, until finished, and then left to eat. My girlfriend would like to go shopping with me, but I do not know when the meal can be done, and I have to and I together in the restaurant and so on, and can not go shopping, until after eating dinner before going shopping, in the middle of waiting for the cooking time wasted. 这就是典型的阻塞.

2.1.2 Network Model

同步阻塞 IO 模型是最常用的一个模型,也是最简单的模型。 In Linux, the 默认情况下所有的socket都是blocking . It conforms to the most common thinking logic of people. 阻塞就是进程 "被" 休息, CPU处理其它进程去了.

In this IO model, the user-space application executes a system call (Recvform), which causes the application to block and do nothing until the data is ready, and the data is copied from the kernel to the user process, and the final process processes the data, 在等待数据到处理数据的两个阶段 and the entire process is blocked. Cannot process other network IO. 调用应用程序处于一种不再消费 CPU 而只是简单等待响应的状态, so from a processing point of view, this is very effective. When you call the recv ()/recvfrom () function, the process of waiting for data and replicating data in the kernel, such as:


Enter picture Description 2.1.3 Process description

When the user process calls recv ()/recvfrom () This system call, kernel就开始了IO的第一个阶段:准备数据 (for network IO, many times the data has not arrived at the beginning.) For example, you have not received a full UDP packet. This time kernel will have to wait for enough data to arrive. This process needs to wait, which means that the data is copied into the buffer of the operating system kernel, which requires a process. On this side of the user process, the entire process is blocked (of course, by the process's own choice of blocking). 第二个阶段:当kernel一直等到数据准备好了,它就会将数据从kernel中拷贝到用户内存, and then kernel returns the result, the user process unlocks the block state and re-runs.

Therefore, the blocking IO is characterized by block in both phases of IO execution.

Advantages:

  1. Be able to return data in time without delay;

  2. This is a hassle for the kernel developers;

Disadvantages:

  1. For the user is waiting to pay the cost of performance;
2.2 Synchronous non-blocking IO (nonblocking IO) 2.2.1 Scenario Description

My girlfriend is not willing to wait in vain, and want to go shopping malls, but also worried about the food good. So we stroll for a while, come back to ask the waiter meal good no, to go back and forth several times, the meal has not eaten is almost exhausted. 这就是非阻塞. Need constant questioning, are you ready?

2.2.2 Network model

同步非阻塞就是 “每隔一会儿瞄一眼进度条” 的轮询(polling)方式。 In this model, the 设备是以非阻塞的形式打开的 . This means that the IO operation is not completed immediately, and the read operation may return an error code stating that the command is not immediately satisfied (Eagain or Ewouldblock).

In the network IO, the non-blocking IO will also make recvform system calls, check whether the data is ready, and blocking IO is not the same, "non-blocking the large block of time is divided into more than n small blocking, so the process has a chance to ' be ' CPU patronage."

也就是说非阻塞的recvform系统调用调用之后,进程并没有被阻塞,内核马上返回给进程,如果数据还没准备好,此时会返回一个error。 After the process returns, it can do something else before initiating the recvform system call. Repeat the above process and iterate through the recvform system calls. 这个过程通常被称之为轮询. Poll the kernel data until the data is ready, and then copy the data to the process for data processing. 需要注意,拷贝数据整个过程,进程仍然是属于阻塞的状态 .

Under Linux, you can make it non-blocking by setting the socket. 当对一个non-blocking socket执行读操作时, Process:


Enter picture Description 2.2.3 Process description

When the user process issues a read operation, if the data in the kernel is not yet ready, it does not block the user process, but returns an error immediately. From the user process point of view, it initiates a read operation and does not need to wait, but immediately gets a result. When the user process determines that the result is an error, it knows that the data is not ready, so it can send the read operation again. Once the data in the kernel is ready and again receives the system call of the user process, it immediately copies the data to the user's memory and then returns.

Therefore, nonblocking IO is characterized by the user process needs to constantly proactively ask kernel data well no.

Synchronous non-blocking mode compared to synchronous blocking mode:

Pros: Ability to do other work while waiting for the task to complete (including submitting other tasks, i.e. "backstage" can have multiple tasks at the same time).

Disadvantage: The response delay for task completion is increased because each time a read operation is polled, the task may be completed at any time between polling two times. This can result in a decrease in overall data throughput.

2.3 io multiplexing (IO multiplexing) 2.3.1 Scenario Description

Similar to the second scenario, the restaurant installed an electronic screen to display the status of the order, so that my girlfriend and I go shopping for a while, come back without having to ask the waiter, directly to see the electronic screen on it. So everyone's meal is good, all directly to see the electronic screen on it, 这就是典型的IO多路复用 .

2.3.2 Network Model

Because synchronous non-blocking mode requires constant active polling, polling takes up a large part of the process, polling consumes a lot of CPU time, and "backstage" may have multiple tasks at the same time, people think of the loop to query the completion status of multiple tasks, as long as any one task is completed, to deal with it. If polling is not the user state of the process, it is good to have someone help. 那么这就是所谓的 “IO 多路复用”. Unix/linux The Select, poll, Epoll is to do this (epoll than poll, select high efficiency, do the same thing).

IO多路复用有两个特别的系统调用select、poll、epoll函数。 Select calls are kernel-level, the difference between a select poll relative non-blocking polling is--- 前者可以等待多个socket,能实现同时对多个IO端口进行监听 , when any one of the sockets is in the right data 就能返回进行可读 然后进程再进行recvform系统调用,将数据由内核拷贝到用户进程,当然这个过程是阻塞的 . After a select or poll call, the process is blocked, unlike the blocking IO blocking 此时的select不是等到socket数据全部到达再处理, 而是有了一部分数据就会调用用户进程来处理 . How do you know if some of the data arrives? 监视的事情交给了内核,内核负责数据到达的处理。也可以理解为"非阻塞"吧.

I/O复用模型会用到select、poll、epoll函数,这几个函数也会使进程阻塞,但是和阻塞I/O所不同的的,这两个函数可以同时阻塞多个I/O操作。 I/O functions can be detected at the same time for multiple read operations and multiple write operations, until there is data readable or writable (note that not all data is readable or writable) before actually invoking the I/O operation function.

For multiplexing, that is, polling multiple sockets. 多路复用既然可以处理多个IO,也就带来了新的问题,多个IO之间的顺序变得不确定了, of course, can also be used for different numbers. The specific process, as shown in:


Enter picture Description 2.3.3 Process description

Io Multiplexing is what we call Select,poll,epoll, and in some places this IO mode is the event driven IO. select/epoll的好处就在于单个process就可以同时处理多个网络连接的IO. The basic principle of the select,poll,epoll is that the function will constantly poll all sockets that are responsible, and when a socket has data arrives, notifies the user of the process.

当用户进程调用了select,那么整个进程会被block, and at the same time, kernel will "monitor" all select-Responsible sockets 当任何一个socket中的数据准备好了,select就会返回 . This time the user process then invokes the read operation, copying the data from the kernel to the user process.

Multiplexing is characterized by the 通过一种机制一个进程能同时等待IO文件描述符 kernel monitoring of these file descriptors (socket descriptors), either of which goes into a read-ready state, select, the Poll,epoll function can be returned. For the way of monitoring, can be divided into Select, poll, Epoll three ways.

The graph above and blocking IO are not very different, in fact, worse. 因为这里需要使用两个system call (select 和 recvfrom),而blocking IO只调用了一个system call (recvfrom). However, the 用select的优势在于它可以同时处理多个connection .

Therefore, if the number of connections processed is not high, Web server using Select/epoll does not necessarily perform better than the Web server using multi-threading + blocking IO, and may be more delayed. (The advantage of Select/epoll is not that a single connection can be processed faster, but that it can handle more connections.) )

In the IO multiplexing model, 实际中,对于每一个socket,一般都设置成为non-blocking However, as shown, the entire user's process is in fact always block. 只不过process是被select这个函数block,而不是被socket IO给block. SoIO多路复用是阻塞在select,epoll这样的系统调用之上,而没有阻塞在真正的I/O系统调用如recvfrom之上。

During the I/O programming process, the 当需要同时处理多个客户端接入请求时,可以利用多线程或者I/O多路复用技术进行处理 . I/O multiplexing technology 通过把多个I/O的阻塞复用到同一个select的阻塞上,从而使得系统在单线程的情况下可以同时处理多个客户端请求 . Compared with the traditional multi-threaded/multi-process model, the I/O多路复用的最大优势是系统开销小 system does not need to create new additional processes or threads, and does not need to maintain the operation of these processes and threads, lowering the system maintenance workload, saving system resources, the main application scenarios of I/O multiplexing are as follows:

The server needs to handle multiple sockets at the same time, either in the listening state or multiple connection states.

The server requires a socket that handles multiple network protocols at the same time.

Understanding the previous three IO modes, when 他们在等待数据到来的时候,处理的方式不一样,直接等待,轮询,select或poll轮询 the user process makes system calls, the two-phase process:

The first stage has some blocking, some do not block, some can block and can not block.

The second stage is blocked.

从整个IO过程来看,他们都是顺序执行的,因此可以归为同步模型(synchronous)。都是进程主动等待且向内核检查状态。【此句很重要!!!】

高并发的程序一般使用同步非阻塞方式而非多线程 + 同步阻塞方式。 To understand this, the first thing to do is to pull the difference between concurrency and parallelism. For example, to a department to do business needs to go to several windows, 办事大厅里的人数就是并发数,而窗口个数就是并行度 . 并发数是指同时进行的任务数(如同时服务的 HTTP 请求)that is to say, and 并行数是可以同时工作的物理资源数量(如 CPU 核数) . By reasonably dispatching different stages of a task, the number of concurrent numbers can be far greater than the degree of parallelism, which is the mystery of just a few CPUs that can support tens of thousands of concurrent requests. In this high-concurrency scenario, it is very expensive to create a process or thread for each task (user request). 而同步非阻塞方式可以把多个 IO 请求丢到后台去,这就可以在一个进程里服务大量的并发 IO 请求.

Note: IO Multiplexing is a synchronous-blocking or asynchronous-blocking model, which is analyzed here:

It's still unclear, and it's highly recommended that you talk about the fundamental difference between synchronous and asynchronous in scrutiny, asynchronous, blocking, and non-blocking 同步是需要主动等待消息通知,而异步则是被动接收消息通知,通过回调、通知、状态等方式来被动获取消息 . IO多路复用在阻塞到select阶段时,用户进程是主动等待并调用select函数获取数据就绪状态消息,并且其进程状态为阻塞. So, 把IO多路复用归为同步阻塞模式 .

2.4 Signal-driven IO (signal-driven io)

Signal-driven I/O: First we allow the socket to signal-drive IO and install a signal processing function, and the process continues to run without blocking. When the data is ready, the process receives a sigio signal that can be called by the I/O operation function in the signal processing function to process the data. The process is as follows:


Input Picture Description 2.5 Asynchronous non-blocking IO (asynchronous IO) 2.5.1 Scenario Description

Girlfriend do not want to shop, and restaurant too noisy, go home and have a good rest. So we call takeout, make a phone call to order, and then my girlfriend and I can have a good rest at home, the food is good delivery staff to send to the home. This is the typical asynchronous, just need to make a phone call, and then can do their own things, the meal is ready to send.

2.5.2 Network Model

Asynchronous IO is not sequential execution, relative to synchronous IO. 用户进程进行aio_read系统调用之后,无论内核数据是否准备好,都会直接返回给用户进程,然后用户态进程可以去做别的事情. When the socket data is ready, the kernel copies the data directly to the process 然后从内核向进程发送通知 . IO两个阶段,进程都是非阻塞的.

Linux provides an AIO library function for asynchronous implementations, but with little use. There are many open-source asynchronous IO libraries, such as Libevent, Libev, LIBUV, and so on. The asynchronous process looks like this:


Enter picture Description 2.5.3 Process description

After the user process initiates the aio_read operation, you can begin to do other things immediately. And on the other hand, from kernel's perspective, when it receives a asynchronous read, 首先它会立刻返回,所以不会对用户进程产生任何block . Then, kernel waits for the data to be ready and then copies the data to the user's memory, 当这一切都完成之后,kernel会给用户进程发送一个signal或执行一个基于线程的回调函数来完成这次 IO 处理过程 telling it that the read operation is complete.

In Linux, the way to notify is "signal":

如果这个进程正在用户态忙着做别的事(例如在计算两个矩阵的乘积),那就强行打断之,调用事先注册的信号处理函数, this function can determine when and how to handle this asynchronous task. Since the signal processing function is suddenly broken in, as with the interrupt handler, there are a lot of things that can't be done, so be on the safe side 一般是把事件 “登记” 一下放进队列,然后返回该进程原来在做的事 .

如果这个进程正在内核态忙着做别的事, such as reading and writing disks in a synchronous blocking manner 那就只好把这个通知挂起来了,等到内核态的事情忙完了,快要回到用户态的时候,再触发信号通知 .

如果这个进程现在被挂起了,例如无事可做 sleep 了,那就把这个进程唤醒, the next time the CPU is idle, it will dispatch to this process, triggering signal notification.

The asynchronous API is lightweight and hard to do, mainly for the implementation of the API. Asynchronous IO (AIO) support for Linux was introduced by 2.6.22, and many system calls do not support asynchronous IO. The asynchronous IO for Linux was originally designed for the database 因此通过异步 IO 的读写操作不会被缓存或缓冲,这就无法利用操作系统的缓存与缓冲机制 .

很多人把 Linux 的 O_NONBLOCK 认为是异步方式,但事实上这是前面讲的同步非阻塞方式。It should be noted that although the IO API on Linux is slightly coarser, each programming framework has a encapsulated asynchronous IO implementation. Operating system less work, more freedom to the user, it is UNIX design philosophy, but also the Linux programming framework for a flourishing of the reason.

From the classification of the preceding IO model, we can see the motive of AIO:

The synchronous blocking model needs to block the application at the start of the IO operation. This means that processing and IO operations cannot overlap at the same time.

Synchronous nonblocking models allow processing and IO operations to overlap, but this requires the application to check the status of the IO operation based on the recurring rules.

This leaves asynchronous nonblocking io, which allows processing and IO operations to overlap, including notification of completion of IO operations.

Io multiplexing requires more than blocking select 函数所提供的功能(异步阻塞 IO)与 AIO 类似 . However, 它是对通知事件进行阻塞,而不是对 IO 调用进行阻塞 .

2.6 About asynchronous blocking

Sometimes our API only provides asynchronous notification methods, such as in node. js, 但业务逻辑需要的是做完一件事后做另一件事 such as when a database connection is initialized before it can start accepting HTTP requests from users. 这样的业务逻辑就需要调用者是以阻塞方式来工作.

为了在异步环境里模拟 “顺序执行” 的效果,就需要把同步代码转换成异步形式,这称为 CPS(Continuation Passing Style)变换。 The Continuation.js Library of Byvoid is a CPS transformation tool. 用户只需用比较符合人类常理的同步方式书写代码,CPS 变换器会把它转换成层层嵌套的异步回调形式.


Enter a picture description
Enter a picture description

另外一种使用阻塞方式的理由是降低响应延迟。 If the non-blocking way, a task A is submitted to the background, start to do another thing B, but B has not finished, A is finished, this time to let A's completion of the event is processed as soon as possible (for example, A is an emergency), or discard to do half of B, or save the middle State of b and switch back to A, Time (either from disk to memory or from memory to cache), which is bound to slow the response of a. 因此,对实时系统或者延迟敏感的事务,有时采用阻塞方式比非阻塞方式更好.

35 Types of IO Models summarize 3.1 blocking and non-blocking differences

Calling blocking IO will block the corresponding process until the operation is complete, and non-blocking IO will return immediately when the kernel is ready for the data.

3.2 Synchronous IO and asynchronous IO differences

Before explaining the difference between synchronous IO and asynchronous IO, you need to give a definition of both. The definition of POSIX is this:

A synchronous I/O operation causes the requesting process to being blocked until that I/O operation completes;

An asynchronous I/O operation does not cause the requesting process to be blocked;

两者的区别就在于synchronous IO做”IO operation”的时候会将process阻塞。 According to this definition, the blocking io,non-blocking Io,io Multiplexing described previously are synchronous IO.

Some people will say, non-blocking io is not block AH. Here's a very "tricky" place, 定义中所指的”IO operation”是指真实的IO操作 in the example of Recvfrom, the system call. Non-blocking IO does not block the process when it executes recvfrom this system call if the kernel data is not ready. However, 当kernel中数据准备好的时候,recvfrom会将数据从kernel拷贝到用户内存中,这个时候进程是被block了 during this period, the process is blocked.

The asynchronous IO is not the same 当进程发起IO 操作之后,就直接返回再也不理睬了,直到kernel发送一个信号,告诉进程说IO完成 . Throughout this process, the process has not been blocked at all.

Comparison of each IO model:


Enter a picture description

From the above image, it can be found that the difference between non-blocking io and asynchronous IO is still obvious. 在non-blocking IO中,虽然进程大部分时间都不会被block,但是它仍然要求进程去主动的check, and when the data is ready, it is also necessary for the process to proactively call Recvfrom again to copy the data to the user's memory. and asynchronous Io is completely different. 它就像是用户进程将整个IO操作交给了他人(kernel)完成,然后他人做完后发信号通知. During this period, the 用户进程不需要去检查IO操作的状态,也不需要主动的去拷贝数据 .



The books of Tao
Links: http://www.jianshu.com/p/486b0965c296
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Talk about Linux five IO models

Related Article

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.