Os Unit 4
Os Unit 4
4.1 Scheduling types-Scheduling objective, CPU and I/O burst cycles, Pre-
emptive, Non-Pre-emptive.
4.2 Types of scheduling algorithms-First come first served (FCFS), shortest job
first (SJF), Shortest Remaining Time (SRTN), Round Ribon(RR) Priority
scheduling, multilevel queue scheduling.
4.3 Critical section problem.
4.4 Deadlock- system, Models, Necessary condition leading to Deadlocks,
Deadlock Handling-Preventions, avoidance and Recovery
****************************************************************
Scheduling
• The scheduler (dispatcher) is the module that gets invoked when a
context switch needs to happen and manipulates the queues, moving jobs
to and fro.
• The scheduling algorithm determines which jobs are chosen to run next
and what queues they wait on.
• In general, the scheduler runs:
• When a job switches states (running, waiting, etc.)
• When an interrupt occurs
• When a job is created or terminated
• Scheduling algorithms in two contexts as below-
• A preemptive scheduler can interrupt a running job• A non-
preemptive scheduler waits for running job to block
• Many potential goals of scheduling algorithms
• Utilization, throughput, wait time, response time, etc.
• Various algorithms to meet these goals -FCFS/FIFO, SJF, Priority, RR.
CPU Scheduling
• CPU scheduling is the process of switching the CPU among various
processes, the operating system can make the computer more productive.
Pre-emptive Scheduling
• Preemptive scheduling is used when a process switches from running
state to ready state or from waiting state to ready state.
• The resources (mainly CPU cycles) are allocated to the process for the
limited amount of time and then is taken away, and the process is
again placed back in the ready queue if that process still has CPU
burst time remaining.
• That process stays in ready queue till it gets next chance to execute.
Non-Pre-emptive Scheduling
• Non-pre-emptive Scheduling is used when a process terminates, or a
process switch from running to waiting state.
• In this scheduling, once the resources (CPU cycles) is allocated to a
process, the process holds the CPU till it gets terminated or it reaches
a waiting state.
• In case of non-pre-emptive scheduling does not interrupt a process
running CPU in middle of the execution.
• Instead, it waits till the process complete its CPU burst time and then
it can allocate the CPU to another process.
Advantages-
• It is simple and easy to understand.
• It can be easily implemented using queue data structure.
• It does not lead to starvation.
Disadvantages-
• It does not consider the priority or burst time of the processes.
• It suffers from convoy effect i.e. processes with higher burst time arrived
before the processes with smaller burst time
FCFS Example 1
Consider the processes P1, P2, P3 given in the below table, arrives for execution
in the same order, with Arrival Time 0, and given Burst Time
Solution Gantt
Consider the processes P1, P2, P3, P4 given in the below table, arrives for
execution in the same order, with given Arrival Time and Burst Time.
Solution Gantt
Advantages-
• SJF is optimal and guarantees the minimum average waiting time.
• It provides a standard for other algorithms since no other algorithm
performs better than it.
Disadvantages-
• It can not be implemented practically since burst time of the processes
can not be known in advance.
• It leads to starvation for processes with larger burst time.
• Priorities can not be set for the processes.
• Processes with larger burst time have poor response time.
Advantages-
• It gives the best performance in terms of average response time.
• It is best suited for time sharing system, client server architecture and
interactive system
Disadvantages-
• It leads to starvation for processes with larger burst time as they have to
repeat the cycle many times.
• Its performance heavily depends on time quantum.
• Priorities can not be set for the processes.
Priority Scheduling
• Priority Scheduling
→ Choose next job based on priority
→ Airline check-in for first class passengers
→ Can implement SJF, priority = 1/(expected CPU burst)
→ Also can be either preemptive or non-preemptive
→ This is what you’re implementing in Nachos in Project 1
• Problem
→ Starvation – low priority jobs can wait indefinitely
• Solution
• “Age” processes
• Increase priority as a function of waiting time
• Decrease priority as a function of CPU consumption
• The SJF algorithm is a special case of the general priorityscheduling
algorithm.
• A priority number (integer) 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.
• The CPU is allocated to the process with the highest priority (smallest
integer º highest priority)
• There are two types of priority scheduling.
• Preemptive priority scheduling.
• Non-preemptive priority scheduling.
• SJF is a priority scheduling where priority is the predicted next CPU burst
time.
• Priorities can be defined either internally or externally.
o Internally defined priorities use some measurable quantities to
compute priority of a process. For example, time limits, memory
requirements, the number of open files etc.
o External priorities are set by criteria that are external to the operating
system, such as importance of the process and other political factors.
• Preemptive priority scheduling: 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.
• Non-premptive priority scheduling: A “non-preemptive priority”
scheduling algorithm will simply put the new process at the head of the
ready queue.
• Problem “ Starvation” – low priority processes may never execute
Deadlock
• In multiprogramming environment, several processes may compete for a
finite number of resources.
• A process requests
resources; if the resources
are not available at that
time, the process enters a
wait state. Waiting
processes may never again
change state, because the
resources they have
requested are held by other
waiting processes. This
situation is called a
deadlock.
• When a process request for resources held by another waiting process
which in turn is waiting for resources held by another waiting process and
not a single process can execute its task, then deadlock occurs in the
system.
Deadlock Prevention
• Deadlock prevention is a set of methods for ensure that at least any one of
the four necessary conditions (like mutual exclusion, hold and wait, no
pre-emption and circular wait) cannot hold.
• These methods prevent deadlocks by constraining how requests for
resources can be made.
• By ensuring that that at least one of these conditions cannot hold, the
occurrence of a deadlock can be prevented.
Deadlock Avoidance
• An alternative method for avoiding deadlocks is to require additional
information about how resources are to be requested.
• Each request requires the system consider the resources currently
available, the resources currently allocated to each process, and the future
requests and releases of each process, to decide whether the could be
satisfied or must wait to avoid a possible future deadlock.
Deadlock Recovery
• Deadlock recovery occurs when a deadlock is detected. When a deadlock
is detected, our system stops working, and when the deadlock is resolved,
our system resumes operation.
• As a result, after detecting deadlock, a method or way must be required to
recover that deadlock in order to restart the system. The technique is
known as deadlock recovery.
• There are three basic approaches to getting out of a bind:
o Inform the system operator and give him/her permission to intervene
manually.
o Stop one or more of the processes involved in the deadlock.
o Prevent the use of resources.
• There are three basic methods of deadlock recovery:
1. Deadlock recovery through preemption–
o The ability to take a resource away from a process, have another
process use it, and then give it back without the process noticing. It
is highly dependent on the nature of the resource.
o Deadlock recovery through preemption is too difficult or
sometimes impossible.
22 | P a g e 186_Shraddha Keshao Bonde_N2
CM3101
***************************************************************