Parent-child process, process, thread, coroutine, process

1. Process definition

The written code is just a static file stored on the hard disk. After compiling, a binary executable file (stored on the hard disk) will be generated. When werunAfter this executable is loaded, it will be loaded intoin memory, then the CPU will execute every instruction in the program, then thisrunning program, which is called a "process" (Process)
insert image description here
1. Processes are executed concurrently. A
single-core CPU can only run one process at a certain moment. But during 1 second, it may run multiple processes, which creates
the illusion of parallelism, but in fact it is concurrency.

2. Concurrency and parallelism
Concurrency: Hand over tasks to processors at different points in time for processing. At the same point in time, tasks do not run concurrently.
Parallelism: Assign each task to each processor to complete independently. At the same point in time, the tasks must be running concurrently.
insert image description here

Two, the status of the process

1. A process has at least three basic states during its activity, namely running state, ready state, and blocked state.
insert image description here
The meaning of each state in the above figure:

  • Running state (Running): the process occupies the CPU at this moment;
  • Ready state (Ready): can run, temporarily stop running because other processes are running;
  • Blocked state (Blocked): The process is waiting for a certain event to occur (such as waiting for the completion of the input/output operation) and temporarily stops running. At this time, even if it is given CPU control, it cannot run;

Of course, the process has two other basic states:

  • Creation state (new): the state when the process is being created;
  • End state (Exit): The state when the process is disappearing from the system;

Therefore, the transition of a complete process state is as follows:
insert image description here
Reference: "Graphic Operating System" - Kobayashi

3. Process Control Block (PCB: Processing Control Block)

The kernel object of the process: what we usually call PCB (process control block), this structure can only be accessed by the kernel, it is a data structure used by the operating system to manage the process, and the operating system perceives and manages it through this data structure Process; its members are responsible for maintaining various information of the process, including the status of the process (created, ready, running, sleeping, suspended, dead, etc.), message queue, etc.; it is also the place where the system stores statistical information about the process .
In other words, the OS controls and manages concurrently executed processes according to the PCB. The PCB usually occupies a continuous memory space in the system memory, storing all the information used by the operating system to describe the process and control the operation of the process.
PCB is a data structure in the core of the operating system, which mainly represents the state of the process. Its function is to make a program a basic unit that can run independently and can be executed concurrently.
Note: For the specific structure, refer to the task_struct data structure of Linux.

Create a pcb when the process is created, and destroy the pcb when the process terminates

PCB area explain
CPU_state When p stops, the current state of the CPU, which consists of various hardware registers (register) and status flags (flag), is saved in this area. When p resumes execution, the saved information is copied back to the CPU. (e.g. program counter)
process_state Store the current state of process p. For example, running, ready, or blocked.
memory Describes the memory region allocated to p. In the simplest case, this area will point to main memory. On systems using virtual memory, this area can also point to a hierarchy of memory pages or segments.
scheduling_information Contains information used by the scheduler to decide when p will run. This information usually records p's CPU time (CPU time), real time in the system (real time), priority (priority) and any possible deadlines (deadlines).
accounting_informaton Information needed to track counting and billing. For example, the amount of CPU time or memory used.
open_files Keep track of the files currently opened by p.
other_resources Keep track of any resources that p has requested and successfully obtained. For example, a printer.
parent Every process is created by some other running process. The parent process of process p is the process that created p. Information about the parent process of p recorded in the parent area.
children A child process c (child process) of process p is a process created by p. Process p is the parent process of process c. The information of each child process of process p is recorded in the children area.

Reference:
2.3 Process Control Block
[windows operating system] Process Control Block (PCB)

Four, thread

process problem

  • How do processes communicate and share data?
  • The system overhead of maintaining a process is relatively large. For example, when a process is created, resources are allocated and PCBs are built; when a process is terminated, resources are recovered and PCBs are revoked;

So how to solve it? There needs to be a new entity that satisfies the following properties:

  • entities canrun concurrently
  • between entitiesshare the same address space

This new entity, isThread, threads can run concurrently and share the same address space.

1. A thread is an execution flow in a process.
Multiple threads in the same process can share resources such as code segments, data segments, and open files, but each thread has its own set of registers and stacks, which ensures that the control flow of the thread is Advantages of independent
threads:

  • Multiple threads can exist in a process at the same time;
  • Each thread can be executed concurrently;
  • Resources such as address space and files can be shared between threads;

Disadvantages of threads:

  • When a thread in a process crashes, it crashes all threads of the process it belongs to.

2. Thread context switching
As we have seen before, the biggest difference between a thread and a process is that a thread is the basic unit of scheduling, while a process is the basic unit of resource ownership.
Therefore, the so-called task scheduling of the operating system actually schedules threads, and the process only provides resources such as virtual memory and global variables for threads.
In addition, threads also have their own private data, such as stacks and registers.

3. Implementation of threads
There are mainly three implementation methods of threads:

  • User Thread: A thread implemented in user space, not a thread managed by the kernel, is managed by a user-mode thread library;
  • Kernel Thread: A thread implemented in the kernel is a thread managed by the kernel;
  • Lightweight Process (LightWeight Process): supports user threads in the kernel;

3.1. How to understand user thread? What advantages and disadvantages exist?

  • User threads are implemented based on the user-mode thread management library, and the thread control block (Thread Control Block, TCB) is also implemented in the library. The operating system cannot see this TCB, it only Can see the PCB of the whole process.
  • Therefore, the operating system does not directly participate in the entire thread management and scheduling of user threads, but user-level thread library functions complete thread management, including thread creation, termination, synchronization, and scheduling.

