Miscellaneous Notes on Operating System Knowledge

1. What is the difference between a process and a thread? When to use process? When to use threads?

A process refers to an application that is running in the system; a program is a process once it runs, or more specifically: a process refers to an instance of a program when it is executed. A thread is an entity of a process. Process - the smallest unit of resource allocation, thread - the smallest unit of program execution.
The difference between thread processes is reflected in several aspects:

  1. Because the process has independent stack space and data segment, so whenever a new process is started, it must be assigned an independent address space, and numerous data tables are established to maintain its code segment, stack segment and data segment, which is very important for multi-process It is very "luxury", and the system overhead is relatively large, but the thread is different, the thread has an independent stack space, but shares the data segment, they use the same address space with each other, share most of the data, and are more economical than the process, overhead It is relatively small, and the switching speed is faster than that of the process, and the efficiency is high. However, due to the independent characteristics of the processes, the security of the process is relatively high, and because the process has an independent address space. After a process crashes, it will not have an impact on other processes, while threads are just different execution paths within a process. When one thread dies, the whole process dies.
  2. Reflected in the communication mechanism, because the processes do not interfere with each other and are independent of each other, the communication mechanism of the process is relatively complicated, such as pipelines, signals, message queues, shared memory, sockets and other communication mechanisms, while threads share data segments So the communication mechanism is very convenient.
  3. All threads belonging to the same process share all resources of that process, including file descriptors. The different processes are independent of each other.
  4. A thread is also called a lightweight process, a process has a process control block, and a thread has a thread control block;
  5. A thread must belong to only one process, and a process can have multiple threads and at least one thread;

Follow-up continuous update

Guess you like

Origin blog.csdn.net/qq_41290252/article/details/117912716