In-depth understanding of Java I/O series six: IO model in Linux

Source: Internet
Author: User
Tags epoll

IO model

Linux system IO is divided into two phases: The kernel prepares the data and copies the data from the kernel to the user space.

This diagram outlines the process of moving data from an external disk to the memory of a running program.

User space, kernel space

Now the operating system is using virtual memory, then for the 32-bit operating system, its address space (virtual storage space) is 4G (2 of 32). The core of the operating system is the kernel, which is independent of the normal application, has access to protected memory space, and has all the permissions to access the underlying hardware device. In order to ensure that the user process can not directly manipulate the kernel, to ensure the security of the kernel, the operating system divides the virtual space into two parts, a part of the kernel space, part of the user space.

How to allocate the size of these two spaces is also fastidious, such as the Windows 32-bit operating system, the default user space: The ratio of kernel space is 1:1, while in 32-bit Linux system The default scale is 3:1 (3G user space, 1G kernel space).

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 becomes a switchover of the process. Any process that runs under the support of the operating system kernel is closely related to the kernel.

The process of process switching passes through the following changes:

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

2, update the PCB information.

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

4, select another process to execute, and update the PCB

5. Update the data structure of memory management.

6. Recovery Processor Context

Cache io

Cache Io is also known as standard IO, and most file system default IO operations are cache io. In the Linux cache IO mechanism, the operating system caches the IO data in the file system's page cache. That is, the data is copied into the buffer of the operating system kernel before it is copied from the cache of the operating system kernel to the application's address space.

The disadvantage of this approach is that multiple copies of the application address space and kernel are required, and the CPU and memory overhead of these copy actions is very large.

synchronous, asynchronous, blocking, non-blocking

1. The IO synchronous call requires the main thread to wait for the IO thread to return, and the asynchronous call simply initiates the IO thread and returns immediately, notifying the main thread via a notification mechanism such as a callback function after IO completes. The difference between synchronous asynchrony is whether the main thread is blocked during the data copy process.

2, blocking IO before the main thread is suspended, non-blocking IO does not suspend the main thread, from the caller's point of view, the difference between blocking and non-blocking IO can also be understood as whether the call returns immediately.

3, thus, synchronous asynchronous and blocking non-blocking IO is the classification under different standards. Blocking non-blocking IO is concerned about a state in which the program waits for the result to be called.

Linux IO Model

The essence of network IO is the socket read, socket in Linux system is abstracted as stream, IO can be understood as the operation of convection. As mentioned at the beginning of the article, for an IO access (in read, for example), the data is copied to the operating system kernel buffer before it is copied from the operating system kernel buffer to the application's address space. So, when a read operation occurs, it goes through two stages:

First stage: Wait for data preparation.

Second stage: Copying data from the kernel to the process

For the socket stream:

The first step: usually involves waiting for a packet of data on the network to arrive, and then copy it to a buffer in the kernel.

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

Of course, if the buffer in the kernel space already has data, then the first step can be omitted. Why not just let the disk controller send the data to the application's address space? One of the simplest reasons is that applications cannot manipulate the underlying hardware directly.

Network applications need to deal with nothing more than two major types of problems, network IO, data calculation. 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 broadly divided into the following five types:

1. Blocking IO

2. Non-blocking IO

3. Multiplexing IO

4. Signal-driven IO

5. Asynchronous IO

The first four types are synchronous, and only the last is asynchronous IO. The following model introduces the first example of life to illustrate the concept: the weekend and girlfriend to shopping malls, to the evening meal, ready to eat and then go shopping, but weekend people, the new White Deer Hotel need to queue, so there are several options:

1. Blocking IO Model

Scenario Description:

In the hotel after the number, there are n table, do not know when to us, but can not leave, because after the number must be re-number. Had to wait in the hotel, waited until the station-to-station we had supper, and then went shopping. In the middle of the waiting time nothing can be done.

Network model:

In this model, the application, in order to perform this read operation, invokes the corresponding system call, gives the control over to the kernel and waits (the waiting process is blocked), and the kernel starts to execute the system call. Once executed, the response is returned to the application, and after the application responds, it is no longer blocked and does the work behind it.

Advantages:

Ability to return data in a timely manner without delay.

Disadvantages:

Waiting for the user is a performance cost.

2. Non-blocking IO

Scenario Description:

Waiting for the process is too boring, so we go to the mall, every once in a while to ask the waiter, the number of call to us, the whole process to go back and forth several times. This is non-blocking, but requires constant questioning.

Network model:

  When the user process issues a read operation, the corresponding system call is called, which is immediately returned from the kernel. But at this point in time of return, the data in the kernel may not be ready, that is, the kernel just returns to system call very quickly, so that it does not block the user process, and for the application, although the IO operation returns quickly, it does not know whether the IO operation is really successful, In order to know if the IO operation is successful, the application needs to proactively cycle through the kernel.

