To say the answer

Because resources are limited, the purpose of multithreading is to maximize the utilization of CPU resources.

(1) Basic concepts

1. Is the underlying Linux single-process or multi-process?

Answer: Multi-tasking with a single thread.

What is the difference between a process, a thread, and a coroutine?

  • Processes are the smallest unit of resource allocation and threads are the smallest unit of processor scheduling. A process is made up of one or more threads.
  • Thread: the initial unit is 8MB, fixed immutable; Done by the kernel
  • Coroutine: the initial size is generally 2KB and can be increased as needed; Done by the user

What is the state of the CPU?

  1. Kernel mode: Linux file subsystem, I/O read and write disk; Send HTPP request;
  2. User mode:
  3. Switching between kernel mode and user mode

(2) Why do we need to learn IO model with limited resources?

What is the maximum number of threads that can theoretically be created by a single process in LINUX on the system?

In 32-bit Linux, the answer is 381.

The default thread stack is 8M=8192KB

32-bit Linux system process user space is the size of 3G, that is 3072M with 3072M/8M=384.

But actually the code and data segments and so on take up some space, so this value should be rounded down to 383 and subtracted from the main thread to get 382. So why is there one less thread on LinuxThreads? This makes perfect sense, because LinuxThreads also requires an administrative thread.

Nofile Specifies the maximum number of open file descriptors for the process

2. A process can contain multiple threads (381), so the actual execution process is much less.

When a project is running, many clients will make concurrent requests to the server. If 100 users make concurrent requests and each request takes 5 threads to process, the system will crash.

So how do you handle high concurrency, that is, network IO?

This is why we learn about IO models – concurrency can be improved by optimizing network IO

Here are some important concepts for learning IO models:

(3) Synchronous and asynchronous

Has to do with the notification mechanism for messages

The essential difference Real example
Synchronous mode It is up to the message handler to wait for the message to be triggered I go to the bank to do business, choose to wait in line, at the end of the line.
Asynchronous mode A trigger mechanism notifies the message handler I go to the bank to deal with business, take a small piece of paper above have my number, wait until the row to my this number by the counter notice me turn I go to deal with business.

(4) Blocking and non-blocking

It relates to the state of the thread waiting for a message, whether synchronous or asynchronous.

The title The essential difference Real example
Blocking calls The thread hangs and can’t do anything else. In the above example, either queuing or using a number to wait for notifications, the mechanism is blocked if the waiting person can do nothing but wait for the message while waiting.
Non-blocking call The thread is active and can do other things. It is non-blocking to make a phone call or send a text message while the bank is waiting for these transactions.

(v) Examples of their combined applications:

Blocking calls Blocking calls Non-blocking call
Synchronous mode read/write read/write O_NONBLOCK
Asynchronous mode IO multiplexing: select/poll, epoll(LT mode) AIO series: AIO_read, AIO_write, etc. Epoll (ET)

The specific IO model is shared by many people, so I will not repeat it here. There is also a link in the reference link below.

(6) Reference links

1, process, thread, coroutines www.cnblogs.com/Survivalist…

2, select, poll, blog.csdn.net/ithomer/art epoll difference…

3, the Linux asynchronous mechanism blog.csdn.net/yechaodechu…

4. Maximum number of processes, maximum number of threads… Blog.csdn.net/gatieme/art…

5, memory space and Kernel space (the User space and Kernel space) www.ruanyifeng.com/blog/2016/1…

6, I/O model zhuanlan.zhihu.com/p/43933717

Write in the last

Recommend a full series of TS tutorials bar. Recent TS in ascension, collect a set of very good tutorial, free to share with everyone www.yidengxuetang.com/pub-page/in…