Understanding of processes, threads, and co-routines-your own essays

Source: Internet
Author: User

1. IO operation does not consume CPU (read data from hard disk, read data from network, read data from memory)

Calculations that occupy the CPU, such as 1+1=2, are CPU-intensive.

Python multi-threading, not suitable for CPU-intensive operating system tasks, suitable for IO-intensive tasks.

2.

The relationships and differences between processes, threads, and the process have been bothering me for a while, and I've had some recent experiences.

The process has its own independent heap and stack, neither sharing the heap nor sharing the stack, and the process is dispatched by the operating system.

Threads have their own separate stacks and shared heaps, shared heaps, non-shared stacks, and threads are also dispatched by the operating system (standard threads Yes).

The threads share the heap as a thread and do not share the stack, and the process is displayed by the programmer in the code of the coprocessor.

The difference between the process and the other two is obvious.

The difference between the process and the thread is that the co-process avoids meaningless scheduling, which can improve performance, but therefore, the programmer must assume responsibility for the dispatch, and also lose the ability of the standard thread to use the multi-CPU.

For example, suppose that there is an operating system that is single-core, no other programs on the system need to run, there are two threads A and B, a and B in a separate run requires 10 seconds to complete their own tasks, and the task is operations, a B there is no competition and sharing data between the problem. Now a b two threads in parallel, the operating system will continue to switch between a B two threads, to achieve a pseudo-parallel effect, assuming that the frequency of switching is once per second, the cost of switching is 0.1 seconds (mainly stack switching), a total of 20 + 19 * 0.1 = 21.9 seconds. If the use of the way, you can run the process of a, the end of a time to give way to the process B, only one switch, the total time is 20 + 1 * 0.1 = 20.1 seconds. If the system is dual-core, and the thread is a standard thread, then a-B two threads can be really parallel, the total time is only 10 seconds, and the scheme of the co-process will still need 20.1 seconds.

3. From the perspective of hardware development, from the initial single-core single CPU, to single-core multi-CPU, multi-core multi-CPU, it seems to have reached the limit, but the single-core CPU performance is still increasing. The server side is also constantly evolving. If the program is divided into IO-intensive applications and CPU-intensive applications, the development of the server is as follows:
IO-intensive applications: Multi-process, multithreading---event-driven
CPU-intensive applications: multi-process and multi-threading

If multiple processes are multi-CPU and multithreading corresponds to multi-core CPUs, then event-driven and co-scheduling is fully exploiting the potential of a single-core CPU that continually improves performance .
The following discussion does not take into account CPU-intensive applications without special instructions.

Asynchronous vs Synchronization

Whether it is a thread or a process, the use of synchronous input, when blocking, performance will be greatly reduced, can not fully utilize the CPU potential, waste hardware investment, more importantly, the software module of the iron-plate, tight coupling, can not be cut, not conducive to future expansion and change. whether it is a process or a thread, each blocking, switching needs to be caught in the system call, first let the CPU run the operating system scheduler, and then by the dispatcher decide which process (thread) to run. A lock is also required between multiple threads to access mutually exclusive code, which is one of the reasons why multithreaded programming is difficult.

The most popular asynchronous servers are event-driven (such as Nginx). Event-driven simplifies the programming model and solves the problem that multithreading is difficult to program and difficult to debug. In the asynchronous event-driven model, an operation that causes blocking is translated into an asynchronous operation, and the main thread initiates this asynchronous operation and processes the result of the asynchronous operation. Since all blocking operations are translated into asynchronous operations, the main thread of the theory most of the time is to handle the actual computational tasks, less multi-threaded scheduling time, so the performance of this model is usually better.

In general, the use of asynchronous server simplifies the programming model and improves the performance of IO-intensive applications when single-core CPU performance improves and the CPU does not become a performance bottleneck.

Co-process vs thread

Previously said, the process is a user-level lightweight thread. The co-process has its own register context and stack. When the schedule is switched, the register context and stack are saved elsewhere, and the previously saved register context and stack are restored when it is cut back. So:

The process can retain the state of the last invocation (that is, a specific combination of all local states), each time the procedure is re-entered, which is equivalent to the state of the last call, in other words: The position of the logical stream at the last departure.

In concurrent programming, a process is similar to a thread, where each association represents an execution unit, has its own local data, and shares global data and other resources with other processes. At present, the mainstream language basically chooses multithreading as the concurrency facility, the thread-related concept is preemptive multitasking (preemptive multitasking), and the association is related to collaborative multitasking.

Whether it is a process or a thread, each blocking, switching needs to be caught in the system call, first let the CPU run the operating system scheduler, and then by the dispatcher decide which process (thread) to run.
And due to the uncertain nature of the preemptive scheduling execution sequence, the use of threads requires very careful handling of synchronization issues, and there is no such problem with the process (event-driven and asynchronous programs have the same benefits).

We are in the process of the completion of the logic flow scheduling, touch the i\o I use non-blocking. Then we can take advantage of the asynchronous advantage, but also to avoid repeated system calls, as well as the cost of process switching, the minute gives you thousands of logical flows effortlessly. This is the co-process.

Co-process vs event-driven

The event-driven asynchronous server that is represented by Nginx is sweeping the world, so will the event-driven model be the end of the server-side model?
We can take a closer look at the model of event-driven programming.
The architecture of event-driven programming is to pre-design an event loop that constantly checks the information currently being processed and runs a trigger function based on the information to be processed. This external information may come from a file in a folder, possibly from a keyboard or mouse action, or a time event. This trigger function can be either the system default or a user-registered callback function.

Event-driven programming focuses on resiliency and asynchrony. Many GUI frameworks (such as the GUI framework for Windows Mfc,android), zookeeper Watcher, and so on, use event-driven mechanisms. There will be other event-driven works in the future.

Event-driven programming is a single-threaded thinking, which is characterized by asynchronous + callbacks.
The co-process is also single-threaded, but it allows non-human code to be written using asynchronous + callback methods, which can be written in a seemingly synchronous way. It is the key to the so-called non-preemptive collaboration to achieve push-pull interactions.

Summarize

Benefits of the co-process:

    • Cross-platform
    • Cross-architecture
    • No overhead for thread context switching
    • No need for atomic operation locking and synchronization overhead
    • Easy switching of control flow and simplified programming model
    • High concurrency + high scalability + Low cost: A CPU support for tens of thousands of processes is not a problem. Therefore, it is suitable for high concurrency processing.

Disadvantages:

      • Unable to take advantage of multicore resources: The nature of the process is a single thread, it can not be a single CPU at the same time multiple cores, the process needs and processes to run on multi-CPU. Of course, most of the applications that we write in the day-out are not necessary, except for CPU-intensive applications.
      • Blocking (Blocking) operations, such as IO, block the entire program: As with event-driven, asynchronous IO operations can be used to resolve

Understanding of processes, threads, and co-routines-your own essays

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.