Difference between process and thread and coroutine, process thread coroutine

Source: Internet
Author: User

Difference between process and thread and coroutine, process thread coroutine

Now multi-process and multi-thread is a common concept, and coroutine has become popular in recent years. There is also a coroutine library in python, and tornado also uses the coroutine encapsulated by gevent. This article describes the differences among processes, threads, and threads.

I. Concepts

1. Process

A process is a running activity of a program with certain independent functions on which a dataset is closed. A process is an independent unit for the system to allocate and schedule resources. Each process has its own independent memory space, and different processes communicate through inter-process communication. Because processes are heavy and occupy independent memory, switching overhead (stack, register, virtual memory, file handle, etc.) between context processes is relatively large, but relatively stable and secure.

2. threads

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. Inter-thread communication mainly relies on shared memory. Context switching is fast and the resource overhead is low. However, compared with the unstable process, data is easy to be lost.

3. coroutine

Coroutine is a lightweight thread in user mode,The scheduling of coroutine is completely controlled by the user. Coroutine has its own register context and stack. During the coroutine scheduling switch, the register context and stack are saved to other places. When the switch is back, the previously saved register context and stack are restored, direct stack operations have no overhead for Kernel switching, and global variables can be accessed without locking. Therefore, context switching is fast.

 

Ii. Differences:

1. Comparison between multiple processes and threads

A thread is an execution unit in a process and a schedulable entity in the process. Differences between threads and processes:
1) address space: A thread is an execution unit in a process. There is at least one thread in the process. They share the address space of the process, and the process has its own address space.
2) resource ownership: a process is the unit of resource allocation and ownership. threads in the same process share the resources of the process.
3) threads are the basic unit of Processor Scheduling, but processes are not
4) both can be executed concurrently.

5) each independent thread has a program running entry, sequence of execution, and program exit, but the thread cannot be executed independently and must exist in the application, multiple thread execution control provided by the application

2. Compare coroutine with Thread

1) A single thread can have multiple threads, and a single process can have multiple threads separately, so that python can use multi-core CPU.

2) thread processes are both synchronous, while coroutines are asynchronous.

3) The Association can retain the status of the last call. When the process is re-accessed, it is equivalent to entering the status of the last call.

 

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.