In fact, the five states of the java thread need to be truly high-definition. You only need to understand the knowledge of the process in the computer operating system, and the principles are the same.
The system controls the process according to the status value in the PCB structure.
In a single CPU system, there is only one process in execution at any one time.
The five states of the process: initial state, execution state, waiting state, ready state, and termination state.
Execution state: The state when a process has obtained the necessary resources and is executing on the CPU.
The execution state process is in a waiting state because it is waiting for a certain event to occur.
When the time slice of the execution state process is used up or there is a higher priority process in the preemptive scheduling, it becomes the ready state.
Blocking primitive: An executing process expects a certain event to occur, but the conditions for occurrence are not yet met. The process calls the blocking primitive to block itself.
Process: Execution state process-interrupt processor and protect the CPU site-become blocked-insert waiting queue-transfer to the process scheduler to select the ready process to run.
Processes in the ready state can enter the execution state after being selected by the scheduler.
A process in the ready state has obtained all resources except the processor, and can be executed immediately once the processor is obtained
The process in the waiting state cannot get the processor immediately and enters the ready state after being awakened due to a waiting event.
There are two ways to wake up the process: system process wake up and event process wake up
The difference between the ready state and the waiting state: When assigned to the process processor, whether the process can be executed immediately, it can be the ready state, otherwise it is the waiting state.
Initial state: When the process is just created, it cannot be executed because other processes are occupying the processor, and it is in the initial state. (The process being created is initially in its initial state)
Termination state: After the execution of the process ends, it exits execution and is terminated, and it is in the termination state.
Processes have concurrent features such as independence and asynchrony.
Process control is to manage all processes in the system. Its functions include process creation, cancellation, blocking, wakeup, etc. (Digression, these functions are implemented by the operating system kernel)
Process control is achieved through primitives.
Primitive: A section of program composed of several machine instructions to complete characteristic functions. This section of program is indivisible during execution. The execution of primitives cannot be interrupted.
Processor three-level scheduling
A job undergoes three levels of scheduling from submission to completion.
1. Advanced scheduling (job scheduling)
According to certain principles, select one or more jobs from the backup state on the external storage, allocate memory, input and output and other necessary resources to them, and establish the corresponding process so that the job has the right to obtain a competing processor. The operation frequency of job scheduling is low, once every few minutes.
Create the corresponding process when the job is selected into the memory
There are two problems with job scheduling: the scheduler must decide how many jobs the operating system can accept, and the scheduler must decide which jobs to accept.
How many jobs are accepted into the memory each time for job scheduling depends on the degree of concurrency of multiple programs, that is, how many jobs are allowed to run in the memory at the same time.
Transferring those jobs from external storage to memory depends on the scheduling algorithm.
First-come, first-come algorithm: The job that enters the external memory first is transferred to the memory first.
Short job priority method: The job with the shortest execution time on the external memory is transferred to the memory first.
2. Intermediate scheduling
Transfer the processes with operating conditions in the external memory swap area to the memory, or swap the temporarily inoperable processes in the memory to the external memory.
3. Low-level scheduling (process scheduling)
Select a process from the ready queue and assign the processor to it.
The process scheduling operation frequency is high, once every tens of milliseconds.
The difference between job scheduling and process scheduling: the result of job scheduling is to create a process, and the result of process scheduling is that the process is executed.
Process scheduling (emphasis)
In order to achieve process scheduling, the process management module must record the execution and status characteristics of each process in the system in the PCB of each process, and organize the PCB of the process into a corresponding queue according to the status characteristics and resource requirements of each process.
Process scheduling method: When a process is executing on the processor, if a more important process needs to be processed (a higher priority process enters the ready queue), how should the processor be allocated at this time. Two ways:
Preemptive: A process is being executed on the processor. If a higher priority process enters the ready queue, the executing process is immediately suspended and the processor is assigned to the new process.
Non-preemptive: A process is executing on the processor. If a higher priority process enters the ready queue, the executing process is still allowed to continue execution until the process automatically releases the processor (the time slice is used up and enters blocking Done) before assigning the processor to the new process.
First come first serve (jobs, process scheduling)
Schedule according to the order of requests of jobs or processes.
Note that in process scheduling, once a process occupies the processor, it will continue to run until the process completes or becomes a waiting state (the processor is not time-sliced, and there is no transition from execution to ready). Non-preemptive.
Short job priority (job, process)
Choose the job or process that can be completed the fastest (starvation will occur)
Always monopolize the processor
Priority scheduling (jobs, processes)
Call the highest priority, always monopolize the processor
Subdivided into preemptive and non-preemptive
Time slice rotation method (process)
Each process cannot occupy the processor until it is completed, there is a transition from execution to ready
A section of program that does not allow multiple concurrent processes to execute across (this section of program must be executed at once without interruption, multi-threaded synchronization code block) is called the critical part.
Process synchronization and mutual exclusion
Indirect restriction (mutual exclusion): A certain process requires the use of a certain resource, and the resource is being used by another process, and this resource is not allowed to be used by two processes at the same time, then the process can only wait for the process that has occupied the resource to release Use it later.
Mutually exclusive requirements:
Idle let in: When no process enters the critical section, a process requesting to enter the critical section can be allowed to enter the critical section immediately.
If busy, wait: there are processes that have entered the critical section, and other processes trying to enter the critical section must wait.
Limited wait: For the process that wants to access the critical part, the critical part of the process should be guaranteed within a limited time.
Give power to wait: When a process cannot enter the critical part for some reason, the processor should be released to other processes.
Direct restriction (synchronization): If a process cannot receive the necessary information provided by another process, it cannot continue to run.
Use semaphores to manage public resources in critical sections.
Semaphore sem, sem>=0 represents the number of resource entities available for concurrent processes, and sem<0 represents the number of processes waiting to use the critical section.
P operation sem-1, V operation sem+1.
When a process is executing in the critical section (in the execution state), other processes execute the P primitive (the previous doubt here is that there is a process executing, and other processes will not execute because they do not occupy the processor. In fact, Considering this, when the process executes the critical section, it is not executed, and then the time slice is used up, and it turns to the ready state. Then other processes execute the P primitive in the processor and find that they cannot enter the critical section and block themselves. When the critical section can be executed , Select one from the same blocking queue to be awakened, ready to be executed), the process will enter the waiting queue and wait for other V primitive operations to release resources, and then enter the critical section. This is the P primitive before it ends.
P primitive operation:
2. If >=0 after sem-1, the P primitive returns to indicate that the process can continue to execute.
3. If <0 after sem-1, the process is blocked, enters the corresponding blocking queue, transfers to process scheduling, and selects a process in the ready queue to enter the execution state.
V primitive operation
2. The sum is greater than 0, the execution of the V primitive is stopped, the process returns to the source of the call, and the execution continues
3. Add <=0, select a process to wake up from the signal waiting queue (because a negative number represents the number of waiting processes, sem+1 means that there is one less waiting process), the V primitive stops execution, and then returns to the original The process continues.
4.conditions for deadlock
Deadlock handling method