Implementation modes of several concurrent servers in Linux

Source: Internet
Author: User
Tags epoll

Design patterns of several concurrent servers under Linux

1> single-threaded or single-process

The equivalent of a short link, when the accept, the start of data reception and data transmission, do not accept new connections, that is, a server, a client

There is no concurrency.

2> Loop server and concurrent server

1. Loop server: A server can only receive one client at a time, and the next client connection can be made after the current client ends access.

2. Concurrent server: One server can respond to many client access at the same time.

3>select+ multithreaded Mode

Three ways to implement concurrent servers

1. Multi-Process Concurrent server

Refers to the TCP connection, each client's request is not processed directly by the server, but by the server to create a child process to handle

2. Multi-threaded Concurrent server

A multi-process server is an improvement to a multi-process server, because a multi-process server consumes a large system resource when it creates a process, so a thread is used to replace the process so that the service handler can be created more quickly. According to statistics, creating a thread is 10,100 times times faster than creating a process, so the thread is called a "lightweight" process. A process differs from a process in that all threads in a process share information such as global memory, global variables, and so on.

Refers to the TCP connection, each client's request is not processed directly by the server, but by the server

3. Multiplexing I/O

I/O is to resolve thread/process blocking in that I/O call, a common select or pool

4>epoll

After linux2.6 some epoll, the practical method is: with a thread dedicated to the port monitoring, accept received connection, the connection is set to non-blocking mode, the Epoll time set to the Edge trigger mode, add epoll management. The receive thread blocks the wait event function in Epoll. Another thread is dedicated to sending data.

Attention:

1. If the epoll is set to horizontal trigger efficiency, the level of select is used.

The 2.Unix system has a limit on the descriptors that a single process opens, as well as the number of open descriptors within the system. The limit on the number of descriptors opened within the system is limited by two soft and hard links. Hard connections are different depending on the configuration of the machine. The soft connection limit can be modified but must be less than the hard limit.

Application:

Large-scale TCP concurrency under Linux.

There are other ways to present concurrency. such as thread pooling. Process pool, and so on, each model has his pros and cons, if large-scale concurrency, the adoption of Epoll will be better.

The Epoll time is set with the Edge trigger mode and the horizontal trigger mode.

1. Horizontal Trigger Mode:

If the file descriptor is ready to perform an IO operation without blocking, a notification is triggered. Allowing the state of the IO to be repeatedly detected at any time, it is not necessary to perform as many io,select,poll as possible after each descriptor is a level trigger event.

Triggers an event whenever the requirement is met.

2. Edge Trigger Mode:

If the file descriptor has a new IO activity since the last state change, a notification is triggered. When you receive an IO event notification as many IO operations as possible, because if you do not complete the IO again in the notification, you will need to wait until the next new IO activity arrives to get the ready descriptor. Signal-driven IO is an edge trigger.

An event is triggered whenever the state changes.

Eg: there is now a 1000-byte message, whether it is a horizontal trigger or an edge trigger, will send a read-only notification, when the 100 bytes received, the horizontal trigger because there are no more bytes read, will send a read-only notification, but the edge trigger will remain waiting for the notification, waiting for the arrival of the next message and discard the socket until the edge triggers the return ewouldblock.

    

        

  

Implementation modes of several concurrent servers 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.