One, foreword
Whether it’s in your college course operating Systems, whether it’s in your job interview, whether it’s in your daily work communication, processes and threads are very important, and some of you may have heard of coroutines. So what exactly are they? What is the relationship between them? Read on with me!
Computer composition and operating system
Before we talk about processes, let’s review what a computer is made of.
-
Central processing Unit (CPU) : The core of a computer, responsible for some of the computing and control
CPU has two core components, ALU (arithmetic logic unit) and CU (logic control unit). ALU is responsible for some operations, including arithmetic operations (addition, subtraction, multiplication and division), logical operations (and or not) and relational operations (size equals). CU is responsible for the coordination of logical components and acts as a commander. The CPU also has registers, albeit of very small size, which are used to cache the intermediate results of some calculations.
-
Main storage: Stores data
Divided into RAM and ROM. RAM, or random access memory, is commonly known as memory; ROM, read only memory, power off data is not lost, is commonly known as disk.
-
IO: various input and output devices (mouse, keyboard, monitor, network card, sound card, video card, etc.)
With the hardware support of these computers, we can do all kinds of things. If we manually controlled the CPU and memory, we would probably have to write all kinds of low-level instructions, and there would certainly be all kinds of bugs. Operating system on the basis of computer hardware, encapsulation of the hardware implementation details, abstraction of the upper layer of a more convenient system call instructions, this is the operating system.
In a multitasking system, the operating system takes over all hardware resources and holds the highest authority over hardware control. Programs executed in the operating system run as processes with lower privileges. All hardware resources are allocated by the operating system according to the priorities and running status of processes.
Three, processes,
With an operating system, we can implement programs on it. For example, through a QQ to achieve the chat function. How does the QQ program start up? The operating system throws the QQ program into a container and executes it, and this container is the process. What is the relationship between process and program?
- Programs are dead (just a bunch of instruction code on disk), processes are alive (occupying CPU, memory, file resources, IO, etc.)
- A process is a running instance of a program
Four, thread
Wikipedia: A thread is the smallest unit in which an operating system can schedule operations. It is contained within the process and is the actual operating unit within the process. A thread is a single sequential flow of control in a process, and multiple threads can be concurrent in a process, each performing a different task in parallel.
A process has multiple threads. Take QQ as an example, there needs to be a thread to monitor keyboard input and convert it into text, and there needs to be a thread to pull messages sent by the other party, etc. From an operating system perspective:
- Processes are the smallest allocation unit of resource management
- Threads are the smallest unit of execution
Both processes and threads are controlled by the operating system. Threads, for example, have multiple execution states: initialized, runable, running, blocked, and destroyed. The transformation relationship of the five states is as follows:
Thread state transformation is changed by the TCB thread control block in the operating system kernel, which requires CPU resources.
To summarize, a process is a running instance of a program that manages various resources; A process has more than one thread, thread is the specific execution unit, they share part of the process resources, also has private resources (PC program counter, execution stack, etc.), thread by default through shared memory interaction, synchronization between threads through locks/semaphore and other mutually exclusive operations. Since thread switching requires switching between kernel/user modes in the operating system to change state, thread switching itself can be very costly.
Fifth, coroutines
Coroutines, also known as microthreads, fibers. English name Coroutine. More lightweight than threads, just as a process has multiple threads, a thread can have multiple coroutines.
Take an example from Liao Xuefeng:
def a(a):
print 1
print 2
def b(a):
print 'A'
print 'B'
Copy the code
When we call a and B, we’re going to print 1, 2, A, b. What if I want to print 1, A, b, 2, or 1, A, 2, b? Obviously you can’t control it through multiple threads. So we can do it by coroutines. Compared with thread interrupt, it is a bit like CPU interrupt. It does not need the intervention of the operating system and is highly efficient.
The Python language itself supports coroutines, through the yield keyword.
def a(a):
print 1
yield
print 2
yield
def b(a):
print 'A'
print 'B'
generator=a()
next(generator)
b()
generator.send(' ')
Copy the code
The statement prints:
1
A
B
2
Copy the code
- By calling the
a()
Generate an actuator - through
next
Trigger the actuator to executea
And print the1
- call
b
printA
andB
- through
send
The actuator sends a signal to continue execution and print2
The whole process is executed by a thread, and the execution process is controlled by some syntax. It does not involve locking or thread switching. It assists A and B to work cooperatively through keywords, so it is called coroutine.
ES6 introduced for.. of.. , which is itself a syntactic sugar, is implemented by introducing iterators, which are implemented by coroutines. More intuitive async/await ‘has been introduced in ES7.
Java itself does not support coroutines, but it can be implemented through tripartite libraries such as Quasar and Akka.
In the next article, we’ll learn more about coroutines through the different implementations of JS and Java.
References:
- The wiki process
- Ruan Yifeng << A Simple Explanation of Processes and threads >>
- Comics: What is a coroutine?
- Understand coroutines
- Liao Xuefeng coroutine
- Coroutines and event loops