[Basic knowledge] differences between threads and processes

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

I sorted out some online materials and added some of them myself.

1. Similarities:
(A) Both have IDs, a set of registers, statuses, priorities, and scheduling policies to be followed.
(B) each process has a process control block and a thread control block.
(C) threads and child processes share resources in the parent process; threads and child processes are independent of their parent processes and compete for the use of processor resources; the creator of the thread and sub-process can implement certain control on the thread and sub-process. For example, the creator can cancel, suspend, continue, and modify the priority of the thread and sub-process; threads and sub-processes can change their attributes and create new resources.

2. Differences:
(A) A thread is a part of a process. A process without threads can be considered as a single thread. If a process has multiple processes, the execution process of a process is not completed by a line (thread), but by multiple lines (threads.
(B) It takes much less space to start a thread than to start a process, and the time required for switching between threads is much less than the time required for switching between processes.
(C) When running, the system allocates different memory regions for each process, but does not allocate memory for the thread (the resources used by the thread are the resources of the process to which it belongs ), thread groups can only share resources. For different processes, they have independent data space, and data transmission can only be performed through communication. This method is not only time-consuming, but also inconvenient. The data of one thread can be directly used by other threads, which is fast and convenient.
(D) similar to the process control table PCB, the thread also has its own control table TCB, but the thread state stored in TCB is much less than that in the PCB table.
(E) A process is a basic unit for allocating all resources of the system. It has a complete virtual space address and does not depend on threads but exists independently.

In addition, the thread sharing and exclusive resources are supplemented.

Environments shared by threads include process code segments and public data of processes (using the shared data, threads can easily communicate with each other) process open file descriptor, signal processor, current directory of the Process, process user ID and process group ID.

Thread personality includes:
1. Thread ID: Each thread has its own thread ID, which is unique in this process. The process uses this to identify the thread.

2. value of the register group: Because threads run concurrently, each thread has its own different running clues. When switching from one thread to another, the status of the registers set of the original thread must be saved so that the thread can be restored when it is switched over again.

3. Thread Stack: A stack is required to ensure independent running of threads. A thread function can call a function, and the called function can be nested in layers. Therefore, a thread must have its own function stack so that function calls can be executed normally without the influence of other threads.

4. Error return code
Because many threads in the same process are running at the same time, the errno value may be set after a thread calls the system, but this error has not been processed by this thread, another thread is put into operation by the scheduler at this time, so that the error value may be modified. Therefore, different threads should have their own error return code variables.

5. Thread signal shielding code
Because each thread is interested in different signals, the thread's signal shielding code should be managed by the thread itself. But all threads share the same signal processor.

6. thread priority
Because a thread needs to be scheduled as a process, a parameter must be available for scheduling. this parameter is the priority of the thread.

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.