Network programming model of Unix IO

After the first, we have to understand IO model the first thing to know is the underlying operating system to realize the transmission of data through which equipment, and secondly to understand the IO model, which is a blocking call operation occurs, then With the above basic knowledge, we began to understand how IO model is an evolution, and finally through the evolution of IO model we want to identify the key terms associated with distinction IO model of the mind map covers the following points to share the knowledge!

 

 

Data transmission exchange medium

Unix operating system architecture diagram

Summary Description

  • User space: Part of the above user level or unix programming application called user space, we can call the kernel command by boot process has finished reading or writing from hardware devices and other operations

  • Kernel: the application is dealing directly with the computer hardware level, computer-related books also called an operating system, some components may be provided by the operating system level to help users complete the process with the computer hardware operation of communication interactions, can be called as a transit point

  • Hardware: for network programming, the associated hardware device is a network interface controller, the network interface controller via the byte stream data and then transmitted to the Internet IP address, etc. The information transmitted to other computer systems applications, multi- communication between computer system

  • File descriptor (File descriptor): in the linux / unix system, a process file stores a file description table (memory array), a storage array structure pointer file type, file processes the file to complete the operation by the array index, array where index file descriptor is referred to as fd

 

 

Thus, by the above diagram and a brief description clear, the user process can not directly operate the hardware device must request command to the kernel initiated, then parse and execute the corresponding command operations to the hardware device by the kernel, then across a computer process network data transmission needs the kernel then send data to the network interface controller, communicate, shown below between computers via a network interface controller:

 

 

Network IO blocking operation

IO read operation step core

  • A user process to the kernel initiates data read operation request, the kernel must wait until the data packet to obtain data from the hardware device ready

  • When the kernel from the device to prepare a good data packets need to be copied from the kernel to user space

IO blocking operation

  • recvfrom function: as IO operation system calls, here distinguished system kernel and user processes, call the function when the need to switch from the contextual space of the user process to the kernel space to run, in order to switch back after a period of time

  • select / poll function: Allows a process to instruct the kernel to wait for any occurrence of a plurality of events, and only in one or more events have occurred or are more than a specified period of time will wake the user process to inform the current event is ready

Contrast IO operations

  • recvfrom function: calling the current function of time, then the user process is blocked in the system kernel, when other process initiated the request to call at this time must wait outside the system kernel to complete the work to deal with other processes the request, which is 1: 1 model

  • select / poll function: calling the current function of time, then the user process is currently blocked in select functions, continuously polls the ready state to select the kernel descriptor or more than a specified period of time to be awakened, select the system function call is due to carry descriptor set to the kernel, it is possible to listen to the N descriptor (descriptor tentatively called the ready state to a readable state event, the event discussed are discussed later), i.e., N: 1 model, as shown below:

 

 

 

IO model evolution

Based on the data transmission and blocking operations IO understood, network programming necessary to read the data transmitted by the network through the system kernel then needs to user space, kernel needs to wait for the completion of data preparation and copy the data to the user space during step two, while to simplify the concept, low-TCP sockets and other information hiding not explain, in simple and understandable way to demonstrate the UDP transport five kinds of IO models under Unix

 

Blocking IO model

  • Since the application process initiated recvfrom system calls, in the meantime has been in is blocked, this time to wait because the kernel gets copied datagram information to the user space unless interrupted abnormal returns, otherwise it will remain in a blocked state

  • The following is a timing diagram showing blocking IO 

Non-blocking IO model

  • Non-blocking is mainly reflected in the user process initiated when the recvfrom system call, this time the system kernel has not received datagrams, an error is returned directly to the user process, telling the user process " currently no datagram up, come back late ."

  • User process receives the information, but does not know when the user process datagrams up, so he began polling (polling) to initiate a system call recvfrom system kernel " asked data yet ," if not then continue to return an error

  • Polling process initiated by the user until the system call recvfrom datagram up, this time need to wait for the system kernel buffer to copy the data to report user process will return success prompted after the copy is complete

  • The following timing diagram shows the way NIO 

IO multiplexing model

  • IO multiplexing model is to use select or poll function to initiate calls to the system kernel, blocking calls in which one of the two systems function, rather than actually blocking the actual IO operations (recvfrom call blocking system call is the actual IO operations)

  • Call blocking to select the function, wait for the system kernel ready data and inform current event to a readable state

  • When receiving the select kernel state notification event is read, can be copied to the system kernel to the user initiates data calls recvfrom buffer space

  • IO multiplexing mode timing chart below 