Advantages:

Be able to do other things in the waiting time.

Disadvantages:

The response latency for task completion is increased because each time a read operation is polled, and the task may complete at any time between two polls, which results in a decrease in overall data throughput.

3. IO multiplexing

Scenario Description:

And the second is often similar, the hotel installed an electronic screen, showing the status of the number of the call, so in the shopping time, you do not have to ask the waiter, but to see the big screen on it. (Not only do we not have to ask the waiter, all the other people can not ask the waiter)

Network model:

As in the second, after calling system call, it does not wait for the results of the kernel to return but to return immediately. Although the calling function that returns the result is an asynchronous one, the application is blocked by functions such as SELECT, poll, and Epoll with multiple file descriptors, waiting until the system call has the result returned, and then notifies the application. In this case, from the actual effect of the IO operation, the asynchronous blocking IO is the same as the first synchronous blocking IO, and the application waits until the IO operation succeeds (the data has been written or read) before starting the following work. The difference is that asynchronous blocking IO uses a SELECT function to provide notifications for multiple file descriptors, providing concurrency. For example: There are 10,000 concurrent read requests, but there is still no data on the network, at this time the 10,000 read will be blocked at the same time, now with the Select, poll, Epoll such functions are specifically responsible for blocking the status of the 10,000 requests, once the data arrives, it is responsible for the notification , which translates 10,000 waits and blocks into a dedicated function to be responsible and manageable.

The difference between asynchronous blocking IO and synchronous nonblocking IO is that synchronous non-blocking IO requires the application to proactively cycle through queries for data, while asynchronous blocking IO detects multiple event handles at the same time through an IO multiplexing function like Select to tell the application whether there is data.

  Understanding the previous three IO modes, when the user process makes system calls, they wait for the data to arrive, the way of processing is not the same, direct waiting, polling, select or poll polling, two stages of the process:

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

The second stage is blocked.

From the whole IO process, they are executed sequentially, so they can be classified as synchronous models, which are automatically waiting for the process and checking the state to the kernel.

  highly concurrent programs generally use synchronous nonblocking mode instead of Multithreading + synchronous blocking mode. to understand this, first understand the difference between concurrency and parallelism: for example, to a department to do business needs to go to several windows, the number of Office Hall is the number of concurrent, and the number of Windows is the degree of parallelism. That is, concurrency is the number of simultaneous tasks (such as HTTP requests for simultaneous service), and the number of parallel is the number of physical resources (such as the number of CPU cores) that can work concurrently. By reasonably dispatching different stages of a task, the number of concurrent numbers can be much greater than the degree of parallelism. This is why only a few CPUs can support tens of thousands of users for concurrent requests. In this high-concurrency scenario, it is very expensive to create a process or thread for each user request. Synchronous nonblocking mode can throw multiple IO requests into the background so that a CPU can serve a large number of concurrent IO requests.

  Whether IO multiplexing is a synchronous or asynchronous blocking model, let's say:

Synchronization is the need to actively wait for message notification, while asynchronous is passively accept the message notification, through callback, notification, state, etc. to passively get the message. Io multiplexing when blocking to the Select phase, the user process is actively waiting for and invoking the Select function to get a ready status message, and its process state is blocked. So IO multiplexing is a synchronous blocking mode.

4. Signal-driven IO

The application submits a read request, calls system call, and the kernel begins to process the corresponding IO operation, while the application does not wait for the kernel to return a response, and it begins to perform other processing operations (the application is not blocked by IO), and when the kernel finishes executing, it returns a read response, Generates a signal or executes a thread-based callback function to complete the IO process. The read and write operation of Io here is done by the application after the IO event occurs. Asynchronous IO Read and write operations always return immediately, regardless of whether Io is blocked, because the kernel is in charge of the real read and write operations. This means that the synchronous IO Model requires user code to perform IO operations on its own (moving the data from the kernel buffer to the user buffer or vice versa), while the asynchronous operation mechanism is performed by the kernel to perform IO operations (moving the data from the kernel buffer to the user buffer or vice versa). It can be argued that synchronous IO notifies the application of an IO-ready event, while asynchronous IO notifies the application of an IO completion event.

5. Asynchronous IO

Asynchronous IO is the same as the asynchronous concept above, when an asynchronous procedure call is issued, the caller cannot get the result immediately, and the function that actually processes the call notifies the caller of the input and output operation through state, notification, and callback. Asynchronous IO works by telling the kernel to initiate an operation and letting the kernel notify us when the entire operation is complete, the difference between the model and the signal-driven IO is that the signal-driven IO is the kernel that notifies us when an IO operation can be initiated, which is implemented by a user-defined signal function. The asynchronous IO model is the kernel that tells us when the IO operation is complete.

 

In-depth understanding of Java I/O series six: IO model in Linux

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.