Advantages of user threads:

  • Each process needs its private thread control block (TCB) list to track and record its thread state information (PC, stack pointer, register). TCB is maintained by user-level thread library function and can be used for different An operating system that supports thread technology;
  • The switching of user threads is also done by thread library functions, without switching between user mode and kernel mode, so the speed is very fast;

Disadvantages of user threads:

  • Since the operating system does not participate in thread scheduling, if a thread initiates a system call and blocks, none of the user threads contained in the process can execute.
  • When a thread starts running, unless it voluntarily surrenders the right to use the CPU, other threads in the process where it resides cannot run, because user-mode threads cannot interrupt the current running thread. thread, it does not have this privilege, only the operating system has it, but user threads are not managed by the operating system.
  • Since time slices are allocated to processes, compared with other processes, each thread gets less time slices during multi-threaded execution, and the execution will be slower;

3.2. How to understand the kernel thread? What advantages and disadvantages exist?

  • Kernel threads are managed by the operating system, and the TCB corresponding to the thread is naturally placed in the operating system. In this way, the creation, termination, and management of threads are all in charge of the operating system.

Advantages of kernel threads:

  • In a process, if a kernel thread initiates a system call and is blocked, it will not affect the operation of other kernel threads;
  • Assigned to threads, multi-threaded processes get more CPU running time;

Disadvantages of kernel threads:

  • In an operating system that supports kernel threads, the kernel maintains process and thread context information, such as PCB and TCB;
  • The creation, termination and switching of threads are all carried out through system calls, so for the system, the system overhead is relatively large;

3.3. A lightweight process (Light-weight process, LWP) is a user thread supported by the kernel.
A process can have one or more LWPs, and each LWP is mapped to a kernel thread one-to-one, that is, LWP is supported by a kernel thread.
In addition, LWP can only be managed by the kernel and be scheduled like a normal process. The Linux kernel is a typical example of supporting LWP.

Five, process and thread comparison

insert image description here
1. The comparison between threads and processes is as follows:

  • A process is a unit for resource allocation (including memory, open files, etc.), and a thread is a unit for CPU scheduling;
  • Processes have a complete resource platform, while threads only exclusively share essential resources, such as registers and stacks;
  • Threads also have three basic states of ready, blocked, and executing, and also have transition relationships between states;
  • Threads can reduce the time and space overhead of concurrent execution;

2. Compared with processes, threads can reduce overhead, which is reflected in:

  • The thread creation time is faster than the process, because the process also needs resource management information, such as memory management information and file management information, while the thread creation process does not involve these resource management information. is to share them;
  • The termination time of a thread is faster than that of a process, because the resources released by the thread are much less than that of the process; the
    thread switching within the same process is faster than the process switching, because the threads have the same address space (virtual memory sharing), which means the same If the threads of two processes all have the same page table, then there is no need to switch the page table when switching. For the switching between processes, the page table must be switched when switching, and the switching process of the page table is relatively expensive;
  • Since the threads of the same process share memory and file resources, the data transfer between threads does not need to go through the kernel, which makes the data interaction between threads more efficient;

6. Coroutine

insert image description here
A coroutine is a thread-based, but lighter-weight existence than a thread. This kind of lightweight thread managed by a program is also called a user-space thread, which is invisible to the kernel. Just as there are multiple threads in a process, there can also be multiple coroutines in a thread.

The coroutine also has its own registers, context, and stack at runtime. The scheduling of the coroutine is completely controlled by the user. When the coroutine schedule is switched, the register context and stack will be saved in the allocated private memory area. When switching back, Restore the previously saved register context and stack, and directly operate the stack without the overhead of kernel switching, and can access global variables without locking, so context switching is very fast. (Of course, there are also stackless coroutines, and there will be chapters to explain detailed definitions and code implementations later.)
Refer to
(14) In-depth concurrent threads, processes, fibers, coroutines, monitors and deadlocks, livelocks, and lock hunger Detailed
explanation opens the door of thread | process | coroutine

Seven, Qiancheng

Fiber (Fiber) is a concept added to the operating system by the Microsoft organization to help enterprise programs be better ported to the Windows system. It is controlled by the operating system kernel according to the corresponding scheduling algorithm. It is also a lightweight thread.

  • We will talk about the actual code of Qiancheng later. In fact, after reading the code, you will feel that Qiancheng is more like the realization of the concept of LWP. But there is no introduction to the article, let us talk about our personal understanding here
    insert image description here

8. Coroutine and Qiancheng

The concepts of fiber and coroutine are the same, both are many-to-one models of threads, but they are distinguished in some places, but from the essential concept of coroutine: the concepts of fiber, green thread, and micro-thread all belong to the scope of coroutine . The difference between fibers and coroutines is:

  • Fibers are implemented at the OS level, while coroutines are implemented at the language level. Fibers are controlled by the OS kernel, and coroutines are invisible to the kernel.

If there are any mistakes or deficiencies, welcome to comment and point out! Creation is not easy, please indicate the source for reprinting. If it is helpful, remember to like and follow (⊙o⊙)
For more content, please follow my personal blog: https://blog.csdn.net/qq_43148810

Guess you like

Origin blog.csdn.net/qq_43148810/article/details/130873489