当前位置:网站首页>Process, time slice, concurrency and parallelism

Process, time slice, concurrency and parallelism

2022-06-10 04:26:00 little-peter

3.1 Preliminary understanding of the process

  • For the operating system , A task is a process (Process).
  • Textbook concept : An execution instance of a program , The program being executed, etc .
  • Kernel view : Responsible for allocating system resources (CPU Time , Memory ) Entity of .
  • How to understand the process ?

Such as : Zhang San goes to the bank to transfer money , Use the bank's resources ( Reception , Transfer personnel , Bank computer , If necessary, the security guard can also follow :) )
Complete the transfer for Zhang San , And inside the bank , The execution of this whole process , We are collectively referred to as handling business .
analogy , A program is a program on the hard disk at the beginning , Load runtime , To accomplish certain tasks ( If Xunlei finishes downloading
Mission ), And to complete the task , You need the operating system to provide enough resources for this task ( Such as memory and CPU resources ), And this whole
The execution of a set of tasks , We can call it a process


therefore : The process is responsible for allocating system resources (CPU Time , Memory ) Entity of , It has dynamic characteristics , For example, Zhang San is handling the transfer business
Service period , In this way, the task always exists and the state changes .
You can transfer money to the bank... Not immediately , You need to draw the number first , Wait in the hall , Because there may be many people waiting in the hall
stay , So there is the process waiting queue . And the number drawn by Zhang San , It can be called a process ID
below , We try to view the system process
Write a java process

public class Test {
public static void main(String[] args) throws Throwable {
Thread.sleep(99999999);
     }
}
 

stay cmd Compile and run :

javac Test.java
java Test

After running, it will start java Program ( process ), You can view the process in the task manager

there PID The code representing this process , And our legendary java virtual machine , In essence, it is just a process in a system . For details, see the previous sample diagram


3.2 Time slice

Modern operating systems like Mac OS X,UNIX,Linux,Windows etc. , All support. “ multitasking ” Operating system of . The so-called multiple posts
service , The operating system can run multiple tasks at the same time .
operating system ( Such as Windows、Linux) Task scheduling is based on Time slice revolves Preemptive scheduling , That is to say, a task
After a short period of execution, force a pause to perform the next task , Each task is executed in turn .
A short period of time during which a task is performed is called Time slice , The status when the task is executing is called Running state , Forced pause after task execution for a period of time
Go to the next task , The suspended task is in Ready state Wait for the arrival of the next time slice that belongs to it .
So that every task can be carried out , because CPU Very efficient implementation of , Time slice is very short , Quickly switch between tasks
in , The feeling is that there are multiple tasks “ At the same time ”, This is what we call Concurrent .
How to understand ?

Suppose Zhang San is going to handle the transfer business , But when he gave all the information to the staff , The staff told him , You can't handle it now ,
Because now he needs to fill out an application form , here , The staff kept his data , Ask him to fill in the information , Complete
After that , Come back and continue to handle , meanwhile , Zhangsan has gone to fill in the form , And the staff continue to provide services to others . This process is called a process
Switch .
Three forms have been filled out , Go back to the counter , The staff took out his previous information , Continue to handle business for Zhangsan , This is called up and down the process
Protection and restoration of cultural relics ( Think , Why do you do this ? Because the process is running in CPU Upper ,CPU There are registers , Save the process
Various temporary data for operation , In order to achieve the purpose of switching and easy recovery , There will be CPU How to save and restore registers , In the final analysis
The end result is to continue running at the last position )
later , The bank has issued regulations , Each person should not spend more time at the counter than 10 minute ( To prevent others from waiting for a long time ),
So in order to better serve all personnel , Bank staff applied the above idea of switching and recovery to various businesses , So long term
Look at , Even if there is only one staff member , It can also serve multiple customers at the same time , This mechanism is called time slice based process rotation manager
system , While the above 10 minute , It is the time slice of bank rotation , As long as the time comes , The customer waited for the wine , Let other users handle
Business
And all the above rotation and management work , In the operating system , Unification is done by a kernel module called the scheduler
And for everyone , In a period of time , Maybe everyone's business can be promoted ( Even if it's not done ), But not for a long time
Wait between , This mechanism is called concurrency
If the bank is rich and powerful , There are more staff than customers , That's easy , One person, one staff , All the tasks are really at the same time
The reason is , This mechanism is called parallelism .
below , Let's look at the detailed concepts of concurrency and parallelism


3.3 Concurrency and parallelism


Now? , Multicore CPU It's very popular , Because the number of tasks is far more than CPU The number of cores , therefore , The operating system will automatically
Many tasks are scheduled to execute on each core in turn . about

  • Concurrent : Multiple processes in one CPU Time slice rotation is adopted under , In a period of time , Let multiple processes move forward , Call it concurrency .
  • parallel : Multiple processes in multiple CPU Lower division , Run at the same time , This is called parallelism .

In the computer operating system, parallelism and concurrency are clearly distinguished , Mainly from the micro point of view , Specifically, it refers to the parallelism of processes
( In the case of multiprocessors , Multiple processes running at the same time ) And concurrency ( In the case of a single processor , Multiple processes run at the same time interval
Yes ).
Concurrency and parallelism are similar to pipelining in a factory , To expand production ,1 Is to consider building multiple factories , This is parallel ,2 Is to consider every worker
New assembly lines in the plant , This is similar to concurrency .


3.4 Kernel state and user state


General operating system ( Such as Windows、Linux) Level the execution authority : User mode and kernel mode .
As shown in the figure above , The operating system kernel is the underlying software that directly controls the hardware devices , The highest authority , Called kernel state , Or core
state .
The user program has the lowest permissions , It is called user mode .
How to understand ?
Just like the example above , Zhangsan goes to fill in the form , Write your own name , Telephone , Mailbox and so on , Do your own thing , This is called user mode
And Zhang San passed through the staff of the window , Give your needs to the staff , Since then , Zhangsan is waiting , The bank staff are busy , Yes, Zhang San
Speaking of , It's called getting into the kernel . So what does kernel state mean ? It is the status of the staff when they help you with your business
 


3.5 Context in process


The context is simply an environment , When the process rotates and switches in the time slice , Because each process runs in a different environment , It involves transformation
Context switching before and after

  • When a process is executing ,CPU The values in all registers of 、 The state of the process and the contents on the stack .
  • When switching, you need to save all the states of the current process , Save the process context of the current process , So that when the process is executed again , Able to restore the state when switching , Carry on

3.5 Process status

 

be ready : The process is in a runnable state , It's just CPU The time slice has not been rotated to the process , The process is ready .
function : The process is in a runnable state , And CPU The time slice wheel goes to the process , The process is executing code , Then the process is running .
Blocking : The process does not have the conditions to run , Waiting for an event to complete




 

原网站

版权声明
本文为[little-peter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091240104096.html