The difference between a Python thread and a process

Source: Internet
Author: User

Basic concepts of processes

Concept
A process is a dynamic execution of a program on a data set. The process is generally composed of three parts: program, data set and process control block. The program we write is used to describe what the process is going to accomplish and how it is done, and the data set is the resource that the program needs to use in its execution, and the process control block is used to document the external characteristics of the process, to describe the process of its execution, and it can be used to control and manage the process, which is the only sign of

Basic Concepts of threading

Concept
A thread is the smallest unit of execution in a process, is an entity in a process, is the basic unit of dispatch and dispatch by the system, the thread does not own the system resources, and only has the resources necessary to run it, but it can share all the resources owned by the process with other threads belonging to one process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process.

Benefits

    • Easy scheduling.
    • Improves concurrency by threading to facilitate and efficiently implement concurrency. A process can create multiple threads to perform different parts of the same program.
    • Less overhead, creating a line turndown create a process faster, with little overhead required

The relationship between the basic state of a process and its state
Status: Running, blocking, pending blocking, ready, pending

Transitions between states:

    1. The ready process is executed by the CPU and becomes the running state;
    2. A running process, making an I/O request or not getting the requested resource, into a blocking state;
    3. Running process, process execution (or time slice has arrived), become ready state;
    4. The blocked process hangs, becomes a suspended blocking state, when the I/O operation that causes the process to block is completed before the user restarts the process (called Wake-up), the suspend blocking state becomes the suspend-ready state, and when the user restarts the process before the I/O operation is completed, the suspend blocking state becomes the blocking state;
    5. Suspends a process in readiness (or running) into a pending ready state, and when the process resumes, the pending ready state becomes the ready state;
Relationship of process and thread:
    • A thread can belong to only one process, while a process may have multiple threads, but at least one thread.
    • A resource is assigned to a process, and all the threads of the same process share all the resources of that process.
    • The processor is assigned to a thread, that is, a thread that actually runs on the processing machine.
    • Threads need to be synchronized during execution. Synchronization is achieved between threads of different processes using the means of message communication. A thread is an execution unit within a process and a scheduler within a process.

# # #进程与线程的区别:

    • Run differently

      • a process cannot be executed alone, it is just a collection of resources. The
      • process must first create a thread to manipulate the CPU.
      • All threads in the same process are the same memory space occupied by the same process. The first thread in the
    • Relationship

      • process is the main threads, the main thread can create other threads, and the other threads can create threads, and the threads are equal. The
      • process has a parent process and a child process, a separate memory space, and a unique identifier: PID.
    • Speed

      • startup thread is faster than START process
      • run thread and run process speed is the same, no comparability
      • thread shared memory space, process memory is unique of the state.
    • Create a

      • parent process to generate a child process, which is equivalent to copying a copy of the memory space, cannot be accessed directly between processes
      • creating a new thread is simple, creating a new process requires a copy of the parent process
      • one thread can control and manipulate other threads thread the sibling line, but the process can only manipulate child processes.
    • Interactive

      • the threads in the same process can be accessed directly.
      • two processes want to communicate must be implemented through an intermediary agent.
    • schedules
      • threads as the basic unit for dispatch and allocation, and processes as basic units that have resources
    • concurrency
      • not only can processes concurrently Rows, and concurrent execution of
    • between multiple threads of the same process has resources: The
      • process is a separate unit of the owning resource, and the thread does not own the system resources, but can access the resources that belong to the process.
    • system overhead:
      • When a process is created or revoked, the cost of the system is significantly greater than the cost of creating or revoking the thread, because the system allocates and reclaims resources for it.

The Python thread differs from the process

Related Article

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.