From the Linux kernel to see the similarities and differences between processes and threads

 The third chapter of "Linux Kernel Design and Implementation" explains process management. The difference between the concepts of process and thread is still blurred. The book says:

    A process is a program in the execution period. But the process is not limited to a piece of executable program code. Usually the process also contains other resources, such as open files, pending signals, internal kernel data, device status, one or more memory address spaces with memory mapping, and one or more execution threads, and of course it also includes Store the data segment of global variables, etc.

    However, each thread has an independent program counter, process stack, and a set of process registers. The following sentence: The object of kernel scheduling is a thread, not a process. In fact, the premise of this sentence is that a process contains only one thread. In the current operating system, a process includes multiple threads.

    Task (task) is the most abstract, is a general term, refers to an activity completed by software. A task can be either a process or a thread. In short, it refers to a series of operations that collectively achieve a certain purpose. For example, read data and put data into memory. This task can be implemented as a process or as a thread (or as an interrupt task).

  Process is often defined as the execution of a program. A process can be regarded as an independent program, with its complete data space and code space in memory. The data and variables owned by a process belong only to itself.

  A thread is a program that runs independently all the way in a certain process. In other words, the thread exists in the process. A process is composed of one or more threads, and each thread shares the same code and global data, but each has its own stack. Since the stack is one for each thread, local variables are private to each thread. Since all threads share the same code and global data, they are closer than processes and tend to interact more than separate processes. Interaction between threads is easier because they have some shared memory for communication. : Global data of the process.

  The most significant difference between a process and a thread is that the thread has its own global data. Threads exist in the process, so the global variables of a process are shared by all threads. Because threads share the same system area, the resources allocated by the operating system to a process are available to all threads of that process, just as global data is available to all threads.

In short, a program has at least one process, and a process has at least one thread. The division scale of threads is smaller than that of processes, which makes multithreaded programs have high concurrency. In addition, the process has an independent memory unit during execution, and multiple threads share memory, which greatly improves the efficiency of the program. Threads are still different from processes in the execution process. Each independent thread has a program entry, sequential execution sequence, and program exit. However, threads cannot be executed independently, and must be stored in the application program, and the application program provides multiple thread execution control. From a logical point of view, the meaning of multithreading is that in an application, multiple execution parts can be executed at the same time. However, the operating system does not regard multiple threads as multiple independent applications to implement process scheduling and management and resource allocation. This is the important difference between processes and threads.

A thread can create and cancel another thread; multiple threads in the same process can execute concurrently.

The main difference between processes and threads is that they are different operating system resource management methods. A process has an independent address space. After a process crashes, it will not affect other processes in the protected mode, and a thread is just a different execution path in a process. Threads have their own stacks and local variables, but there is no separate address space between threads. The death of a thread means the death of the entire process. Therefore, a multi-process program is more robust than a multi-threaded program, but it costs more when switching between processes. The resources are larger and the efficiency is worse. But for some concurrent operations that require simultaneous execution and share certain variables, only threads can be used, not processes.

 

Process concept 
   Process is the basic unit of resource allocation and the basic unit of scheduling operation. For example, when users run their own programs, the system creates a process and allocates resources for it, including various tables, memory space, disk space, I/O devices, and so on. Then, put the process in the ready queue of the process. The process scheduler selects it, allocates CPU and other related resources to it, and the process really runs. Therefore, the process is the unit of concurrent execution in the system. 
In Mac, Windows NT and other operating systems that adopt a micro-kernel structure, the function of the process has changed: it is only the unit of resource allocation, not the unit of scheduling operation. In the microkernel system, the basic unit of real scheduling operation is the thread. Therefore, the unit of concurrency is the thread.


Thread concept 
  Thread is the smallest unit of performing operations in a process, that is, the basic unit of execution processor scheduling. If the process is understood as a logical task completed by the operating system, then the thread represents one of many possible subtasks to complete the task. For example, suppose the user starts a database application in a window, the operating system will represent the call to the database as a process. Assuming that the user wants to generate a payroll report from the database and transfer it to a file, this is a subtask; in the process of generating the payroll report, the user can input the database query request, which is another subtask . In this way, the operating system represents each request-payroll report and new input data query as an independent thread in the database process.

     Threads can be independently scheduled for execution on the processor, so that in a multi-processor environment, several threads are allowed to execute on a separate processor. The operating system provides threads for the convenience and effective realization of this concurrency.


The benefits of introducing threads
(1) Easy to schedule.
(2) Improve concurrency. Concurrency can be easily and effectively achieved through threads. A process can create multiple threads to execute different parts of the same program.
(3) Less overhead. Creating a thread is faster than creating a process and requires very little overhead.
(4) Conducive to give full play to the functions of multi-processors. By creating a multi-threaded process (that is, a process can have two or more threads), each thread runs on a processor, so as to achieve the concurrency of the application, so that each processor can be fully run. 


The relationship between processes and threads
(1) A thread can only belong to one process, and a process can have multiple threads, but at least one thread.
(2) Resources are allocated to the process, and all threads of the same process share all the resources of the process.
(3) The processor is assigned to threads, that is, threads are actually running on the processor.
(4) Threads need to be coordinated and synchronized during execution. The threads of different processes must use message communication to achieve synchronization.

    But for the Linux operating system, the object of scheduling is the thread rather than the process. A large part of the reason is that linux thinks that threads and processes are the same.

Another very appropriate explanation about processes and threads: http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html

Guess you like

Origin blog.csdn.net/smilejiasmile/article/details/109389607