Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1 Subject : OPS - 12178 Chapter 4 Process Scheduling Scheduling 4.1 Scheduling – Objectives, concept, criteria, CPU and I/O burst cycle. 4.2 Types of Scheduling- Pre-emptive, Non pre-emptive. 4.3 Scheduling Algorithms. First come first served (FCFS), Shortest job first (SJF), Round Robin (RR), Priority. 4.4 Other Scheduling- Multilevel, Multiprocessor, real-time. 4.5 Deadlock - System model, principle necessary conditions, mutual exclusion, critical region. 4.6 Deadlock handling- Prevention, avoidance algorithm-Banker’s algorithm, Safety algorithm 4.1 Scheduling – Objectives, concept, criteria, CPU and I/O burst cycle. Objectives: - CPU scheduling is the basis of multiprogrammed operating systems. By switching the CPU among processes, the operating system can make the computer more productive. Basic Concepts: - In a single-processor system, only one process can run at a time; any others must wait until the CPU is free and can be rescheduled. The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization. The idea is relatively simple. A process is executed until it must wait, typically for the completion of some I/O request. In a simple computer system, the CPU then just sits idle. All this waiting time is wasted; no useful work is accomplished. With multiprogramming, we try to use this time productively. Several processes are kept in memory at one time. When one process has to wait, the operating system takes the CPU away from that process and gives the CPU to another process. This pattern continues. Every time one process has to wait, another process can take over use of the CPU. Scheduling of this kind is a fundamental operating-system function. Almost all computer resources are scheduled before use. The CPU is, of course, one of the primary computer resources. Thus, its scheduling is central to operating-system design. Scheduling Criteria Different CPU scheduling algorithms have different properties, and the choice of a particular algorithm may favor one class of processes over another. In choosing which algorithm to use in a particular situation, we must consider the properties of the various algorithms. • CPU utilization. We want to keep the CPU as busy as possible. CPU utilization can range from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly loaded system) to 90 percent (for a heavily used system). • Throughput. One measure of work is the number of processes that are completed per time unit, called throughput. For long processes, this rate may be one process per hour; for short transactions, it may be 10 processes per second. • Turnaround time. Important criterion is how long it takes to execute that process. The interval from the time of submission of a process to the time of completion is the turnaround time. Turnaround time is the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O. • Waiting time. The CPU scheduling algorithm affects the amount of time that a process spends waiting in the ready queue. Waiting time is the sum of the periods spent waiting in the ready queue. • Response time. The time from the submission of a request until the first response is produced called response time, is the time it takes to start responding. CPU and I/O burst cycle: Process execution consists of a cycle of CPU execution and I/O wait. Processes alternate between these two states. 2 Subject : OPS - 12178 Chapter 4 Process Scheduling Process execution begins with a CPU burst. That is followed by an I/O burst, which is followed by another CPU burst, then another I/O burst, and so on. The final CPU burst ends with a system request to terminate execution. The durations of CPU bursts vary greatly from process to process and from computer to computer, they tend to have a frequency curve characterized as exponential or hyper exponential, with a large number of short CPU bursts and a small number of long CPU bursts. An I/O-bound program typically has many short CPU bursts. A CPU-bound program might have a few long CPU bursts. This distribution can be important in the selection of an appropriate CPU-scheduling algorithm. 4.2 Types of Scheduling- Pre-emptive, Non pre-emptive. CPU-scheduling decisions may take place under the following four circumstances: 1. When a process switches from the running state to the waiting state (for example, as the result of an I/O request or an invocation of wait for the termination of one of the child processes) 2. When a process switches from the running state to the ready state (example, when an interrupt occurs) 3. When a process switches from the waiting state to the ready state (for example, at completion of I/O) 4. When a process terminates. For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in the ready queue) must be selected for execution. There is a choice, however, for situations 2 and 3. Non preemptive Scheduling: Scheduling takes place only under circumstances 1 and 4. Also known as cooperative scheduling. Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. 3 Subject : OPS - 12178 Chapter 4 Process Scheduling This scheduling method was used by Microsoft Windows 3.x. Used on certain hardware platforms, because it does not require the special. Preemptive Scheduling: Scheduling takes place only under circumstances 2 and 3. Windows 95 introduced preemptive scheduling, and all subsequent versions of Windows operating systems have used preemptive scheduling. Require the special hardware. incurs a cost associated with access to shared data affects the design of the operating-system kernel Sr. No. Non preemptive Scheduling:- Preemptive Scheduling:- 1 Scheduling takes place process switches from the running state to the ready state and waiting state to the ready state. Running process can be preempted to run another process. 5. Scheduling takes place when process switches from the running state to the waiting state and process terminate. Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. Does not require need specific platform. Does not incurs a cost associated with access to shared data. used by Microsoft Windows 3.x. 6. Simple to implement 2 3 4 Requires specific platform. Incurs a cost associated with access to shared data. Used by Windows 95 and thereafter versions. Difficult to implement 4.3 Scheduling Algorithms. First come first served (FCFS), Shortest job first (SJF), Round Robin (RR), Priority. First-Come First-Served Scheduling (FCFS) The process that requests the CPU first is allocated the CPU first. FCFS is implemented with a FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. The code for FCFS scheduling is simple to write and understand. The average waiting time under the FCFS policy, however, is often quite long. The average waiting time under an FCFS policy is generally not minimal and may vary substantially if the process's CPU burst times vary greatly. The FCFS scheduling algorithm is non-preemptive. The FCFS algorithm is not suitable for time-sharing systems, where it is important that each user get a share of the CPU at regular intervals. E.g. Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds: 4 Subject : OPS - 12178 Chapter 4 Process Scheduling If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the result shown in the following Gantt chart: The waiting time of processes is P1 0 ms P2 24 ms P3 27 ms The average waiting time is (0+ 24 + 27)/3 = 17 milliseconds. If the processes arrive in the order P2, P3, P1, however, the results will be as shown in the following Gantt chart: The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds. This reduction is substantial. Shortest job first (SJF) Scheduling Algorithm associates with each process the length of the process's next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie. Scheduling depends on the length of the next CPU burst of a process. Gives the minimum average waiting time for a given set of processes. The real difficulty with the SJF algorithm is in knowing the length of the next CPU burst request. SJF scheduling is used frequently in long-term scheduling. It cannot be implemented at the level of short-term CPU scheduling. There is no way to know the length of the next CPU burst. The SJF algorithm can be either preemptive or non-preemptive. As an example of SJF scheduling, consider the following set of processes, with the length of the CPU burst given in milliseconds: Using SJF scheduling, we would schedule these processes according to the following Gantt chart: 5 Subject : OPS - 12178 Chapter 4 Process Scheduling The waiting time is 3 milliseconds for process P1, 16 milliseconds for process P2, 9 milliseconds for process P3, and 0 milliseconds for process P4. Thus, the average waiting time is (3 + 16 + 9 + 0)/4 = 7 milliseconds. The choice arises when a new process arrives at the ready queue while a previous process is still executing. The next CPU burst of the newly arrived process may be shorter than what is left of the currently executing process. A preemptive SJF algorithm will preempt the currently executing process, whereas a nonpreemptive SJF algorithm will allow the currently running process to finish its CPU burst. Preemptive SJF algorithm As an example, consider the following four processes, with the length of the CPU burst given in milliseconds: If the processes arrive at the ready queue at the times shown and need the indicated burst times, then the resulting preemptive SJF schedule is as depicted in the following Gantt chart: Process P1 execution is started at time 0, since it is the only process in the queue. Process P2 arrives at time 1. The remaining time for process P1 (7 milliseconds) is larger than the time required by process P2 (4 milliseconds), so process P1 is preempted, and process P2 is scheduled. The waiting time of processes is P1 0 + (10 - 1) = 9ms P2 (1 - 1) = 0 ms P3 17 - 2 = 15 ms P4 5–3 = 2 ms The average waiting time for this example is ((10 - 1) + (1 - 1) + (17 - 2) + (5 - 3))/4 = 26/4 = 6.5 milliseconds. Non preemptive SJF scheduling would result in an average waiting time of 7.75 milliseconds. Priority Scheduling A priority is associated with each process, and the CPU is allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order. Priorities are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to 4,095. Some systems use low numbers to represent low priority; others use low numbers for high priority. In this text, we assume that low numbers represent high priority. Priorities can be defined either internally or externally. 6 Subject : OPS - 12178 Chapter 4 Process Scheduling Internally defined priorities use measurable quantities to compute the priority of a process. For example, time limits, memory requirements, the number of open files, and the ratio of average I/O burst to average CPU burst have been used in computing priorities. External priorities are set by criteria outside the operating system, such as the importance of the process, the type and amount of funds being paid for computer use, the department sponsoring the work, and other, often political, factors. Priority scheduling can be either preemptive or non-preemptive. A preemptive priority scheduling algorithm will preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently running process. A non-preemptive priority scheduling algorithm will simply put the new process at the head of the ready queue. A major problem is indefinite blocking, or starvation. A low priority process by wait indefinitely for the CPU. A solution is aging. Increase the priority of process gradually (after some predefined time) that waits in the system for a long time. An example: the following set of processes are assumed to have arrived at time 0, in the order P1, P2, • • -, P5, with the length of the CPU burst given in milliseconds: Using priority scheduling, we would schedule these processes according to the following Gantt chart: The waiting time of processes is P1 6 = 6 ms P2 0 = 0 ms P3 16 = 16 ms P4 18 = 18 ms P5 1 = 1ms The average waiting time for this example is (6 + 0 + 16 + 18 + 1)/5 = 41/5 = 8.2 milliseconds. Round-Robin Scheduling The round-robin (RR) scheduling algorithm is designed especially for time sharing systems. A small unit of time, called a time quantum or time slice, is defined. A time quantum is generally from 10 to 100 milliseconds. The ready queue is treated as a circular queue. The CPU is allocated to each process for a 1 time slice. The ready queue is kept as a FIFO queue of processes. New processes are added to the tail of the ready queue. 7 Subject : OPS - 12178 Chapter 4 Process Scheduling The CPU scheduler picks the first process from the ready queue, and when the time slice gets expired the process is put at the tail of ready queue. If the process has CPU burst less than 1 time slice then the process itself will release the CPU voluntarily. The average waiting time under the RR policy is often long. No process is allocated the CPU for more than 1 time quantum in a row (unless it is the only runnable process). If a process's CPU burst exceeds 1 time quantum, that process is preempted and is put back in the ready queue. The RR scheduling algorithm is thus preemptive. Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds: If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds. Since it requires another 20 milliseconds, it is preempted after the first time quantum, and the CPU is given to the next process in the queue, process P2. Since process P1 does not need 4 milliseconds, it quits before its time quantum expires. The CPU is then given to the next process, process P3. Once each process has received 1 time quantum, the CPU is returned to process P1for an additional time quantum. The resulting Gantt chart for RR schedule is The waiting time of processes is P1 0+6 = 6 ms P2 4 = 4 ms P3 7 = 7 ms The average waiting time is 17/3 = 5.66 milliseconds. Multilevel Queue Scheduling For processes that can be easily classified into different groups. 8 Subject : OPS - 12178 Chapter 4 Process Scheduling For example, a common division is made between foreground (interactive) processes and background (batch) processes. The ready is partition queue into several separate queues. The processes are permanently assigned to one queue, generally based on some property of the process, such as memory size, process priority, or process type. Each queue has its own scheduling algorithm. For example the foreground queue might be scheduled by an RR algorithm, while the background queue is scheduled by an FCFS algorithm. There is scheduling among the different queues, which is implemented as fixed-priority preemptive scheduling. Each queue has absolute priority over lower-priority queues. An example of a multilevel queue scheduling algorithm with five queues, listed below in order of priority: 1. System processes 2. Interactive processes 3. Interactive editing processes 4. Batch processes 5. Student processes Process in the batch queue will be executed after the queues system processes, interactive processes, and interactive editing processes are all empty. If an interactive editing process entered the ready queue while a batch process is running, the batch process would be preempted. Another possibility is that each queue is given a time slice. i.e. each queue gets a certain portion of the CPU time. For instance, in the foreground-background queue example, the foreground queue can be given 80 percent of the CPU time for RR scheduling among its processes, whereas the background queue receives 20 percent of the CPU to give to its processes on an FCFS basis. Multilevel Feedback-Queue Scheduling A process can move between queues. It uses many ready queues and associates a different priority with each queue. Processes are separated according to their CPU bursts. If a process uses too much CPU time, it will be moved to a lower-priority queue. This scheme leaves I/O-bound and interactive processes in the higher-priority queues. 9 Subject : OPS - 12178 Chapter 4 Process Scheduling In addition, a process that waits too long in a lower-priority queue may be moved to a higherpriority queue. This form of aging prevents starvation. Multilevel-feedback-queue scheduler defined by the following parameters: 1. number of queues 2. scheduling g algorithms for each queue 3. method used to determine when to upgrade a process 4. method used to determine when to demote a process 5. method used to determine which queue a process will enter when that process needs service For example :- Three queues: o Q0 – time quantum 8 milliseconds o Q1 – time quantum 16 milliseconds o Q2 – FCFS A process entering the ready queue is put in queue O. A process in queue 0 is given a time quantum of 8 milliseconds. If it does not finish within this time, it is moved to the tail of queue 1. If queue 0 is empty, the process at the head of queue 1 is given a quantum of 16 milliseconds. If it does not complete, it is preempted and is put into queue 2. Processes in queue 2 are run on an FCFS basis but are run only when queues 0 and 1 are empty. Queue Q0 has highest priority and queue Q2 has lowest priority. Processes in High priority queue will preempt the processes in low priority queue. Processes in queue 2 will only be executed if queues 0 and 1 are empty. A process that arrives for queue 1 will preempt a process in queue 2. Multiple-Processor Scheduling One approach to CPU scheduling in a multiprocessor system has all scheduling decisions, I/O processing, and other system activities handled by a single processor—the master server. The other processors execute only user code. This asymmetric multiprocessing is simple because only one processor accesses the system data structures, reducing the need for data sharing. A second approach uses symmetric multiprocessing (SMP), where each processor is self-scheduling. All processes may be in a common ready queue, or each processor may have its own private queue of ready processes. Regardless, scheduling proceeds by having the scheduler for each processor examine the ready queue and select a process to execute. If we have multiple processors trying to access and update a common data structure, the scheduler must be programmed carefully: We must ensure that two processors do not choose the same process and that processes are not lost from the queue. Virtually all modern operating systems support SMP, including Windows XP, Windows 2000, Solaris, Linux, and Mac OS X. In the remainder of this section, we will discuss issues concerning SMP systems. Deadlock In a multiprogramming environment, several processes may compete for a finite number of resources. 10 Subject : OPS - 12178 Chapter 4 Process Scheduling A process requests resources; and if the resources are not available at that time, the process enters a waiting state. Sometimes, a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes. This situation is called a deadlock. Necessary Conditions A deadlock situation can arise if the following four conditions hold simultaneously in a system: 1. Mutual exclusion. At least one resource must be held in a non-sharable mode; that is, only one process at a time can use the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released. 2. Hold and wait. A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes. 3. No preemption. Resources cannot be preempted; that is, a resource can be released only voluntarily by the process holding it, after that process has completed its task. 4. Circular wait. A set {P0, P1, ..., Pn} of waiting processes must exist such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, •••, Pn-1 is waiting for a resource held by Pn, and Pn, is waiting for a resource held by Pn. Methods for Handling Deadlocks We can deal with the deadlock problem in one of three ways: o We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state. o We can allow the system to enter a deadlock state, detect it, and recover. o We can ignore the problem altogether and pretend that deadlocks never occur in the system. Deadlock prevention provides a set of methods for ensuring that at least one of the necessary conditions cannot hold. Deadlock avoidance requires that the operating system be given in advance additional information concerning which resources a process will request and use during its lifetime. So that OS can decide the process should wait or not. The system provides an algorithm that examines the state of the system to determine whether a deadlock has occurred and an algorithm to recover from the deadlock. The third solution is the one used by most operating systems, including UNIX and Windows. This method is cheaper than the prevention, avoidance, or detection and recovery methods, which must be used constantly Deadlock Prevention For a deadlock to occur, each of the four necessary conditions must hold. By ensuring that at least one of these conditions cannot hold, we can prevent the occurrence of a deadlock. Mutual Exclusion: Most of the resources must be sharable and the mutual-exclusion condition must hold for non-sharable resources like a printer. Sharable resources do not require mutually exclusive access and thus cannot be involved in a deadlock. 11 Subject : OPS - 12178 Chapter 4 Process Scheduling Hold and Wait: Process is to be allocated all its resources before it begins execution. An alternative protocol allows a process to request resources only when it has none. A process may request some resources and use them. Before it can request any additional resources, however, it must release all the resources that it is currently allocated. No Preemption: If a process requests additional resources and the resources cannot be allocated then all resources currently being held by the process are released. The process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Alternatively, check if the resources are available. If they are allocate them. If not and allocated to a waiting process then resources of waiting process are released and allocated to the requesting process. Circular Wait: All the resources types are ordered and requested in an increasing order of enumeration.