I don't know if I have such a dense fear myself , Put together a bunch of concepts that look similar and relevant , It looks a bit numb , It's much more comfortable to stroke .
Mission 、 Homework （Job,Task,Schedule）
Before the concept of process , Process has such a name .
In order to allow multiple programs to be concurrent （ Only one is running at the same time , But it feels like multiple simultaneous operations ; parallel （ There are really many running at the same time , It doesn't feel like more than one ）） Implementation , The operating system needs a structure to abstract and represent the operation of this program .
- A process is an abstract structure of the operating system for a running program .
- A process is a basic unit that can run independently in an operating system and be used for resource allocation , Commanded by a set of machines 、 Data and stack can run independently of the active entity .
- The operating system can run multiple processes at the same time , Multiple processes can directly execute and exchange information concurrently .
- A process needs some resources to run , Such as
- List item
CPU、 Storage space and I/O Equipment etc. .
Process is the basic unit of resource allocation , Process scheduling involves a lot of content （ Storage space ,CPU,I/O Resources, etc , Process site protection ）, Scheduling costs a lot , In the concurrent switching process, the efficiency is low . For more efficient scheduling , The basic unit of independent operation and scheduling, which is lighter than the process, is proposed .
- Threads are lighter than processes
- Threads can run independently , Independent scheduling , Has the resources （ It's usually CPU resources , Program counter, etc ）
- Thread scheduling can greatly reduce the cost of scheduling （ Relative to the process ）, Thread switching will not cause process switching
- The introduction of threads further improves the concurrency of the operating system , Threads can execute concurrently
- Multiple threads of the same process share the resources of the process （ It saves a lot of work of resource scheduling and on-site protection ）
coroutines 、 Common procedure 、Coroutine
- A coroutine is a lightweight thread in user mode , The kernel of the operating system knows nothing about the process
- The scheduling of the cooperation process is completely controlled by the application program , The operating system doesn't care about the scheduling of this part
- A thread can contain one or more coroutines
- It has its own register context and stack , When the coordination scheduling is switched , Save up and down the register and stack , Restore the storage context and stack of the previous shipment when switching back
- The process can keep the state of the last call , See all kinds of generators here （ A generator is a castrated process ） The concept of ..
- Windows The next implementation is called fiber
Code portability has always been the focus of interaction between platforms , In will reference the program from Unix Migration to Windows In the process of , There will be some inconsistencies similar to thread stack management 、 Structure and exception handling , Increase the difficulty and cost of transplantation .
To help companies move faster 、 More correctly porting their code to Windows,Microsoft Added fiber in the operating system （Fiber）. Thread vs fiber , It has the following characteristics ：
- The thread is in Windows Implemented in the kernel , The operating system will schedule the threads according to the scheduling algorithm of the system .
- Fiber is implemented in user mode , The core knows nothing about fiber path .
- Fiber is a lighter thread , A thread can contain one or more fibers
- The kernel will do preemptive scheduling of threads , Threads can only execute one fiber of code at a time （ Which fiber to execute is determined by the user scheduling algorithm ）
- There is no direct relationship between fiber scheduling and thread scheduling , The operating system may seize the running right of the thread where the fiber is located at any time
- Unless the running fiber is explicitly switched to another fiber , Otherwise, other fibers will not work
- Windows There is a set API In other words, threads are converted to fibers or multiple fibers are created in the same thread
Finally, a dividing line is added because the pipe is not the same as the above concepts , Although it looks like , It's like Car and Bar equally .
The concept of critical resources ：
- Resources that can only be accessed by one process at a time
- Multiple processes can only mutually exclusive access resources
Access to critical resources needs to be synchronized , For example, semaphore is a convenient and effective process synchronization mechanism . But the semaphore approach requires that every process accessing critical resources have wait and signal operation . In this way, a large number of synchronization operations are scattered in various processes , It not only brings troubles to system management , And it will cause deadlock due to improper use of synchronization operation . Management is to solve such a problem .
Various software and hardware resources managed in the operating system , Data structures can be used to describe their resource properties abstractly , That is, the resource is characterized by a small amount of information and operations performed on the resource , And ignore their internal structure and implementation details . Using the shared data structure to represent the shared resources in the system . The operation of the shared data structure is defined as a set of processes , Such as resource request and release process request and release. Process's request for Shared resources 、 Release and other operations , All through this set of procedures to the operation of the Shared data structure to achieve , This group of processes can also accept or block access of processes according to resource conditions , Make sure that only one process uses the shared resource at a time , This enables unified management of all access to Shared resources , Achieve critical resource exclusive access .
Management is a resource management module of an operating system, which is composed of a data structure representing shared resources and a resource management program composed of a group of processes that operate on the shared data structure . The manager is called by the process requesting and releasing critical resources .
Management defines a data structure and can be executed by concurrent processes （ In this data structure ） A set of operations , This set of operations can synchronize the process and change the data in the pipe .
This is also a concept that is not the same as the above concept , Hyper threading is Intel CPU A technique of design .
One CPU The physical core can only execute one thread at a time , And thread switching will consume tens of thousands of cycles , Not efficient enough . Hyper threading is about implementing the same CPU The technology that a physical core can execute almost two threads at the same time . This is where we are Intel CPU It is often seen in the task manager on the double Of CPU The reason for the core of Physics .
- 《 Deep understanding of computer systems 》 The second edition
- 《Windows Programming core 》 The fifth edition
- 《 Computer operating system 》 The third edition
- 《 Operating system concept 》 The seventh edition
- 《 Computer programming art 》 Volume one
- Coroutine Wikipedia
[ Take it with you ！Python 3.9 Official Chinese documents , Time limited collection ！] (http://dwz.date/dE6v)
[ Time limit ！ Quick collar ！14 Zhang HD Python Quick reference table , It is necessary to improve efficiency ！] (http://dwz.date/dE6w)
[GitHub Star sign 3W+,80 individual Python Case study , Take you easy to play Python Study ！] (http://dwz.date/dE64)
本文为[Love to learn]所创，转载请带上原文链接，感谢