Selection of multithreading and multi-process models
The thread here refers to the native thread that is generated by the Linux pthread_create, and the thread resources are valuable and can be seen by the operating system's Task Scheduler (not the concept of Python gevent, go gorouine);
We discuss the following two types of models;
- Multi-process single-threaded model (hereinafter referred to as multi-process);
- Single-process multithreaded model (hereinafter referred to as multithreading);
Advantages of the multi-process model
programming is relatively easy; there is usually no need to consider locking and synchronizing resources.
Greater fault tolerance: one advantage over multithreading is that a process crashes without affecting other processes.
Isolation with Kernel Assurance: Data and error isolation.
For native code written in these languages, such as C + +, error isolation is useful: programs that use a multi-process architecture can generally achieve a degree of self-recovery; (The master daemon monitors all worker processes, discovers that the process hangs and restarts them)
Multi-process case
Nginx Mainstream mode of operation is multi-process mode (also support multithreading model)
Almost all Web server server services are multi-process, at least one daemon with a worker process, such as APACHED,HTTPD, and so on the end of the process, including the INIT.D itself is the level 0 total process, all of your cognitive process is its child process;
The Chrome browser is also a multi-process approach.
Redis can also be categorized as a "multi-process single-threaded" model (usually working as a single process, involving multiple processes when time-consuming operations such as persistence or aof overrides are used)
Multithreading Model Benefits
Multithreading Benefits: Create fast, easy and efficient data sharing
Shared data: The same virtual address space can be shared between multiple threads, and the data sharing between processes requires the use of IPC technologies such as shared memory and semaphore;
Lighter context switching overhead-without switching the address space, without changing the register, without flushing the TLB.
Provision of heterogeneous services
If all are computational tasks, but each task is not time-consuming 1s, but the fluctuation between 1ms-1s, so that multithreading compared to the advantages of multi-process, it can effectively reduce the "simple task by complex task pressure" probability;
Applicable scenarios
1 There is data sharing between the threads, and the data needs to be modified (when there is a large amount of data sharing or frequent communication between different tasks);
2 The provision of heterogeneous services (priority task processing) event response has priority;
3 single-task parallel computing, in the non-CPU bound scenarios to improve response speed, reduce time delay;
4 application with human IO interaction, good user experience (keyboard mouse input, immediate response)
Multi-threaded case
Desktop software, in response to user input is a thread, daemon processing is another thread;
Memcached
Selection
Single-process multithreading and multi-process single-threaded, 2 modes how to choose?
The overhead of creating a process thread is not sufficient as the basis for selection, because we typically use a thread pool or process pool to create fixed threads or processes at system startup and do not create and destroy them frequently;
First of all, according to the working set (need to share the memory) of the size of the set, if the working set is large, multi-threaded, to avoid CPU cache frequent swap-out, such as memcached cache system;
Secondly, the choice of the basis based on the multi-threaded application of the scenario to compare their own business scenarios, whether there is such a scenario requirements: data sharing, providing heterogeneous services, single-task splitting parallelization;
If it is not necessary, or multi-process can be very good, more use of multi-process, enjoy the convenience of single-threaded programming;
The inventor of RCU, Paul McKenny, in the IS Parallel programming hard, and, If so, what Can I do about It? "said:
Multi-process can be used to solve problems easily, do not use multithreading.
Reference
Ref: Linux multithreaded Server programming: Using the Muduo network library
ref:http://www.zhihu.com/question/19903801
ref:https://computing.llnl.gov/tutorials/pthreads/#WhyPthreads
Posted by: Big CC | 10oct,2015
Blog: blog.me115.com [Subscribe]
Github: Big cc
Multithreaded and multi-process models