Linux server development, 2 hours to get high concurrency network programming

Analysis of linux server development related videos:

Analysis of the epoll principle of linux multithreading and the principle of reactor and
the specific implementation of epoll application and epoll thread safety
Tencent T2.3 personal experience sharing, the way to obtain Tencent offer and the learning route of linux background development

1. Use multithreading to deal with the disadvantages of high concurrency

Multithreading processing high concurrency is a common way to process multiple concurrent user requests at the same time, but too many threads will increase the resource consumption of the system (resources occupied by the thread itself + system overhead caused by thread switching), and due to hardware and software Restrictions, the number of threads supported by the operating system is limited, which also inhibits the throughput of the system.

Take catering as an example. Everyone who comes to eat is an event. He will first look at the menu and then order. Just like a website has many requests, the server is required to do something. We need our service staff to handle these dining incidents.

For example, the multi-threaded processing method of catering will be like this: one person comes to eat, one waiter serves, and then the guests will look at the menu and order. The waiter gave the menu to the back kitchen. Two people come for dinner, two waiters serve... Five people come for dinner, five waiters serve...

This is the multi-threaded processing method. When an event arrives, there will be a thread service. Obviously, this method will have a good user experience when there are few people, and every guest feels that he is a VIP and is served by a dedicated person. If the restaurant keeps up to 5 guests at the same time, this restaurant can serve well.

Here comes the good news, because this shop has good service and more people are eating. There will be 10 guests at the same time. The boss is very happy, but there are only 5 waiters, so one-to-one service is not possible, and some guests will be left unattended. The boss hired 5 more waiters. Now they are all right, and everyone can be treated as VIP again.

More and more people are satisfied with this restaurant, the source of customers has increased, and the number of people who come to eat at the same time has reached 20. The boss is not happy. Please ask the waiter again, not to mention the space, but also the start-up money. People can't save money. How to do it? The boss thought for a while, 10 waiters can deal with 20 customers. The waiters should be diligent. It is still too late to serve one customer immediately after serving another. After a comprehensive consideration, the boss decided to use the thread pool of 10 service staff~~~

But this has a more serious disadvantage. If the customers who are being served by the waiter are slow to order, other customers may have to wait for a long time. Some hot-tempered guests may not be able to wait to leave.

Come, directly on the demo code:
Insert picture description here

Compile & run:
Insert picture description here

Then use the browser to directly visit: http://192.168.1.152:80 to see the running effect.

[Article benefits] C/C++ Linux server architect learning materials plus group 812855908 (data including C/C++, Linux, golang technology, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, coroutine, DPDK, ffmpeg, etc.)
Insert picture description here

2. More efficient implementation, Reactor mode

The boss later discovered that customers ordering food was slow, and most of the waiters were waiting for customers to order food. In fact, they didn't do too much work. Of course, there is a difference between the boss being the boss, and finally found a new method, that is: when the customer orders, the waiter can go to greet other guests, and when the customer orders the food, he directly greets "waiter" ", there was a waiter going to serve immediately. Hey, after the boss had this new method, he made a layoff, leaving only one waiter! This is the use of a single thread to do multithreading.

This is the design pattern of Reactor. The actual restaurants are served in the Reactor pattern. Some design models are actually from life. The Reactor mode is mainly to improve the throughput of the system and handle more things with limited resources.

On a single-core machine, multi-threading does not improve the performance of the system unless some blocking occurs. Otherwise, the overhead of thread switching will slow down the processing speed. Just like you do two things by yourself, 1. Peel an apple. 2. Cut a watermelon. Then you can do it one by one, and I think you will do it one by one. If you use multi-threading at this time, peeling apples for a while and watermelon for a while, you can see which one is faster. This is why it may be slower to process with multiple threads on a single-core machine.

Reactor mode
The word Reactor is really not suitable to translate into Chinese. In many places, it is called reactor mode, but it seems to be called reactor mode directly. In fact, I think it is better to call responder mode. Through understanding, this mode is more like a guard who has been waiting for your call, or called a summoned beast.

Concurrent systems often use the reactor mode to replace the commonly used multi-threaded processing methods to save system resources and improve system throughput.

The corresponding implementation of Reactor mode in Linux system high-performance programming is Epoll, and its working principle is as follows:

Insert picture description here

Reactor advantages:

1) Fast response, no need to be blocked by a single synchronization time, although Reactor itself is still synchronized;
2) Programming is relatively simple, which can avoid complicated multi-threading and synchronization problems to the greatest extent, and avoid the overhead of multi-threading/process switching ;
3) Scalability, it is convenient to make full use of CPU resources by increasing the number of Reactor instances;
4) Reusability, the reactor framework itself has nothing to do with specific event processing logic, and has high reusability.

3. Epoll multiple IO multiplexing API

EPOLL
1. Create EPOLL handle
int epoll_create(int size);

2. Add, modify or delete events of interest to the EPOLL object
Insert picture description here

3. Collect events that have occurred in the events monitored by epoll
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout)

Key structure:
Insert picture description here

Fourth, Epoll example implementation

Example implementation

After understanding the working principle and api of Epoll, we use the following routines to implement the previous example using epoll:

Insert picture description here

Compile and run:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40989769/article/details/112614782