Interview questions; detailed explanation of process, thread and multi-threading concepts

1. Overview 
In Windows, every open and running application or background program, such as running QQ, Google Chrome, NetEase Cloud Music, Explorer, etc., is a process. We feel that these programs are running "simultaneously", but in fact, a processor can only run one process at a time, but the CPU is executing in a high-speed rotation, which gives us the illusion that the reason we cannot feel the interruption is the execution speed of the CPU It's way too fast relative to what we feel.

We learn c, write java, and most of the time we do single-threaded programming. There is only one main line of sequential execution - the program starts from the main method and executes the code in sequence until all the code is executed, unless a line of code error causes the main line to block.

Multithreading is the "simultaneous" execution of multiple sequential execution streams without interfering with each other. Multi-threaded application scenarios, take a very simple example, we write a Web site backend, if only supports a single thread, then only one user can access the site at the same time, is this realistic? Should we create a thread for each user so that each user can access the site "simultaneously".

Here, all my "simultaneous" are in quotation marks, because in the computer field, macro "simultaneous" and microscopic "simultaneous" are different, they are called concurrency and parallelism.

Concurrency means that only one instruction (or one process, one thread) can run at the same time, but because the CPU's rotation execution speed is beyond imagination, from a macro perspective, there are multiple instructions executing at the same time. 
Parallelism, as we usually understand, is the simultaneous execution of multiple instructions on multiple processors at the same time.

Second, the process details

1. Concept 
We all know programs. A program is static and usually stored in external memory. When a program is called into memory to run, it becomes a process. 
As the name implies, a process is a program in progress, and it is a dynamic concept. It is the basic unit of system resource allocation and scheduling.

2. Processes generally have the following three characteristics: 
a. Independence: each process has its own independent resources and private address space, the size of which is related to the number of processors, such as Win32 system, 32-bit address mapping 4GB address space , the 2GB of the low address is used as the virtual address space of the user mode (the application can be shared, and the threads are independent), and the 2GB of the high address is used as the virtual address space of the kernel mode (used by the system). 
b. Dynamic: This can be seen from the concept of process, the running program is the process. There are dynamic concepts such as time, state ( detailed in the blog post), and life cycle in the process.  
c. Concurrency: Multiple processes execute concurrently on a single processor.

3. The process mainly includes three parts: 
a. Program code: used to describe the function to be completed by the process. 
b. Data collection: the data and work area required for program execution. 
c. PCB program control block: It contains the description information and control information of the process, which is the only sign of the process. It is precisely because of the PCB that the process becomes a dynamic concept.

3. Thread and multi-threading in detail

1. Concept, discussed by the relationship between thread and process

A process may contain many sequential execution flows, and each sequential execution flow is a thread. 
Most operating systems now use preemptive multitasking operation strategies to support the concurrency of multiple processes, while multithreading is an extension of multiple processes, enabling a process to process multiple tasks concurrently like a processor. Threads are concurrent processes within processes. The basic unit of execution , a thread is also called a "lightweight process". A process can contain multiple threads, each thread has its parent process.

2. Thread resources

We may have known Java's memory mechanism, heard the terms "thread private" and "thread shared", method stacks (store local variables through the local variable table in the stack frame), program counters are thread private, and their The memory space is dedicated to a thread; while the resources of the parent process (such as code segments, process public data) are shared between child threads, and the programming of the shared resource area should be very careful to ensure that it will not affect other threads.

3. The advantages of multithreading are discussed by the difference between threads and processes

a. Compared with separate processes, independent threads have a smaller degree of isolation, because threads share the resources of the process, and it is easier to realize inter-thread communication. 
b. The system creates a process to allocate resources for the process, and to create a thread , it only needs to allocate the private resources of the thread , and the cost is much lower. It is more efficient to use multi-threading instead of multi-process to achieve high concurrency. 
c. Multi-threaded programming is more convenient, and it can be realized at the program code level.

Guess you like

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