ProcessManagementScheduling-Part2
ProcessManagementScheduling-Part2
Process Scheduling
• CPU scheduling is the basis of multiprogrammed operating systems. By
switching the CPU among processes, the operating system can make the
computer more productive.
• 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.
• 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.
Process Scheduling
• The success of CPU scheduling depends on an observed property of
processes:
• process execution consists of a cycle of
• CPU execution and I/0 wait.
• Processes alternate between these two states. 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/0 burst, and so on.
Eventually, the final CPU burst ends with a system request to
terminate execution.
Alternating sequence of CPU and I/O bursts
Histogram of CPU-burst durations
• The durations of CPU bursts have been measured extensively.
Although they vary greatly from process to process and from
computer to computer, they tend to have a frequency curve similar to
that shown in Figure
Histogram of CPU-burst durations
• The curve is generally 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.
CPU Scheduler
• Whenever the CPU becomes idle, the operating system must select one of
the processes in the ready queue to be executed.
• The selection process is carried out by the short-term scheduler (or CPU
scheduler).
• The scheduler selects a process from the processes in memory that are
ready to execute and allocates the CPU to that process.
• Note that the ready queue is not necessarily a first-in, first-out (FIFO)
queue.
• A ready queue can be implementedd as a FIFO queue,priority queue, a
tree, or simply an unordered linked list.
• Conceptually, however, all the processes in the ready queue are lined up waiting for a chance to
run on the CPU. The records in the queues are generally process control blocks (PCBs) of the
processes.
Preemptive Scheduling
• 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/0 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
(for example, when an interrupt occurs).
3. When a process switches from the waiting state to the ready state
(for example, at completion of I/0)
4. When a process terminates.
Preemptive Scheduling
• 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.
• When scheduling takes place only under circumstances 1 and 4, we say
that the scheduling scheme is nonpreemptive or cooperative; otherwise, it
is preemptive.
• Under nonpreemptive 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.
• This scheduling method was used by Microsoft Windows 3.x;
• Windows 95 introduced preemptive scheduling, and all subsequent
versions of Windows operating systems have used preemptive scheduling.
Preemptive Scheduling
• In this scheduling, a process currently executing can be interrupted
and moved back to the ready queue if a higher-priority process
arrives or the CPU time slice for the process expires.
• It's used when the operating system needs to prioritize
responsiveness and fairness.
• It Ensures high-priority tasks are executed quickly.
• It may lead to higher overhead due to frequent context switching.
Preemptive Scheduling
• Let’s consider Round Robin (RR) scheduling:
• There are three processes:
• P1 (Burst time: 4 units)
• P2 (Burst time: 3 units)
• P3 (Burst time: 2 units)
• Time Quantum: 2 units
• Execution:
• First, P1 runs for 2 units → P2 runs for 2 units → P3 runs for 2 units.
• Then, P1 resumes for its remaining 2 units → P2 resumes for 1 unit.
• Here, processes are preempted after their time quantum expires or when a
higher-priority process arrives.
Non-Preemptive Scheduling
• In this scheduling, a process currently executing on the CPU runs until
it either finishes or voluntarily yields the CPU (e.g., by waiting for I/O).
• It’s used where tasks must be completed once they start without
interruption.
• Simpler to implement with less overhead.
• Can lead to starvation of lower-priority processe
Example of Non-Preemptive Scheduling
• Let’s consider First-Come, First-Served (FCFS) scheduling:
• There are three processes:
• P1 (Burst time: 4 units)
• P2 (Burst time: 3 units)
• P3 (Burst time: 2 units)
• Execution:
• P1 executes fully for 4 units → P2 executes fully for 3 units → P3
executes fully for 2 units.
• No process is interrupted until it completes.
Difference
Preemptive Scheduling Non-Preemptive Scheduling
CPU can be taken away from a process before it CPU is assigned to a process until it
completes execution. completes or voluntarily releases it.
Allows interruption of a running process. Once a process starts, it runs until it finishes.
Used in batch processing and simpler
Used in multitasking and time-sharing systems.
scheduling methods.
Example: Round Robin, Shortest Remaining Example: First Come First Serve (FCFS),
Time First (SRTF) Shortest Job Next (SJN)
Race Condition
• Preemptive scheduling can result in race conditions when data are
shared among several processes. Consider the case of two processes
that share data. While one process is updating the data, it is
preempted so that the second process can run. The second process
then tries to read the data, which are in an inconsistent state.
• A race condition occurs in a multi-threaded or multi-process system
when two or more processes access and modify shared data at the
same time, leading to unexpected or incorrect results. It happens
because the execution order of the processes is unpredictable.
Example: Two Processes Writing to the Same
File
• Imagine two processes, Process A and Process B, writing to the same
log file at the same time.
• Scenario (Without Synchronization)
• Process A opens log.txt, reads the content, and prepares to write "Process A executed\n".
• Process B also opens log.txt, reads the same content, and prepares to write "Process B executed\n".
• Both processes write to the file simultaneously, but since they don’t wait for each other:
• The content gets mixed up or overwritten.
• Some messages may be lost.
Why Does This Happen?
• Both processes access the file without locking it.
• They overwrite each other’s data, leading to incomplete or corrupted
output.
How to Prevent This?
• File Locking: The OS should lock the file so only one process can write
at a time.
• Synchronization Mechanisms: Use mutex, semaphores, or atomic
operations to prevent simultaneous access.
• Write Queue: Processes should wait in a queue instead of writing
simultaneously
Impact on Kernel Design
• Complexity: A preemptive kernel is harder to design because it needs
mechanisms to pause and resume tasks properly.
• Efficiency vs. Simplicity: Preemptive kernels make systems faster and
more responsive, but require careful handling of shared resources.
• Safety (Concurrency Issues): When tasks switch frequently, the OS
must prevent race conditions (e.g., two chefs using the same knife at
the same time).
Preemptive Scheduling
• Windows & Linux: Use a preemptive kernel (chef can be
interrupted).
• Older Operating Systems (DOS, early UNIX): Used non-preemptive
kernels (chef must finish before switching).
What Happens Inside the Kernel?
• The kernel is the core part of the operating system that manages
CPU, memory, and devices. It decides which process gets to use the
CPU and for how long.
• When we talk about preemption, we are asking:
"Can the OS interrupt a running process and give the CPU to another
process?"
Preemptive Kernel in Action (Modern OS like
Linux & Windows)
• In a preemptive kernel, the OS can pause a running process and
switch to another.
• Example:
• You are watching a video on YouTube.
• Suddenly, you get a WhatsApp call.
• The OS pauses the video player and gives CPU time to the calling app.
• After the call, the video resumes without restarting.
Preemptive Kernel in Action (Modern OS like
Linux & Windows)
• Why is this good?
• The system feels responsive because urgent tasks (incoming call) don’t wait.
• Multi-tasking works smoothly, allowing many apps to run at once.
• What’s the challenge?
• The OS must save the state of the paused process and restore it later.
• Frequent switching increases CPU overhead (context switching).
Non-Preemptive Kernel in Action (Older OS
like DOS, Early UNIX)
• In a non-preemptive kernel, once a process starts running, it keeps
the CPU until it finishes or voluntarily releases it.
• Example:
• You are printing a large document.
• Meanwhile, you try to open a browser.
• But the OS won’t let you use the browser until printing is completely done,.
Non-Preemptive Kernel in Action (Older OS
like DOS, Early UNIX)
• Why is this good?
• Simple to implement, no need to manage interruptions.
• Less CPU overhead, since the OS doesn’t need to keep switching task
• Why is this bad?
• The system feels slow and unresponsive.
• A single long task can block everything else (e.g., a stuck process can freeze the system).
Impact on Real-World Kernel Design
• Modern Operating Systems (Windows, Linux, macOS)
• Use a preemptive kernel for smooth multi-tasking.
• High-priority tasks (like responding to user input) always get CPU time
quickly.
• Requires synchronization mechanisms (mutex, semaphores) to handle shared
resources safely.
• Embedded Systems (Some IoT Devices, Old OS)
• May use a non-preemptive kernel because they run only one or two tasks at
a time.
• Example: A microwave oven OS doesn’t need preemption because it only
runs a few simple tasks (heating, timing, beeping).
Preemptive Scheduling
• Preemption makes modern OS faster and more efficient but needs
careful management.
• Non-preemptive systems are simpler but outdated for most general-
purpose computers.
• That’s why modern OS kernels prioritize preemptive scheduling to
ensure smooth performance.
Dispatcher
• Another component involved in the CPU-scheduling function is the
dispatcher. The dispatcher is the module that gives control of the
CPU’s core to the process selected by the CPU scheduler. This function
involves the following:
• Switching context from one process to another
• Switching to user mode
• Jumping to the proper location in the user program to resume that program
• The dispatcher should be as fast as possible, since it is invoked during
every context switch. The time it takes for the dispatcher to stop one
process and start another running is known as the dispatch latency.
The role of the dispatcher
Preemptive Scheduling
• The Mac OS X operating system for the Macintosh also uses
preemptive scheduling; previous versions of the Macintosh operating
system relied on cooperative scheduling. Cooperative scheduling is
the only method that can be used on certain hardware platforms,
because it does not require the special hardware (for example, a
timer) needed for preemptive scheduling.
• Preemptive scheduling incurs a cost associated with access to shared
data. Consider the case of two processes that share data. While one is
updating the data, it is preempted so that the second process can
run. The second process then tries to read the data, which are in an
inconsistent state. In such situations, we need new mechanisms to
coordinate access to shared data;
Preemptive Scheduling
• Preemption also affects the design of the operating-system kernel. During the processing of a
system call, the kernel may be busy with an activity on behalf of a process.
• Such activities may involve changing important kernel data (for instance, I/O queues).
• What happens if the process is preempted in the middle of these changes and the kernel (or the
device driver) needs to read or modify the same structure? Chaos ensues.
• Operating-system kernels can be designed as either nonpreemptive or preemptive.
• A nonpreemptive kernel will wait for a system call to complete or for a process to block while
waiting for I/O to complete to take place before doing a context switch. This scheme ensures that
the kernel structure is simple, since the kernel will not preempt a process while the kernel data
structures are in an inconsistent state. Unfortunately, this kernel-execution model is a poor one
for supporting real-time computing, where tasks must complete execution within a given time
frame.
• We need to explore scheduling demands of real-time systems. A preemptive kernel requires
mechanisms such as mutex locks to prevent race conditions when accessing shared kernel data
structures.
• Most modern operating systems are now fully preemptive when running in kernel mode
Preemptive Scheduling
• Because interrupts can, by definition, occur at any time, and because
they cannot always be ignored by the kernel, the sections of code
affected by interrupts must be guarded from simultaneous use.
• The operating system needs to accept interrupts at almost all times.
Otherwise, input might be lost or output overwritten. So that these
sections of code are not accessed concurrently by several processes,
they disable interrupts at entry and re-enable interrupts at exit.
• It is important to note that sections of code that disable interrupts do
not occur very often and typically contain few instructions
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. Many criteria have been
suggested for comparing CPU-scheduling algorithms'. The criteria include
the following:
• CPU utilization
• Throughput
• Turnaround time
• Waiting time
• Response time
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 loaded system).
• CPU utilization can be obtained by using the top command on Linux,
macOS, and UNIX systems.
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 over several
seconds; for short transactions, it may be tens of processes per
second
Turnaround time
• 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 in the ready
queue, executing on the CPU, and doing I/O.
Waiting time
• The CPU-scheduling algorithm does not affect the amount of time
during which a process executes or does I/O.
• It affects only 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
• Another measure is the time from the submission of a request until
the first response is produced. This measure, called response time, is
the time it takes to start responding, not the time it takes to output
the response.
Scheduling Criteria
• It is desirable to maximize CPU utilization and throughput and to
minimize turnaround time, waiting time, and response time. In most
cases, we optimize the average measure.
Scheduling Algorithms
• CPU scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU’s core. There
are many different CPU scheduling algorithms.
1. First-Come, First-Served Scheduling
2. Shortest-Job-First Scheduling
3. Round-Robin Scheduling
4. Priority Scheduling
First-Come, First-Served Scheduling
• First-Come, First-Serve (FCFS) is the simplest CPU scheduling
algorithm, where processes are executed in the order they arrive. It
follows a FIFO (First-In, First-Out) queue mechanism:
• When a process enters the ready queue, its Process Control Block
(PCB) is added to the end of the queue.
• The CPU is allocated to the process at the head of the queue.
• Once the running process completes, it is removed from the queue,
and the next process gets the CPU.
• This method is non-preemptive, meaning once a process starts
execution, it cannot be interrupted until it finishes or requests I/O.
First-Come, First-Served Scheduling
• Consider the following set of processes that arrive at time 0, with the
length of the CPU burst given in milliseconds: Process Burst Time
P1 24
P2 3
P3 3
• 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, which is a bar chart
that illustrates a particular schedule, including the start and finish times of
each of the participating processes.
First-Come, First-Served Scheduling
• The waiting time is 0 milliseconds for process P1, 24 milliseconds for
process P2, and 27 milliseconds for process P3. Thus, 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:
• Gantt chart:
Shortest-Job-First (SJF) Scheduling
• Process P1 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 average waiting time for this example is [(10 − 1) + (1
− 1) + (17 − 2) + (5 − 3)]/4 = 26/4 = 6.5 milliseconds. Nonpreemptive
SJF scheduling would result in an average waiting time of 7.75
milliseconds.
Drawbacks of SJF
• Starvation Risk: Longer processes may face indefinite delays if shorter
processes keep arriving.
• Implementation Difficulty: Predicting CPU burst times accurately is
challenging.
• SJF is highly efficient but requires careful burst-time estimation and
handling of process starvation.
Round-Robin Scheduling
• The round-robin (RR) scheduling algorithm is similar to FCFS
scheduling, but preemption is added to enable the system 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 milliseconds in length. The
ready queue is treated as a circular queue.
• The CPU scheduler goes around the ready queue, allocating the CPU
to each process for a time interval of up to 1 time quantum.
Round-Robin Scheduling
• To implement RR scheduling, we again treat the ready queue as a FIFO
queue of processes. New processes are added to the tail of the ready
queue.
• The CPU scheduler picks the first process from the ready queue, sets a
timer to interrupt after 1 time quantum, and dispatches the process.
• One of two things will then happen. The process may have a CPU burst of
less than 1 time quantum. In this case, the process itself will release the
CPU voluntarily. The scheduler will then proceed to the next process in the
ready queue. If the CPU burst of the currently running process is longer
than 1 time quantum, the timer will go off and will cause an interrupt to
the operating system. A context switch will be executed, and the process
will be put at the tail of the ready queue. The CPU scheduler will then
select the next process in the ready queue.
Round-Robin Scheduling
• The average waiting time under the RR policy is often long. Consider the
following set of processes that arrive at time 0, with the length of the CPU burst
given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
Although the time quantum should be large compared with the context switch time, it should not be too large.
if the time quantum is too large, RR scheduling degenerates to an FCFS policy.
A rule of thumb is that 80 percent of the CPU bursts should be shorter than the time quantum.
Priority Scheduling
• The SJF algorithm is a special case of the general priority-scheduling
algorithm.
• 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.
• An SJF algorithm is simply a priority algorithm where the priority (p) is
the inverse of the (predicted) next CPU burst. The larger the CPU
burst, the lower the priority, and vice versa.
Priority Scheduling
• Priority in scheduling in measured terms of high priority and low
priority.
• Priorities are generally indicated by some fixed range of numbers,
such as 0 to 7 or 0 to 4,095. However, there is no general agreement
on whether 0 is the highest or lowest priority. Some systems use low
numbers to represent low priority; others use low numbers for high
priority. This difference can lead to confusion.
• We will assume that low numbers represent high priority.
Priority Scheduling
• As an example, consider the following set of processes, assumed to
have arrived at time 0 in the order P1, P2, ···, P5, with the length of
the CPU burst given in milliseconds:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority Scheduling
• Using priority scheduling, we would schedule these processes according to the following
Gantt chart: