Process blocking and pending definitions:
Blocking is due to non-fulfillment of the resources required by the process and will eventually cause the process to be suspended
The cause of the process hangs is not necessarily due to blocking, it is also possible that the time slice is not satisfied, the suspended state is a process from memory to external memory in a state, if in the ready state, from memory to external memory, is ready to suspend the state, if in the blocking state, from memory to external memory, it is converted into a blocking suspend state
The high concurrency of Nginx is due to its epoll model, which differs from the traditional server program architecture, and Epoll is a Linux kernel 2.6. Below compare Apache and nginx working principle.
Traditional Apache is multi-process or multi-threaded to work, assuming that the multi-process work (prefork), Apache will be a few processes, similar to the process pool work, but the process pool here will increase as the number of requests increases. For each connection, Apache is processed within a process. Specifically, recv (), and the disk I/O based on the URI to find the file, and send () are blocked. In fact, all is apche for socket I/O, read or write, but read or write is blocked, blocking means that the process has to suspend into sleep state, then once the number of connections, Apache must generate more processes to respond to requests, once the process is more, CPU switching to the process is frequent, resource-intensive and time-consuming, so it leads to the performance of Apache degraded, white is to deal with so many processes. In fact, think about it, if the process is not blocking each request, then the efficiency will certainly improve a lot.
Nginx adopts Epoll model, asynchronous non-blocking. For Nginx, the processing of a complete connection request is divided into events, one for each event. such as accept (), recv (), Disk i/o,send (), and so on, each part has the corresponding module to handle, a complete request may be handled by hundreds of modules. The real core is the event collection and distribution module, which is the core of all the management modules. Only the core module is dispatched to allow the corresponding module to consume CPU resources, thus processing the request. take an HTTP request, first in the Event collection Distribution Module register the listener interested in the event, after registration does not block the direct return, then do not need to pipe, wait for the connection to the kernel will notify you (Epoll polling will tell the process), the CPU can handle other things. Once a request comes in, then the entire request is assigned the appropriate context (in fact pre-allocated), the time to register a new event of interest (read function), the same client data to the kernel will automatically notify the process to read the data, read the data is parsed, after parsing the disk to find resources (I/O), Once I/O is done notifying the process, the process starts sending back the data send () to the client, which is not blocked at this time, so the kernel sends back a notification after the call is sent. The whole down to a request into a number of stages, each stage to many modules to register, and then processing, are asynchronous non-blocking. asynchronous here refers to do a thing, do not need to wait to return the results, done will automatically notify you.
Features of Select/epoll
Select Features: When select handles are traversed for all handles, that is, when the handle has an event response, select needs to traverse all the handles to get to which handles have event notifications, so the efficiency is very low. However, if there are few connections, there is little difference in performance compared to the LT trigger mode of Select and Epoll.
To say more, select supports the number of handles is limited, and only support 1024, this is the handle set limit, if more than this limit, it is likely to lead to overflow, and very difficult to find the problem, of course, you can modify the Linux socket kernel to adjust this parameter.
Epoll features: Epoll for handle event selection is not traversed, is the event response, is the handle on the event to be immediately selected, do not need to traverse the entire handle chain list, so the efficiency is very high, the kernel will handle the handle with the red-black tree saved.
For Epoll, there is the difference between ET and LT, which is the horizontal trigger, et is the edge trigger, and the difference between performance and code implementation is very large.
Apache and Nginx Network model detailed