[Elegant interview] Clear distinction between process, thread and coroutine

Interviewer boss: Young man, let’s talk about the knowledge of process threads first, so let’s talk about the process first.

Me: The code stored on the hard disk is a static file, and the running program is called a process. Data between processes is isolated from each other.

Generally speaking, a process does not run continuously from beginning to end, and it restricts the execution of other processes in concurrent execution. During the activity of a process, there are at least three basic states, namely, running state, ready state, and blocked state.

①When the process is created and initialized, it will become ready.

② After the process in the ready state is selected by the process scheduler of the operating system, it is allocated to the CPU to officially run the process and enter the running state;

③When the process has finished running or has an error, it will be processed by the operating system as an end state;

④ When a process in the running state is running, because the running time slice allocated to it is used up, the operating system will change the process to the ready state, and then select another process to run from the ready state;

⑤When the process requests an event and must wait, for example, when requesting an I/O event, it will change from the running state to the blocked state;

⑥ When the event that the process is waiting for is completed, it will change from the blocked state to the ready state

Big interviewer: (Thinking, the young man is quite comprehensive) Then what about the thread?

Me: A thread is one of the processes 流水线and is the unit of CPU scheduling. Multiple threads can exist in a process at the same time. Threads in the same process share all the resources in the current process. For example, a process is equivalent to In a workshop, the thread is equivalent to the assembly line in the workshop, and the raw materials needed for the assembly line need to be obtained from the workshop. Therefore, it can be said that a process is a resource unit, and a thread is an execution unit, and creating a thread saves resources more than creating a process.

Interviewer boss: Do you know the coroutine in python?

Me: In python, coroutine refers to the realization of concurrency under a single thread. The developer detects the IO operation at the code level. Once the program encounters an IO operation, it will complete the switch at the code level. This gives the CPU the feeling that the program is always running. Running, without IO operations, so as to improve the operating efficiency of the program. In python, the gevent module can be used to implement coroutines. The gevent module itself cannot detect some common io operations, and monkey patches are needed.

Interviewer boss: Do you know the GIL lock in python?

Me: GIL is python's global interpreter lock, and GIL is a feature of CPython, which is used to prevent multiple threads from executing simultaneously in the same process. The reason why GIL exists is that CPython's memory management is not thread-safe.

The code of the program needs to be handed over to the interpreter for interpretation and execution. Since all threads in the process share the resources in the process, there is competition, and the garbage collection mechanism is also a thread in the current process. This thread will interact with the resources in the current process Other threads compete for data. In order to ensure data security, only one thread is running at the same time in the process and there is a GIL.

Interviewer boss: Well, the basic knowledge of process threads is not bad, let’s make an appointment to talk about the network briefly next time~

Me: (The interviewer is really strange next time) Okay, you go slowly.

Guess you like

Origin blog.csdn.net/nhb687095/article/details/130450502