Comparison and differences between processes and threads in Linux

  • 2020-06-12 11:41:14
  • OfStack

Comparison and differences between processes and threads in Linux

1. The concept

Process: A running program.
Thread: One execution path in a process.

2. The difference between

(1) Usually, a process can contain several threads, which can make use of the resources owned by the process. In operating systems where threads are introduced, it is common to treat the process as the basic unit for allocating resources, and the thread as the basic unit for running and scheduling independently.
(2) The difference between thread and process is that child process and parent process have different code and data space, while multiple threads share the data space, and each thread has its own execution stack and program counter for its execution context. Multithreading is mainly to save CPU time, play and use, according to the specific situation. The thread runs using the machine's memory resources and CPU.
(3) Processes are independent of each other and Shared with each thread of process 1. Threads within one process are not visible to other processes.
(4) The thread context switch is much faster than the inter-process context switch.
(5) A process is an unexecutable entity. A program is an inanimate entity. Only when the processor gives the program life can it become an active entity.

3. What is the relationship and difference between threads and processes?

The relationship between processes and threads:

(1) A thread can only belong to a process, and a process can have multiple threads, but at least one thread.
(2) Resources are allocated to the process, and all resources of the process are Shared with all threads of the process 1.
(3) The processor is allocated to threads, that is, it is threads that actually run on the processor.
(4) The thread needs to cooperate and synchronize during execution. Message communication is used to synchronize threads of different processes. A thread is a unit of execution within a process that is also a schedulable entity within the process.

Differences between processes and threads:

(1) Scheduling: Thread as the basic unit of scheduling and allocation, process as the basic unit of owning resources
(2) Concurrency: Concurrent execution can be performed not only between processes, but also between multiple threads of a process
(3) Resource ownership: The process is an independent unit that owns resources. The thread does not own system resources, but can access the resources belonging to the process.
(4) System overhead: When a process is created or undone, the system has to allocate and reclaim resources for it, resulting in a significantly higher overhead than when the thread is created or undone.

The above is a detailed explanation of the difference between threads and processes in Linux. If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading.


Related articles: