Sinsing analysis of threads and processes in Linux

Source: Internet
Author: User

Processes and threads are basic units that are run by the operating system's programs, and the system uses the unit to implement the concurrency of the system to the application. A process has at least one thread, and a thread belongs to at least one process. The difference between a process and a thread is that the thread is scaled less than the process, which makes the multithreaded procedure more concurrency-efficient.

And the process has a separate memory unit during execution, while multiple threads share memory, which greatly improves the efficiency of the program operation. Threads are still different from the process during execution. Each separate thread has a program run entry, sequence of sequence execution, and exit of the program.

From a logical point of view, the meaning of multithreading is that in one application, multiple execution parts can be executed concurrently, but the operating system does not treat multiple threads as separate applications to implement scheduling and management of processes and resource allocation. This is the important difference between processes and threads.

A process is a program with a certain set of independent functions for this run activity on a data collection, and the process is also an independent unit of system process resource allocation and scheduling. A thread is an entity of a process that is the basic unit of CPU scheduling and allocation. A thread is a smaller unit that can run independently than a process, and the thread itself basically does not own the system resources, it has only a few resources that are essential in the run, such as program counters and a set of registers and stacks, but it can share all of the resources owned by the process with other threads of the same process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process.

The main difference between a process and a thread is that they are different ways to manage the operating system resources, the process has a separate address space, and after a process crashes, no other processes are affected in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process to die, so the multi-process program is more robust than multi-threaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.

The same points for processes and threads:

(1) Both have ID, a set of registers, status, priority, and the scheduling policy to follow.

(2) Each process has a process control block, and the thread also has a thread control block.

(3) Both threads and the creator of a child process can have some control on the thread and child processes, such as the creator can cancel, suspend, resume, and modify the priority of threads and child processes. However, threads and child processes can also change their properties and create new resources.

Different points in the process and thread:

(1) A thread is a part of a process, a process without threads can be viewed as single-threaded, and if there are multiple processes within a process, the process is not executed by one thread, but by multiple threads.

(2) Starting a thread is much less than the space it takes to start a process, and the time it takes to switch between threads is much less than the time it takes to switch between processes.

(3) The system allocates different areas of memory for each process at run time, but does not allocate memory for the thread because the resource used by the thread is the resource of the process to which it belongs, and the county group can only share resources. For different processes, they have independent data space, the data can only be transmitted through the way of communication, which is not only time-consuming, but also inconvenient. And the data of a thread can be used by other threads, not only fast, but also convenient.

(4) Similar to the Process Control table PCB, the thread also has its own control table TCB, but the TCB holds a much smaller state of the thread than the PCB.

(5) A process is a basic unit of the system's allocation of resources, with a complete virtual address space, independent of threads.

Sinsing analysis of threads and processes in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.