Processor scheduling and deadlock
hierarchy of processor scheduling
Advanced Scheduling
Advanced scheduling is also called job scheduling or long-range scheduling, its main function is based on an algorithm, the external memory on the backup queue of those jobs into memory, that is, its scheduling object is the job.
1. Work and work steps
Job: A broader concept than the program, not only contains the usual procedures and data, but also should have a job description, the system according to the instructions to control the operation of the program. In a batch system, the memory is transferred from external memory to the base unit of the job.
Job step: During the job run, each job must go through a number of relatively independent and interrelated sequential processing steps in order to get the results, we call each process step as a job step, there is a correlation between the work steps, often is the last job step output as the next step of the input. A typical job can be divided into three steps: ① compile job step, ② link Assembly job step, ③ run job step.
Job flow: After several jobs enter the system, they are stored sequentially on the external memory, which forms the input job flow, which is processed under OS control, and the job flow is formed.
2. Operation control block JCB
In order to manage and dispatch the job, a job control block is set up for each job in the multi-channel batch system, which saves all the information that the system needs to manage and dispatch the job. Usually should include: job identification, user name, user account, job type, job status, scheduling information, resource requirements, enter the system time, start processing time, job completion time, job exit time, resource usage and so on. JCB a similar process PCB.
3. Job scheduling
The main function of job scheduling is to examine whether the system can meet the resource requirements of the user's job according to the information in the job control block, and according to certain algorithms, select some jobs from the external memory queue to transfer into the memory and create the process for them and allocate the necessary resources. The newly created process is then inserted into the ready queue for execution.
Each time a job is scheduled, the following two decisions are required: how many jobs are accepted (depending on the number of procedural degrees), and what jobs are accepted (depending on the scheduling algorithm used).
Low-level scheduling
A low-level dispatch is also called a process dispatch or short-range dispatch, and the object it calls is a process. It is the most basic kind of scheduling.
The main functions of the low-level scheduling are as follows: ① The field information of the storage processor; ② selects the process according to an algorithm; ③ assigns the processor to the process.
Three basic mechanisms in process scheduling:
The ① queue device. In order to improve the efficiency of process scheduling, all the ready processes in the system should be arranged in one or more queues in advance so that the scheduler can find it as quickly as possible.
② Dispatcher. The dispatcher takes the process selected by the process, pulls the process out of the ready queue, and then switches context, assigning the processor to it.
③ the context switching mechanism. When the processor is switched, two context switching operations occur. In the first context switch, the operating system saves the context of the current process, and loads the context of the dispatcher so that the dispatcher runs, and in the second context switch, the dispatcher is moved out and the CPU field information for the new process is loaded into the respective registers of the processor.
3. Process scheduling mode
1) Non-preemptive mode (non-preemptive mode)
In the case of non-preemptive scheduling, the factors that can cause process scheduling can be attributed to the following: ① the executing process is complete, or the execution of an event can not continue execution, ② executing process due to the request for I/O, suspended execution, ③ during the process communication or synchronization process to perform a primitive operation, such as P operation ( Wait operations), block primitives, wakeup primitives, and so on. This scheduling method has the advantage of simple implementation, low system overhead, and is suitable for most batch processing system environments. But it is difficult to meet the requirements of urgent tasks-immediate implementation, which can result in unpredictable consequences. Obviously, in the demanding real-time system, it is inappropriate to use this scheduling method.
2) preemption mode (preemptive mode)
This scheduling method allows the scheduler to suspend an executing process based on some principle and reassign the processor assigned to the process to another process. The advantage is that a long process can be prevented from taking up the processor for a long time, providing a fairer service for most processes, especially for real-time tasks that require more stringent response times. Principle of preemption: priority principle (higher priority for some important and urgent operations); short job or process precedence principle (when the newly arrived job is significantly shorter than the job being performed, the execution of the current long job is paused, and the processor is assigned to the newly arrived job) Time slice principle (each process runs on a time slice, and when a time slice is exhausted, it stops execution of the process and re-dispatches it).
Intermediate scheduling
Intermediate dispatching is also called medium-range dispatch (Medium-term scheduling). The primary purpose of introducing intermediate scheduling is to improve memory utilization and system throughput. For this reason, those processes that are temporarily unable to run are no longer consuming valuable memory resources, and they are moved to external memory to wait for the process state to be called ready for a foreign state or a pending state. When these processes are heavy and running, and the memory is slightly idle, the intermediate scheduling determines which of the external memory are ready to run, re-enter the memory, and modify its status to ready to wait for process scheduling on the ready queue.
scheduling queue model and scheduling criteria
Scheduling Queue Model
1. Scheduling queue model with process scheduling only
2. Scheduling queue model with advanced and low-level scheduling
3. At the same time, there are three levels of scheduling scheduling queue model selection scheduling and scheduling algorithm of several criteria 1. User-oriented guidelines (1) Short turnaround time (2) Fast response time (3) Guarantee of deadline (4) Priority criteria. 2. System-oriented guidelines: High system throughput, good utilization of processors, and balanced use of various resources.Scheduling AlgorithmFirst come first service scheduling algorithm (FCFS) short job (process) Priority scheduling algorithm (SJ (P) f) SJ (P) F scheduling algorithm can effectively reduce the average waiting time of the job, improve the system throughput. But there are disadvantages: ① to long work; ② the algorithm does not take into account the urgency of the operation, so it cannot guarantee that the urgent work will be processed in time; ③ the user may intentionally or unintentionally shorten the estimated time of their job, so that the algorithm does not necessarily really do short-term job first. High priority priority scheduling algorithm in order to take care of the urgent operation, so that it gets into the system after the preferential treatment, the introduction of the highest priority priority scheduling (FPF) algorithm. 1. Priority scheduling algorithm type ① non-preemptive priority algorithm: Once the system has assigned the processor to the highest priority process in the ready queue, the process continues until it is completed, or the system can re-allocate the processor to another process with the highest priority because of an event that causes the process to abandon the processor. This scheduling algorithm is mainly used in batch processing systems, and can also be applied to some real-time systems where real-time requirements are not stringent. ② preemptive priority scheduling algorithm: The system also assigns the processor to the process of highest priority and makes it execute. During its execution, however, as soon as another process with its higher priority arises, the process scheduler immediately stops the execution of the current process (the one that has the highest priority) and re-assigns the processor to the new highest priority process. Therefore, when this scheduling algorithm is used, the priority pi is compared with the priority PJ of the executing process J whenever a new ready process I is present in the system. If PI≤PJ, the original process PJ will continue to execute, but if it is PI>PJ, then immediately stop the execution of PJ, do a process switch, so that I process into execution. Obviously, this preemptive priority scheduling algorithm can better meet the requirements of urgent operation, so it is often used in the demanding real-time systems, as well as in batch processing and timeshare systems with higher performance requirements. 2. The type of priority ① static priority: is determined when the process is created and remains constant throughout the process's run. The priority of the process is determined by the type of process, the requirements of the process for resources, and the user requirements. ② Dynamic Priority: The priority given when creating a process can be changed with the progress of the process or with the increase of its wait time, in order to achieve better scheduling performance. 3. The change rule of priority of high response ratio priority scheduling algorithm can be described as: the sum of waiting time and service time is the response time of the system to the job, so the priority is equal to the response than the RP. Accordingly, can be expressed as: from the above can be seen: ① if the waiting time of the job is the same, the shorter the time required to service, the higher the priority, so the algorithm is conducive to short-term job.
② when the time required for the service is the same, the priority of the job depends on its waiting time, the longer the waiting time, the higher the priority, so it realizes the first to serve first.
③ for long jobs, the priority of the job can increase with the wait time, and when its wait time is long enough, its priority can rise to high, and thus the processor can be obtained. Time-slice-based rotation scheduling algorithm in time-sharing system, in order to be able to respond to user requests in a timely manner, we must adopt the temporal slice-based rotary process scheduling algorithm. 1. Time Slice Rotation method
In the early time-slice rotation method, the system puts all the ready processes on a first-come-first-served basis, queues the CPU for each schedule, and makes it perform a time slice. The size of the time slice is from several ms to hundreds of Ms. When the elapsed time slice runs out, the clock interrupt request is made by a timer, which signals the scheduler to stop the execution of the process and sends it to the end of the ready queue, and then assigns the processor to the new team first process in the ready queue, while also allowing it to execute a time slice. This ensures that all processes in the ready queue are able to obtain the processor execution time for a time slice within a given timeframe. 2. Multilevel feedback Queue scheduling algorithm (1) You should set up multiple ready queues and assign different priorities to each queue. The first queue has the highest priority, the second queue is followed, and the priority of the remaining queues is lowered one by one. The algorithm gives each queue the size of the process execution time slices, and in the higher priority queue, the execution time slices for each process are smaller. For example, the time slice of the second queue is one times longer than the time slice of the first queue ..., the time slice of the i+1 queue is one times longer than the time slice of the I queue. (2) When a new process enters memory, it is first placed at the end of the first queue and queued for dispatch by the FCFS principle. When it is time for the process to execute, it can prepare the evacuation system if it can be completed on the chip, and if it is not completed at the end of a time slice, the scheduler moves the process to the end of the second queue, and then similarly waits for dispatch execution by the FCFS principle, if it is still not completed after running a time slice in the second queue Then put it in the third queue, ..., and so on, when a long job (process) from the first queue down to the nth queue, in the nth queue to take a time-slice rotation of the way to run. (3) The scheduler dispatches the processes in the second queue only when the first queue is idle, and the processes in queue I are scheduled to run only if the 1~ (i-1) queue is empty. If the processor is servicing a process in queue I, and a new process enters a higher priority queue (1~ (i-1)), then the new process will preempt the processor that is running the process, that is, the scheduler puts the running process back to the end of the queue. Assign the processor to the new high priority process. 3. Performance of multilevel feedback queue scheduling algorithm: terminal type job user, short batch processing job user, long batch processing job user.real-time schedulingThe basic conditions for implementing real-time scheduling 1. Provide the necessary information ① ready time
② start and finish deadlines ③ processing time
④ Resource requirements ⑤ Priority 2. Strong system processing ability in real-time systems, often have multiple real-time tasks. If the processing capacity of the processor is not strong enough, it may be because the processor is too busy to make certain real-time tasks can not be processed in a timely manner, resulting in unpredictable consequences. Assuming that the system has m periodic hard real-time tasks, their processing time can be expressed as CI, cycle time is expressed as PI, then in the case of a single processor, the following restrictions must be met:
The solution is to improve the processing capacity of the system by two ways: one is still the use of a single processor system, but to enhance its processing capacity to significantly reduce the processing time for each task; the second is to use multiprocessor system. Assuming that the number of processors in the system is N, the restriction should be changed to <=n. 3. Preemptive scheduling mechanism when a higher priority task arrives, allows the current task to be suspended temporarily, and the high priority task is immediately put into operation, so that the hard real-time task to meet the deadline requirements. But this scheduling mechanism is more complicated.
For some small real-time systems, if the start deadline of the task can be predicted, the scheduling of the real-time task can adopt the non-preemption scheduling mechanism to simplify the scheduler and the overhead of the task scheduling. However, in the design of this scheduling mechanism, should make all the real-time tasks are relatively small, and after the execution of critical procedures and critical areas, can promptly block themselves, in order to release the processor, for the scheduler to schedule the start of the deadline to arrive at the task. 4. With fast switching mechanism The mechanism should have the following two capabilities:
(1) The ability to respond to external interrupts quickly. In order to make the system timely response when the urgent external event request is interrupted, the system is required to have a fast hardware interrupt mechanism, and the time interval of the forbidden interruption should be kept as short as possible to avoid delaying the time (other urgent tasks).
(2) Fast task dispatch ability. After you complete the task schedule, you should switch tasks. In order to improve the speed of task switching, it is necessary to make each running functional unit in the system as small as appropriate to reduce the time overhead of task switching. The classification non-preemptive scheduling algorithm of real-time scheduling algorithm: Non-preemptive rotation scheduling algorithm and non-preemptive priority scheduling algorithm. Preemptive scheduling algorithm: Preemptive priority scheduling algorithm based on clock interrupt; Preemptive priority scheduling algorithm. Commonly used centralized real-time scheduling algorithm: 1. First cut-off time priority is EDF (earliest Deadline first) algorithm 2. The minimum relaxation priority is the reason why the deadlock is caused by the algorithm LLF (Least laxity firstly) and the reason why the deadlock occurs: competitive resources , the sequence of progress between processes is illegal. The necessary conditions for deadlock: mutual exclusion condition, request and hold condition, non-deprivation condition, the basic method of processing deadlock by loop wait condition: Prevent deadlock, avoid deadlock, detect deadlock, unlock deadlock. Methods to prevent deadlocks: abandon the "request and hold" condition, abandon the "no deprivation" condition, abandon the "loop wait" condition system security state: The so-called security state, refers to the system can be in a certain process sequence (P1, P2, ..., Pn) (called 〈p1, P2, ..., pn〉 sequence for safe sequence), To allocate their required resources to each process pi until each process is satisfied with the maximum resource requirements, so that each process can be successfully completed. If the system cannot find such a security sequence, the system is said to be in an unsafe state. Use the banker algorithm to avoid deadlock 1. The data structure in the Banker's algorithm (1) can take advantage of the resource vector available. This is an array of M elements, each of which represents a class of available resources, whose initial value is the number of all available resources in the system that are configured, and whose values change dynamically with the allocation and recycling of that class of resources. If Available[j]=k, it represents the existing RJ class resource K in the system. (2) Max requirement matrix Max. This is a nxm matrix that defines the maximum demand for M-class resources for each process in n processes in a system. If max[i,j]=k, it indicates that process I requires the maximum number of RJ class resources to be K.
(3) Distribution matrix allocation. This is also a nxm matrix that defines the number of resources that each class of resources in the system currently has allocated to each process. If allocation[i,j]=k, indicates that process I currently has the number of RJ class resources as K.
(4) Demand matrix need. This is also a nxm matrix that represents the number of resources required for each process. If need[i,j]=k, it means that process I also requires the RJ class resource K, to complete its task. The following relationships exist between the above three matrices: Need[i, J]=max[i, J]-Allocation[i, J] 2. Banker algorithm
Set Requesti is the request vector of the process pi, if requesti[j]=k, indicates that the process pi needs K RJ type resources. When PI issues a resource request, the system checks as follows:
(1) If REQUESTI[J]≤NEED[I,J], turn to step 2; otherwise it is considered to be an error because it requires more resources than it has declared maximum value.
(2) if REQUESTI[J]≤AVAILABLE[J], then turn to step (3); otherwise, there is not enough resources, PI must wait. (3) The system is tempted to allocate resources to the process pi and modify the values in the following data structures:
Available[j]:=available[j]-requesti[j];
Allocation[i,j]:=allocation[i,j]+requesti[j];
Need[i,j]:=need[i,j]-requesti[j];
(4) System execution security algorithm, check the resource allocation, the system is in a safe state. If security, the resources are formally assigned to the process pi to complete the allocation; otherwise, the temptation allocation will be invalidated, restore the original resource allocation state, let the process pi wait. 3. Security algorithm (1) set two vectors: ① working vector work: it means that the system can provide the number of resources required for the process to continue running, it contains m elements, and work:=available when the security algorithm is executed; ②finish: It indicates whether the system has enough resources to be allocated to the process to make it run complete. First do finish[i]:=false; When sufficient resources are allocated to the process, the finish[i]:=true is then made. (2) To find a process from the process collection that satisfies the following conditions:
①finish[i]=false; ②NEED[I,J]≤WORK[J]; If found, perform step (3), otherwise, perform step (4).
(3) When the process Pi obtains the resources, can execute smoothly, until completes, and releases the resources allocated to it, therefore should execute:
Work[j]:=work[i]+allocation[i,j];
Finish[i]:=true;
Go to step 2; (4) If the finish[i]=true of all processes are satisfied, the system is in a secure state; otherwise, the system is in an unsafe state. Dead Lock: S is a sufficient condition for deadlocks when and only if the resource allocation diagram of the S state is not fully simplified. Release of deadlocks: deprivation of resources;