CPU Scheduling is the basis of multi-programmed operating systems. By switching the CPU among processes, the operating system can make the computer more productive.
1. Basic concepts:
Scheduling is a fundamental operating system functions. 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.
CPU – I/O Burst Cycle:
Process execution consists of a cycle of CPU execution and I/O wait. Processes alternate between these two state.
CPU scheduling decisions may take place under the following two ways:
(i) Non preemptive: In this case, once a process is in the running state, it continues to execute until (a) it terminates or (b) it blocks itself to wait for I/O or to request some operating system service.
(ii) Preemptive: The currently running process may be interrupted and moved to the ready state by the operating system. The decision to preempt may be performed when a new process arrives; when an interrupt occurs that places a blocked process in the ready state; or periodically, based on a clock interrupt.
2. Scheduling Criteria
Many criteria have been suggested for comparing CPU scheduling algorithms. The characteristics used for comparison can make a substantial difference in the determination of the best algorithm.
The criteria include the following:
Throughput: The number of processes completed per unit time, called throughput.
It should be maximized
Turn around Time (TAT): The interval from the time of submission of a process to the time of completion is turn around time.
It should be minimized
Waiting time: Waiting time is the sum of the periods spent waiting in the ready queue.
Or
It should be minimized.
Response time: Time between submission and first response It should be minimized.
TAT = Completion time – Arrival time
Waiting time = TAT – CPU Burst time
Waiting time = Completion time – Arrival time – CPU Burst time
Scheduling algorithms:
CPU scheduling deals with the problem of deciding which of the processes in the ready queue are to be allocated to the CPU. There are following types of scheduling:
1. First-Come, First-Served (FCFS) scheduling: Processes are scheduled in the order they are
2. Shortest-Job-First (SJF) scheduling: Selects the process with the shortest expected processing time, and do not preempt the process.
Advantage:Minimum average waiting time
Disadvantage:(1) There is no way to know the length of the next CPU burst.
(2) The constant arrival of small jobs can lead to indefinite blocking (Starvation)of a long job.
3. Shortest-Remaining-Time-First (SRTF) scheduling: Selects the process with the shortest expected remaining process time. A process may be preempted when another process becomes ready.
4. Priority scheduling: Priority scheduling requires each process to be assigned, a priority value. The CPU is allocated to the process with the highest priority. FCFS can be used in case of a tie.
Priority scheduling can be either preemptive or non-preemptive
A major problem with priority scheduling algorithm is indefinite blocking (or starvation) A solution to the problem of indefinite blocking of low- priority processes is aging. Aging is a technique of gradually increasing the priority of processes that wait in the system for a long time.
5. Round-Robin (RR) Scheduling: This algorithm is designed especially for time sharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes.
A small unit of time, called a time quantum (or time slice) is defined. A time quantum is generally from 10 to 100 millisecond.
To implement RR scheduling, we keep the ready queue as First In First Out (FIFO) queue of processes. New processes are added to the tail of the ready queue
The RR scheduling algorithm is preemptive.
If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunk of at most q time units.
Each process must wait no longer than (n-1)×q time units until its next time quantum
The performance of the RR algorithm depends heavily on the size of the time quantum
At one extreme, if the time quantum is very large, the RR policy is the same as the FCFS policy
If the time quantum is very small (say 1 micro second) the RR approach is called processor sharing
6. Multilevel queue (MLQ) scheduling: A multilevel queue scheduling algorithm partitions the ready 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.
Foreground – RR Back ground- FCFS
7. Multilevel Feedback Queue Scheduling: In MLQ (Multilevel Queue) algorithm, processes are permanently assigned to a queue on entry to the system. Processes do not move between queues. But in multilevel feedback queue scheduling, however, it allows a process to move between queues. The idea is to separate processes with different CPU burst characteristics.
If a process uses too much CPU time, it will be moved to a lower priority queue. This scheme levels I/O bound and interactive processes in the higher-priority queues. Similarly, a process that waits too long in a lower-priority queue. This form of aging prevents starvation.
Queue 0
1
2
Quantum = 8
Quantum = 16
FCFS
Fig. 4.4.2.Multilevel feedback queues System process
Interactive process
Interactive editing process
Batch process
Student process Highest priority
Lowest priority
Fig: 4.4.1. Multilevel Queue Scheduling
4.5: Deadlocks
A process requests resources: If the resources are not available at that time the process enters a wait state. Waiting process may never again change state, because the resources they have requested are held by other waiting processes this situation is called a deadlock.
A deadlock situation can arise if the following four condition hold simultaneously in a system.
(i) Mutual exclusion: At a time only one process can use the resource.
(ii) Hold and wait: A process must hold atleast one resource while waiting, to acquire additional requested resources that are currently being held by other processes.
(iii) Circular wait: In circular wait, the processes in the system form a circular list or chain where each process in the list is waiting for a resource held by the next process in the list.
Fig 4.5.1
(iv) No preemption: We can’t take resources forcefully from any process.
Deadlock can be described more precisely in terms of a directed graph called a system Resource Allocation Graph.
This graph consists of a set of vertices V and a set of edges E.
The set of vertices V is partitioned into two different types of nodes P={P1, P2 , . . . .Pn} the set consisting of all the active processes in the system and R={ R1, R2 , . . . .Rn} resources.
Directed edge Pi Rj : process Pi requested an instance of resources type Rj
Rj Pi: an instance of resource type Rj has been allocated to process Pi
For a single instance: cycle is necessary and sufficient condition for deadlock.
For multiple instances: cycle is necessary but not sufficient condition.
R P signifies, Resource R held by Process P.
& P R signifies Process P is waiting for Resource R.
The request for R by R may now be granted and all the arcs pointing towards P may be erased.
Similarly, the arcs for P can be earased and the reduced graph has no arcs.
Methods for handling deadlocks
By ensuring that at least one of these conditions can not hold we can prevent the occurrence of deadlock.
(a) Mutual exclusion: The mutual- exclusion condition must hold for non sharable resources.
(b) Hold and wait : Two possibilities are there:
(i) The process request to be granted all resources it needs at once, prior to execution.
(ii) It can request any additional resources, however, it must release all the resources that it is currently allocated.
Disadvantage:
(i) waiting time is high.
(ii) resources utilization is low.
(iii) Request resources only when the process has none before it can request any additional resources however it must release all the resources that it is currently allocated.
(iv) starvation is possible.
(c) Circular wait: Each resource is numbered here, and all the processes are forced to request the resources in a numerical order. This protocol ensures that RAG (Resource Allocation Graph) can never have a cycle.
(d) No preemption: To prevent deadlock, no preemption ensures that this condition should not hold to make following protocols.
(i) if a process holding one resource Ri requests for another resource suppose Rjthen it should first release its previously held resource Ri.
(ii) if a process requests for resources and resources are available, allocate them to process. If resources are not available and allocated to other process that is waiting for other resources in such situation, preempt the resources from waiting process and allocate them to
Resources allocation graph with a deadlock
R1