OS Unit-2
OS Unit-2
PROCESS SCHEDULING
Basic Concepts
• In a single-processor system,
▪ Only one process may run at a time.
▪ Other processes must wait until the CPU is rescheduled.
• Objective of multiprogramming:
▪ To have some process running at all times, in order to maximize
CPU utilization.
CPU Scheduler
• This scheduler
▪ selects a waiting-process from the ready-queue and
▪ allocates CPU to the waiting-process.
• The ready-queue could be a FIFO, priority queue, tree and list.
• The records in the queues are generally process control blocks (PCBs) of the
processes.
CPU Scheduling
Four situations under which CPU scheduling decisions take place:
1. When a process switches from the running state to the waiting state. For ex; I/O
request.
2. When a process switches from the running state to the ready state. For ex:
when an interrupt occurs.
3. When a process switches from the waiting state to the ready state. For ex:
completion of I/O.
4. When a process terminates.
• Scheduling under 1 and 4 is non- preemptive. Scheduling under 2 and 3 is preemptive.
Preemptive Scheduling
• This is driven by the idea of prioritized computation.
• Processes that are runnable may be temporarily suspended
• Disadvantages:
1. Incurs a cost associated with access to shared-data.
2. Affects the design of the OS kernel.
Dispatcher
• It gives control of the CPU to the process selected by the short-term scheduler.
• The function involves:
1. Switching context
2. Switching to user mode&
3. Jumping to the proper location in the user program to restart that program
• It should be as fast as possible, since it is invoked during every process switch.
• Dispatch latency means the time taken by the dispatcher to
▪ stop one process and
▪ start another running.
SCHEDULING CRITERIA:
In choosing which algorithm to use in a particular situation, depends upon the properties
of the various algorithms. Many criteria have been suggested for comparing CPU- scheduling
algorithms. The criteria include the following:
CPU utilization: We want to keep the CPU as busy as possible. Conceptually, 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: These the number of processes executed per one unit of time. The unit of time may
be one minute or one hour.
Turnaround time: This is the important criterion which tells 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/0.
Waiting time: It is the time period between arrival time of the process into ready queue to first
response of the process.
Response time: The time interval when first response is displayed to the user regarding process
execution.
SCHEDULING ALGORITHMS
CPU scheduling deals with the problem of deciding which of the processes in the ready
queue is to be allocated the CPU.
Following are some scheduling algorithms:
1. FCFS scheduling (First Come First Served)
2. SJF scheduling (Shortest Job First)
3. Priority scheduling
4. SRTF scheduling
5. Round Robin scheduling
6. Multilevel Queue scheduling
7. Multilevel Feedback Queue scheduling
Priority scheduling
1. FCFS Scheduling
• The process that requests the CPU first is allocated the CPU first.
• The implementation is easily done using a FIFO queue.
• Procedure:
1. When a process enters the ready-queue, its PCB is linked onto the tail of the queue.
2. When the CPU is free, the CPU is allocated to the process at the queue’s head.
3. The running process is then removed from the queue.
Advantage:
1. Code is simple to write & understand.
Disadvantages:
1. Convoy effect: All other processes wait for one big process to get off the CPU.
2. Non-preemptive (a process keeps the CPU until it releases it).
3. Not good for time-sharing systems.
4. The average waiting time is generally not minimal.
• Example: Suppose that the processes arrive in the order P1, P2, P3.
• The Gantt Chart for the schedule is as follows:
2.SJF Scheduling
• The CPU is assigned to the process that has the smallest next CPU burst.
• If two processes have the same length CPU burst, FCFS scheduling is used to
break the tie.
• For long-term scheduling in a batch system, we can use the process time
limit specified by the user, as the ‘length’
• SJF can't be implemented at the level of short-term scheduling, because
there isno way to know the length of the next CPU burst
• Advantage:
1. The SJF is optimal, i.e. it gives the minimum average waiting time
for agiven set of processes.
• Disadvantage:
1. Determining the length of the next CPU burst.
3.Preemptive SJF/SRTF:
Consider the following set of processes, with the length
4.Priority Scheduling
• A priority is associated with each process.
• The CPU is allocated to the process with the highest priority.
• Equal-priority processes are scheduled in FCFS order
• Advantage:
▪ Higher priority processes can be executed first.
• Disadvantage:
▪ Indefinite blocking, where low-priority processes are left waiting
indefinitely for CPU. Solution: Aging is a technique of increasing
priority of processes that wait in system for a long time.
• Example: Consider the following set of processes, assumed to have arrived at time
0, in the order PI, P2, ..., P5, with the length of the CPU-burst time given in
milliseconds.
• The Gantt chart for the schedule is as follows:
.
Figure 2.20 Multilevel feedback queues
Deadlocks
• Deadlock is a situation in which each process is waiting for a resources never changes
its state. Because the resource is held by other waiting processes.
• Let P1 & P2 are two processes and R1&R2 are two resources.
• Here P1 & P2 will never change its state from waiting. This situation is called
Deadlock.
System Model
• A system consists of a finite number of resources to be distributed among a
number of competing processes.
• If a system has two CPUs, then the resource type CPU has two instances. Similarly, the
resource type printer may have five instances.
• If a process requests an instance, the allocation should satisfy the request.
• A process must request a resource, use resource and release the after using it.
• A process may request as many resources as it requires to carry out its task.
• A process may utilize a resource in only the following sequence:
• Request. The process requests the resource. If the request cannot be granted immediately
(for example, if the resource is being used by another process), then the requesting process
must wait until it can acquire the resource.
• Use. The process can operate on the resource.
• Release. The process releases the resource.
The request and release of resources may be system calls
Deadlock Characterization
The features that characterize deadlock are
• Necessary Conditions
• Resource-Allocation Graph
Necessary Conditions
A deadlock situation can arise if the following four conditions occur simultaneously in a
system:
• Mutual exclusion. 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.
• 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.
• No preemption. Resources cannot be preempted; that is, a resource can be released
only after that process has completed its task.
• 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 P0.
Resource-Allocation Graph
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
– The mutual exclusion condition must hold. That is, at least one
resource must be nonsharable. Such as printer cannot be
simultaneously shared by several processes.
– Shared resources such as read only files do not require mutual
exclusion condition.
– Several process can attempt to open a read only file
• No Preemption
– If a process is holding some resources and requests another resource that
cannot be immediately allocated to it (that is, the process must wait),
then all resources the process is currently holding are preempted.
– Alternatively, if a process requests some resources, we first check
whether they are available. If they are, we allocate them. If they are not,
we check whether they are allocated to some other process that is
waiting for additional resources.
– If so, we preempt the desired resources from the waiting process and
allocate them to the requesting process.
• Circular wait
– let R = R1, R2, ..., Rm be the set of resource types.
– We assign unique integer number to each resource type, which allows us to
compare two resources and to determine whether one precedes another
– Each process can request resources only in a ring order of the resource type
R.
– A process has requested for Ri & then request for Rj
– If F(Rj) > F(Ri)
– Whenever a process request an instances of resources type Rj it must have
released resources Ri such that F(Rj) >= F(Ri)
Deadlock Avoidance
• It is to grant only those request for available resources that cannot possibly
result in state of deadlock.
• This algorithm checks the resources allocation state to make use the circular
wait condition never exist.
Safe State
• The critical part of deadlock avoidance is the safe state.
• A state is safe if the system can allocate resources to each process in
some order and still avoid a deadlock.
• A system is in a safe state only if there exists a safe sequence.
• A sequence of processes <P1, P2, ..., Pn> is a safe sequence, where for each Pi
the resources that Pi can still request can satisfy by the currently available
resources plus the resources held by Pj.
• If the resource that process Pi needs is not immediately available
then Pi has wait until Pj has finished.
• A Safe state is not a deadlock state
• A deadlock state is an unsafe state
• Not all unsafe state are deadlock, however unsafe state may lead to a
deadlock
1. Resource-Allocation-Graph Algorithm
• If all resources in the system has only one instance then the RAG can be
used for deadlock avoidance.
• In addition to the request & assignment edge, it consist of a new type of
edge called claim edge.
• A claim edge is represented in the graph by - - - -> (dashed)
• When a process Pi requests resource Rj , the claim edge
Pi - - - > Rj is converted to a request edge.
• Similarly, when a resource Rj is released by Pi , the assignment edge Rj 88 Pi is
converted to a claim edge Pi - - - > Rj
• Suppose that process Pi requests resource Rj . The request can be granted only if
converting the request edge Pi 88Rj to an assignment edge Rj 88 Pi does not
result in the formation of a cycle in the resource-allocation graph.
• If no cycle exists, then the allocation of the resource will leave the system in a
safe state.
• If a cycle is found, then the allocation will put the system in an unsafe state.
• In that case, process Pi will have to wait for its requests to be satisfied.
• The name was chosen because the algorithm could be used in a banking system
• When a new process enters the system, it must declare the maximum
number of instances of each resource type that it may need.
• This number may not exceed the total number of resources in the system.
• When a user requests a set of resources, the system must determine
whether the allocation of these resources will leave the system in a safe
state.
• The resources are allocated if the system in safe state otherwise,
the process must wait
• Resource type A has ten instances, resource type B has five instances, and
resource type C has seven instances.
• One is simply to abort one or more processes to break the circular wait.
• The other is to preempt some resources from one or more of the deadlocked
processes.
• Process Termination
• Resource Preemption
– Selecting a victim
– Rollback
– Starvation