Model-driven signal IO

  • First turn signal IO driver function socket and call system is installed by a built-signaction signal processing function, directly after initiating call will return

  • Then, after waiting for receiving data packets from the core network, transmission signal up to the current data to the user process to the signal processing functions

  • The signal processing function receives the system call information initiates recvfrom datagram buffer waits for the user to copy the kernel space

  • After returning success tips received the copy is complete, the application process can begin to read data from the network

  • The above is a signal driven IO model, when the system kernel descriptor will be ready to send to the user process SIGNO, this time to initiate recvfrom system calls waiting to return success tips, process is as follows:

Asynchronous IO model

  • POSIX specification is defined by informing the kernel start an operation, wait and let the core includes data throughout the operation and notify the user data after completion of the process of data replication process has been ready, the data can be read

  • IO model described above to distinguish between signals that asynchronously notify us when the operation is completed direct IO, IO is the signal when we can notice a start IO operation

  • Timing diagram follows 

5 kinds of IO Model

  • For blocking IO model, is 1: 1 model, which is the kernel can only handle one request, other requests came over to wait for the current request to be processed, the performance is quite poor, through multi-threaded changed to N: 1 model with non-blocking IO Similarly, the distinction is that the former multi-threaded, single-threaded latter

  • For IO model-driven signal, although it is non-blocking IO model, but the kernel-based implementation mechanisms notification callback more complex (and read the data in the signal processing function asynchronous IO operations to maintain order, personally I think that the signal is not designed to function properly deal operations, i.e., the subsequent data read operation and the like)

  • For non-blocking IO model, compared to blocking IO concerned, but recvfrom system calls initiated by constantly polling the way, since the switching kernel and user space exists, and loss performance

  • For IO multiplexing model, call blocking to select function (based on a set of file descriptors to traverse nature), up to the kernel corresponding event and wait for an event or read-out notification to select function

  • And for AIO models, it is a true non-blocking asynchronous IO mode, but in linux / unix system supports this IO model design are not sure

  • Most Unix / Linux-based servers are optimized and improved with IO multiplexing model, that is, to select / poll method enhanced optimization

 

 

IO Key Terms

The definition of synchronous and asynchronous

  • Sync: fn initiate a call, you need to wait for the call returns, the call is either the result desired result is either the result of an exception is thrown, it can be said that atomic operations (either succeed or fail return)

  • Asynchronous: fn initiate a call directly without waiting for the results to return only after the handler is invoked to execute the "wake-up" means to notify the caller to get the result (wake-up mode callback, event notification, etc.)

  • Summary: synchronous and asynchronous communication between the concerned program

Definition of blocking and non-blocking

  • Blocking: blocking thread analogy to explain, in a simultaneous multi-threading race conditions of competition for resources, if there is a thread already holds the lock, then the current thread will not acquire a lock to be suspended in a wait state

  • Non-blocking: Once a thread releases the lock, other threads will enter the ready state, competition for qualified lock

  • Summary: blocking and non-blocking is more concerned about the state of waiting for the results of the program

  • It can be seen, synchronous and asynchronous non-blocking between blocking association does not exist, the goal is not the same concern

Synchronous and asynchronous IO IO (based on the POSIX specification)

  • Synchronous IO: represents the real application process initiated IO operation requests (recvfrom) causes the process has been in a wait state, this time the process is blocked until the IO operation completes successfully prompt return

  • Asynchronous IO: represents the real application process initiated IO operation requests (recvfrom), this time the system kernel will directly return an error message to the user process, "the equivalent of telling process has not deal with, well, you would notice."

  • Blocking IO: IO operations mainly reflected initiate a request to inform the kernel and the kernel after receiving a request to let the process if the wait, then it is blocked

  • Non-blocking IO: IO operation initiated when the request regardless of the result of the process of how to tell "Do not wait, come back late," that is, non-blocking

IO Model Comparison

According to the defined synchronous and asynchronous IO and IO model described above in conjunction with known, and only asynchronous IO model conforms to the POSIX specification asynchronous IO, IO other models exist recvfrom kernel system call is blocked, synchronous IO operations belonging to thereby be summarized as follows:

  • In other words, known as synchronous or asynchronous IO and IO, or IO model name referred to above

  • Most operating systems are based on the synchronous IO manner, the operating system supports asynchronous IO models is uncertain, exposed to practical work in IO model in the strict sense should be called Blocking-IO (blocking IO) and Non-Blocking-IO (non-blocking IO), instead of synchronous and asynchronous IO IO

  • Summary: synchronous and asynchronous communication mechanism for blocking and non-blocking program for the state of waiting for the results of the call

Published 18 original articles · won praise 588 · Views 1.03 million +

Guess you like

Origin blog.csdn.net/hellozhxy/article/details/105198664