当前位置:网站首页>Processes and threads
Processes and threads
2022-07-06 05:40:00 【Embedded Yuexiang Garden】
Processes and threads
1、 process
Process is one of the oldest and most important abstract concepts provided by the operating system , Even if it can be used CPU only one , But because it has ( Pseudo concurrency ) Ability to operate , It can put a CPU Simulate multiple virtual CPU.
1.1 What is a process
A process is the smallest unit of operating system resource allocation . A process has its own resources Pile up 、 Stack 、 Virtual space 、 File description Symbol and other information . Understand processes from a programming perspective , Think of it as ⼀ A class or a PCB Structure of process control block .
A process is an abstraction of the operating system for a running program , The process can be regarded as a running process of the program .
Surface concept : An execution instance of the program , The program being executed
Kernel view : Responsible for allocating system resources (CPU, Memory ) Entity of
In any multiprogramming ,CPU Always switch quickly from one process to another , Each process can run for tens or hundreds of milliseconds , This will give us the illusion that multiple programs are running at the same time , But strictly speaking, at a certain moment ,CPU Only one process can be processed , But for a while (1s) Inside CPU May have handled multiple programs , This creates the illusion of parallelism .
1.2 Process model
In the process model , All the running software on the computer , Organized into several Sequential process , abbreviation process . A process is an instance of an executing program , conceptually , Each process has its own virtual CPU.
for instance :
For example, we have a four way program , That is, these four have their own control processes , And each program can run independently .
because CPU It switches back and forth between multiple processes , Therefore, the operation speed of each process is uncertain , So when a process needs strict real-time requirements , That is, a specific program must run within a specified period of time , Then special measures must be taken to ensure that it can happen in a short time .
A process is similar to some kind of activity , He has procedures 、 Input 、 Output and status . A single processor can be shared by several processes , It uses some scheduling algorithm to decide when to stop a process , Turn to serving another process .
1.3 Why there should be a process
1.4 Process creation
The operating system needs a way to create processes . The creation of general operating system is mainly triggered by the following four ways :
- Initialization of the system
- The program in progress executes the system call to create the process
- The user created a new process
- Initialization of a batch job
The system will generally create several processes during initialization , stay Linux We can pass ps
Instruction to view the running process .
Except for processes opened during system startup , A running process often makes many system calls , To cooperate with their own work , When the work can be easily divided into several related but non interactive processes , Creating a new tutorial will be particularly effective .
When we are using Windows When you double-click an application icon to start a software, a process will be created , Users can open multiple windows , Every window is a process .
In addition to the above three ways to start the process , Processes can also submit batch jobs in the system , The system thinks it can be a running user process , He created a new process , And run the next job in the queue it enters .
1.5 Process exit
After the process is created , He started running , Complete its work . But the process is not eternal , The process will end sooner or later , Usually, the end of a process is caused by the following events :
- The normal exit ( voluntary )
- Error exit ( voluntary )
- Serious mistakes ( Involuntarily )
- Killed by other processes ( Involuntarily )
Most processes are terminated by completing their own work , The second reason for process termination is that a serious error occurred in the process , For example, the user enters the following command :
cc foo.c
If we want to compile foo.c, But the file does not exist , Then the compiler will exit by itself .
The third reason is the error caused by the process , It is usually caused by errors in the program , When the program error occurs , The process will receive a signal , Thus terminating the process , Instead of terminating when such an error occurs .
The fourth reason to terminate the process , Generally in UNIX and Windows Will not use .
1.6 State of process
function : At work , Execution procedure
Blocking : Because of an event , For example, interrupt events , At this point, the process will stop , No further code execution down . for example , A piece of code in the program requires the user to enter a letter , Then when the process executes here , Will stop and wait for the user to enter letters ( Get stuck in a jam ), Until the user hits a letter on the keyboard , At this time, the operating system will let the process continue to execute downward . Blocking time , The process will yield CPU, Because no one knows , How often do you type letters , If you type the letters in an hour , Do you want to let other processes wait for hours ? So the operating system will not waste this time , So will CPU Give way to other processes , Let them execute first .
be ready : At this point, the process has entered the ready queue , except CPU outside , All other required resources have been obtained . That is to say , Just need to get CPU, Then you can start running .
After the process is created and divided CPU All resources other than will enter the ready queue , At this time, it is in the ready state . When it gets through the scheduling of the operating system CPU after , It's running . If the program is executed within the time slice , Then the process will be directly revoked ; If the program is not completed within the time slice , Then when the time slice ends, it will directly enter the ready queue , Wait for next time to get CPU, Carry on ; If there is an interrupt event during operation , for instance IO event , Then the process will be blocked , Give up CPU, until IO The end of the incident , The process will re-enter the ready queue , wait for CPU The arrival of the .
Linux Process status under :
(1) Running state R: be ready / function , Only the running process can be scheduled by the operating system CPU function
(2) Interruptible sleep state S: The current block can be awakened by interruption
(3) Non interruptible sleep state D: The current block will not be awakened by interruption
(4) Stop state T: Stop running and do nothing
(5) Deadpan Z: The process has exited , But the resources have not been completely released
( The difference between sleep and stop is : Sleep is working , His job is to sleep , And stopping doesn't work , Nothing )
1.7 stay Linux Issue process related commands
ps -ef—— View process information
ps -aux—— View process information , But the information is more detailed than the above information
kill Process number —— Kill process
kill -9 Process number —— Force kill process
2、 Threads
Thread concept : An entity of a thread process , yes CPU Basic unit of dispatch and dispatch , It's a smaller, independent unit than a process , The thread itself basically does not have system resources .
2.1 State of thread
Threads have 5 Different states , Namely : newly build (New)、 be ready (Runable)、 function (Running)、 Blocking (Blocked) And death (Dead). The transformation diagram between them is as follows :
New state (New): A new thread object is created .
Ready state (Runnable): After the thread object is created , Other threads called the object's start() Method . The thread in this state is in the runnable thread pool , Become operational , Waiting to get CPU Right to use .
Running state (Running): The ready state is obtained by the thread CPU Right to use , Execute program code .
Blocked state (Blocked): Blocking is when the thread gives up for some reason CPU Right to use , Stop running temporarily . Until the thread is ready , To have a chance to go to run state . There are three types of obstruction :
1、 Waiting for a jam : The running thread executes wait() Method ,JVM This thread is put into the wait pool .(wait Locks held are released )
2、 Synchronous blocking : When a running thread acquires a synchronized lock for an object , If the synchronization lock is held by another thread , be JVM This thread is put into the lock pool .
3、 Other blockages : The running thread executes sleep() or join() Method , Or send out I/O request .
Death state (Dead): The thread executes or exits due to an exception run() Method , The thread ends its life cycle .
2.2 Why use thread pool ?
1、 Improve the efficiency of program execution :
If there are a lot of thread tasks with short-time tasks in the program , Because creating and destroying threads requires interaction with the underlying operating system , A lot of time is spent creating and destroying threads , So it is a waste of time , The system efficiency is very low ; After the end of each thread task in the thread pool , It's not going to die , Instead, go back to the thread pool and become idle again , Wait for the next object to use , Therefore, with the help of thread pool, the execution efficiency of the program can be improved .
2、 Number of control threads , Prevent the program from crashing :
If you create and start threads without restrictions, it is easy to cause program crash , For example, high concurrency 1000W Threads , You need to save 1000W Space for threads , This is very prone to memory overflow ; The number of threads in the thread pool is certain , It can effectively avoid memory overflow .
2.3 Multithreading creation
The first way :
- Create a
Thread
Subclasses of classes - stay Thread Class
Thread
Classrun
Method , Set up thread tasks ( What to do to start a thread task ?) - establish
Thread
Subclass object of class - call
Thread
Methods in classstart
Method , Start a new thread , performrun
Method
void start()
Start execution of this thread ;Java The virtual machine calls the thread's run Method . As a result, two threads run concurrently , Current thread (main Threads ) And another thread ( New thread created , To perform its run Method ).java The program belongs to preemptive scheduling , That thread has a high priority , That thread takes precedence , Same priority , Randomly choose an execution .
边栏推荐
- Knowledge points of circular structure
- Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
- Questions d'examen écrit classiques du pointeur
- C进阶-数据的存储(上)
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
- Note the various data set acquisition methods of jvxetable
- 网站进行服务器迁移前应做好哪些准备?
- Promotion hung up! The leader said it wasn't my poor skills
- B站刘二大人-Softmx分类器及MNIST实现-Lecture 9
猜你喜欢
28io stream, byte output stream writes multiple bytes
ArcGIS application foundation 4 thematic map making
How can large websites choose better virtual machine service providers?
数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
[cloud native] 3.1 kubernetes platform installation kubespher
Modbus protocol communication exception
Sequoiadb Lake warehouse integrated distributed database, June 2022 issue
Questions d'examen écrit classiques du pointeur
Check the useful photo lossless magnification software on Apple computer
[force buckle]43 String multiplication
随机推荐
P2802 回家
02. Develop data storage of blog project
Application Security Series 37: log injection
进程和线程
B站刘二大人-多元逻辑回归 Lecture 7
C Advanced - data storage (Part 1)
Fluent implements a loadingbutton with loading animation
After the project is released, index Html is cached
B站刘二大人-线性回归 Pytorch
JDBC calls the stored procedure with call and reports an error
Vulhub vulnerability recurrence 71_ Unomi
Algorithm -- climbing stairs (kotlin)
Sequoiadb Lake warehouse integrated distributed database, June 2022 issue
Closure, decorator
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
Promotion hung up! The leader said it wasn't my poor skills
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
Graduation design game mall
[machine learning notes] univariate linear regression principle, formula and code implementation
Vulhub vulnerability recurrence 68_ ThinkPHP