Differences between processes and threads and between processes and threads

Source: Internet
Author: User

Differences between processes and threads and between processes and threads

1. Definition

A process is a running activity of a program with certain independent functions. A process is an independent unit for the system to allocate and schedule resources.

A thread is an entity of a process. It is the basic unit for CPU scheduling and scheduling. It is smaller than a process and can run independently. the thread itself basically does not have system resources, and only has a few resources (such as program counters, a set of registers and stacks) that are essential for running ), however, it can share all resources of a process with other threads of the same process.


2. Relationship

One thread can create and cancel another thread, and multiple threads in the same process can be concurrently executed.

A thread is a concept closer to the execution body. It can share data with other threads in the same process, but has its own stack space and independent execution sequence.


3. Differences

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 protection mode, but the thread is only a different execution path in the process. A thread has its own stack and local variables, but there is no separate address space between threads. If a thread dies, the whole process dies. Therefore, multi-process programs are more robust than multi-threaded programs, however, during process switching, resources are consumed and the efficiency is lower.But for some concurrent operations that require simultaneous and shared variables, you can only use threads, not processes.

1) In short, a program has at least one process, and a process has at least one thread.

2) The thread division scale is smaller than the process, making the multi-threaded program highly concurrent.

3) In addition, the process has independent memory units during execution, and multiple threads share the memory, which greatly improves the program running efficiency.

4) there is a difference between a thread and a process during execution. Each Independent thread has a program running entry, sequence execution sequence, and program exit.But the thread cannot be executed independently,The application must provide multiple thread execution control according to the existing application.

5) Logically, multithreading means that multiple execution parts can be executed simultaneously in an application. However, the operating system does not view multiple threads as multiple independent applications to implement process scheduling, management, and resource allocation.This is an important difference between processes and threads.

4. Advantages and Disadvantages

The use of threads and processes has their own advantages and disadvantages: the thread execution overhead is small, but it is not conducive to resource management and protection. The process is the opposite. At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines.


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.