The process of five states of sickness and death-twin brothers Java thread

Source: Internet
Author: User

The five states of Java threads actually want to be really high definition, only need to understand the computer operating system process knowledge, the principle is the same.

The system controls the process based on the status values in the PCB structure.

In a single CPU System, there is only one process that is in the execution state at any one time.

Five states of the process: initial state, execution state, waiting state, ready state, terminating state.

Execution state: A process obtains the necessary resources, and the state is executed on the CPU.

The execution process is waiting for an event to occur.

The time slice of the execution process runs out or becomes ready when there are higher priority processes in the preemptive schedule.

Blocking primitives: An execution process expects an event to occur, but the condition is not yet available, and the process itself calls the blocking primitive to block itself.

Process: Execution process--interrupt processor and protect the CPU field --into a blocking state--insert wait queue--go to process Scheduler selected ready process run.

The process of the ready state is checked by the scheduler before it can enter the execution state.

The ready state process has acquired all the resources except the processor, which can be executed immediately once the processor is acquired.

A process waiting for a state cannot get to the ready state immediately after the wait event has been awakened.

There are two ways to wake up a process: System process wakeup and event wake-up process

The difference between a ready state and a waiting state: when assigned to the process processor, the process can execute immediately, which is the ready state, otherwise it is the waiting state.

Initial state: When a process has just been created, it is in its initial state because other processes are not executing because of the processor being occupied. (The process being created is initially in the initial state)

Termination status: After the execution of the process, the exit execution is terminated and is in the signaled state.

Processes have concurrency characteristics such as independence and asynchrony.

Process Control is the management of all processes in the system. Its functions include process creation, revocation, blocking, wakeup, etc. (Off-topic, these features are implemented by the operating system kernel)

Process control is achieved through the primitive language.

Primitive : A procedure consisting of a number of machine instructions that is used to perform features that are inseparable during execution. The execution of the primitive cannot be interrupted.

Processor Level Three scheduling

A job goes through a level three schedule from the start of the commit until it finishes.

1, advanced scheduling (Job scheduling)

Select one or more of the jobs in the fallback state from the external memory on a certain principle, assign them the necessary resources such as memory, output input and so on, and establish the corresponding process, so that the job has the right to obtain the competition processor. Job scheduling runs at a low frequency, once a few minutes.

Create a process when the job is selected in memory

Two problems with job scheduling : The Scheduler must decide how many jobs the operating system can accept, and the scheduler must decide to accept those jobs.

How many jobs are admitted to the memory each time the job is scheduled depends on the concurrency of the multiprogramming, that is, how many jobs are allowed to run concurrently in memory.

The transfer of those jobs from the external memory into memory depends on the scheduling algorithm.

First come first to the algorithm: the first job to enter external memory first into memory.

Short job Priority: the job with the shortest execution time on the external memory is first transferred into memory.

2, intermediate scheduling

The process that has a running condition in the external memory swap is transferred into memory, or a process that is temporarily unable to run in memory is swapped to external memory.

3, low-level scheduling (process scheduling)

Select a process from the ready queue and assign the processor to him.

Process scheduling runs at a high frequency of dozens of milliseconds.

The difference between job scheduling and process scheduling: The result of job scheduling is to create processes, and the result of process scheduling is that processes are executed.

Process scheduling (Focus)

In order to realize the process scheduling, the Process Management module must record the execution status and state characteristics of each process in the process, and organize the PCB of the process into the corresponding queue according to the status characteristics of each process and the resource requirement.

Process scheduling: When a process is executing on a processing machine, if a more important process needs to be processed (the higher priority process enters the ready queue), how the processor should be allocated at this time. Two different ways:

Preemptive: A process is executing on a processing machine, and if a higher-priority process enters the ready queue, it immediately suspends the process being executed and assigns the processor to the new process.

Non-preemptive: a process is executing on the machine, if there is a higher priority process into the ready queue, still let the executing process continue to execute, until the process automatically release processor (time slices run out, into blocking, complete), before the processor is assigned to the new process.

Scheduling algorithm

First come first service (job, process scheduling)

Dispatched in the order in which the job or process is requested.

Note that in process scheduling, once a process consumes the processor, he continues to run until the process completes or becomes a wait state (there is no time slice on the processor, there is no transition to ready). Non-preemptive type.

Short job first (job, process)

Select the job or process that can be completed most quickly (starvation occurs)

has been an exclusive 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 continue to account for the processor until it is complete, there is a transition to ready

A program that does not allow multiple concurrent processes to cross-execute (this program must be executed one time, not interrupted, multithreaded synchronous code blocks) is called the critical section.

Process synchronization and mutex

Two kinds of restrictive relations

Indirect restriction (mutual exclusion): a process that requires the use of a resource that is being used by another process and that does not allow two processes to be used at the same time can only wait for the resource-intensive process to be freed and then used.

Mutex Requirements:

Idle-forward: When no process enters the critical section, a process that requests access to the critical section can be allowed to enter the critical area immediately.

Busy waiting: The process has entered the critical section, other processes trying to enter the critical part must wait.

Limited wait: For the process to access the critical section, the critical part of the process should be guaranteed for a limited time.

Let's wait: When a process cannot enter a critical section for some reason, it should release the processor to other processes.

Direct restriction (synchronization): A process cannot continue without receiving the necessary information provided by another process.

P,v Primitive Language

Use semaphores to manage the public resources of the critical section.

The semaphore sem,sem>=0 represents the number of resource entities that can be used by concurrent processes,sem<0 Represents the number of processes that are waiting to use a critical section.

P Operation sem-1,v operation sem+1.

When a process is executing in a critical section (in the execution state), Other processes execute the P Primitive ( the previous confusion here is that there is a process executing and no other process is executing because the processor is not occupied.) In fact, in this case, the process executes the critical section, does not finish, then the time slice runs out, and then the other process executes the P primitive in the processor, found unable to enter the critical section, blocking itself, when the critical section can be executed, from the same blocking queue Select one to be awakened, ready to be executed), then the process will go into the wait queue waiting for other V Primitive Operations to release resources, enter the critical section, this is the P primitive to end.

P Primitive Operation:

1, Sem-1

2, if the sem-1 after >=0, then the P Primitive returned to indicate that the process can continue to execute.

3, if the sem-1 after the <0, then the process is blocked, into the corresponding blocking queue, transfer process scheduling, select the ready queue a process into the execution state.

V Primitive Operation

1, sem+1

2, the sum is greater than 0,theV Primitive stops executing, the process returns the source of the call, and continues execution

3, add <=0, Select a process wake-up from the signal waiting queue (because the negative number represents a waitingprocess,sem+1 , it means that the wait process is one less),V The primitive stops executing, and then returns to the original process to continue execution.

Four conditions of deadlock

Handling Deadlock Methods

The process of five states of sickness and death-twin brothers Java thread

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.