Process and thread of Python Web study notes

To understand the difference and connection between the two, you must first have a macro understanding of processes and threads.

 

    Process is the basic unit for allocating and managing resources during the execution of concurrently executed programs. It is a dynamic concept and the basic unit for competing for computer system resources. Each process has its own address space, the process space or (virtual space). The size of the process space is only related to the number of bits of the processor. The size of the process space of a 16-bit long processor is 216, while the size of the process space of a 32-bit processor is 232. A process has at least five basic states, which are: initial state, executing state, waiting state, ready state, and terminated state.

 

    Threads. In a network or multi-user environment, a server usually needs to receive a large number of concurrent requests from an indeterminate number of users. It is obviously not feasible to create a process for each request, either in terms of system resource overhead or In terms of efficiency in responding to user requests. Therefore, the concept of threads in the operating system was introduced. Threads are part of a process, and a process without threads can be considered single-threaded. A thread, sometimes called a light-weight process or a light-weight process, is also a basic unit of CPU scheduling.

 

    Having said that, we have a general impression of processes and threads, and now let's talk about the general differences between the two.

 

    The execution process of the process is linear, and although there will be interruptions or pauses in the middle, the resources owned by the process only serve the linear execution process. Once a process context switch occurs, these resources must be protected. This is the execution process at the macro level of the process. There are two types of processes: single-threaded processes and multi-threaded processes. We know that the process has a process control block PCB, the relevant program segment and the data structure set that the program segment operates on. The execution process of a single-threaded process is macroscopically linear, and microscopically only a single execution The execution process of a multi-threaded process is also linear at the macro level, but there can be multiple execution operations (threads) at the micro level, such as different code fragments and related data structure sets. The change of the thread only represents the change of the CPU execution process, without the change of the resources owned by the process. Apart from the CPU, the allocation of hardware and software resources in the computer has nothing to do with the thread , and the thread can only share the resources of the process to which it belongs. Similar to the process control table and PCB, each thread also has its own thread control table TCB, and the thread state information saved in this TCB is much less than the PCB table, which is mainly related to the pointer stack (system stack and user stack), status data in registers. A process has a complete virtual address space and does not depend on a thread to exist independently; on the contrary, a thread is a part of the process, does not have its own address space, and shares all resources allocated to the process with other threads in the process .

 

    Threads can effectively improve the execution efficiency of the system, but they are not applicable in all computer systems, such as some real-time systems that rarely do process scheduling and switching. The advantage of using threads is that when there are multiple tasks that need to be processed by the processor, the switching time of the processor is reduced; moreover, the system overhead required for the creation and termination of threads is much smaller than the creation and termination of processes. The most suitable systems for using threads are multiprocessor systems and networked or distributed systems.

----------------------------------

1. Execution characteristics of threads.

    A thread has only 3 basic states: ready, executing, blocked.

    There are five basic operations for a thread to switch the state of the thread: fork, block, activate, schedule, and end.

2. Process communication.

    There are four forms of process communication in a stand-alone system: master-slave, conversational, message or mailbox mechanism, and shared storage.

        Typical examples of master-slave: terminal control process and terminal process.

        A typical example of conversational: the communication between the user process and the disk management process.

 

 

refer to

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324807817&siteId=291194637