Java-io model

Source: Internet
Author: User

Chapter Content

  1 synchronous, asynchronous, blocking, non-blocking concepts

25 IO Models: Blocking IO, non-blocking IO, multiplexed io, signal driven IO, asynchronous IO individual features

synchronous, asynchronous, blocking, non-blocking (note: This is an introduction to concepts from the user thread and kernel level)

Synchronous and asynchronous (describes how the user thread interacts with the kernel-the communication mechanism of the message)

 1 Synchronous user threads need to wait after initiating IO

2 The asynchronous user thread continues execution after initiating an IO request, notifies the user thread when the kernel operation is complete , or invokes a callback function registered by the user thread

blocking and non-blocking (describes the state that the program waits for the call result-the state of the caller)

  1 blocking before the call results are returned, the caller's execution thread hangs, and the thread cannot do anything else, only waiting for the result to go down

2 The non-blocking IO operation returns the caller a status value immediately after the call, without waiting for the IO operation to complete completely

IO Model

Introduced

In order to secure the operating system, the memory space is divided into user space and kernel space, the user process can not directly manipulate the underlying hardware, can only submit the request to the CPU, executed by the CPU

The process (thread) submits the request to the CPU, waiting for the CPU to return the data (this is a two-segment process)

1 Waiting for data preparation: The kernel copies data from the IO device to buffer in the memory space

2 The kernel copies data from the internal buffer to the process space

  

Note: During IO operation, the kernel will maintain a buffer for each IO device, waiting for data input to buffer, copying from buffer to process memory space, all of which require time, according to the wait mode can be divided into:

1 blocking IO-blocking IO (two-segment blocking)

2 nonblocking io non-blocking IO (first segment not blocked, second segment blocked)

3 io multiplexing io multiplexing (two segments are blocked)

4 signal driven IO signal driver io (first segment not blocked, second segment blocked)

5 Asynchronus io Asynchronous IO (two segments are not blocked)

Here is a detailed introduction to the above five models

Blocking IO

1 The user thread blocks while reading and writing, the system call does not return the call result and keeps the current thread blocked until the system call gets the result or the time-out error is returned

  

:

1 Recvfrom initiates a system call that waits for data preparation (the first paragraph) to be blocked by the application process when the kernel is not ready to be reported.

2 when the kernel data is ready, copy the data to user space (second paragraph) after the application process blocks the copy completion, the return succeeds, let the user process to process the packet, at this time the blocking end, transfer control to the application process

2 Disadvantages

The user process is blocked in the IO process and cannot do anything for the CPU resource utilization is not high

Non-blocking IO

1 The user thread constantly initiates an IO request, the system returns a status value before the data arrives, and the data arrives before actually reading the data (requires the user thread to constantly determine if the datagram is ready)

  

:

1 when the user process Recvfrom initiates a system call, the kernel does not have a packet ready to return a Ewouldblock state when the user process does not know that the kernel has prepared the data, you need to continue polling to determine whether the datagram is ready (this process user process is non-blocking)

2 after the kernel has prepared the data, the kernel copies the data to the process space (user process blocking)

2 Disadvantages

The user process can immediately return each request Io, but in order to get the data, the need to constantly poll, needlessly consume a lot of CPU

Io multiplexing (the pattern description is not very detailed and will be described in more detail in subsequent Bio,nio)

1 The user first adds the socket that requires IO operation to the Select, and then waits for the block function Select to return, and when the data arrives, the socket is activated, select returns, and the user thread can then initiate the read request (Note: blocking is Select)

    Here are three mechanisms provided by the operating system's underlying select,poll,epoll, and here is an example of select (the use of this pattern will be described in subsequent NIO)

  

:

I/O multiplexing is a mechanism by which multiple descriptors can be monitored, and once a descriptor is ready (usually read-ready or write-ready), the program can be notified of the appropriate read and write operations

2 advantages

More connections can be processed in a single thread/process

System overhead is small and the system does not have to create processes/threads or maintain these processes/threads, which greatly reduces the overhead of the system

Signal-driven IO

  

:

1 The driver IO function of socket is opened, and a signal processing function is installed by sigachtion system call, the system call returns immediately, the process continues to work, and when the datagram is ready, the kernel generates a Sigio signal for the process (the first segment does not block)

2 Once the signal is received, the system call is initiated again, and the kernel copies the packet to the user space (second block)

Asynchronous IO

 1 working mechanism tells the kernel to initiate an action and let the kernel notify us when the entire operation is complete

  

:

1 The application process initiates a system call, and the kernel immediately returns a state at which time the process continues execution

2 notifies process processing (no two blocks) when the kernel prepares the data and copies the packets to the user space

Summarize:

1 blocking IO: The user process initiates a system call, blocks the user process, the kernel processes the datagram, returns a success indication when both processes are complete, and then processes the datagram by the user process

2 non-blocking IO: The user process initiates a system call, returns a status to the user process when the kernel datagram is not ready, the user process continues to execute, is not blocked, but needs to initiate polling, continually asks if the kernel datagram is ready, and blocks the user process when the datagram is found ready. The kernel copies the datagram to the user space and, after the copy is completed, returns a success indicator, which is handled by the user process.

3 multiplexing IO: The user processregisters the IO request into the Select, the user process continues, select initiates the system call, the kernel does the data preparation, when the kernel prepares the data, returns the user process's readable instructions, the user process initiates the system call, the user process is blocked, The kernel copies the datagram to the user space and, after the copy is completed, returns a success indicator, which is handled by the user process

4 Signal-driven IO: The user process initiates a system call, when the kernel datagram is not ready, returns a status to the user process, the user process continues to execute, is not blocked, when the kernel prepares the data, through the signal notifies the user process, the user process initiates the system call, the user process is blocked, The kernel copies the datagram to the user space and, after the copy is completed, returns a success indicator, which is handled by the user process

5 Asynchronous IO: The user process initiates a system call when the kernel packet is not ready to be, returns a status to the user process, the user process continues to execute, is not blocked, and when the kernel prepares the data and copies the data to the user space, notifies the user process to directly process the datagram  

Java-io model

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.