ADITYA COLLEGE OF ENGINEERING & TECHNOLOGY
Operating Systems
UNIT - II
By
[Link] DIVYA, [Link](Ph.D)
[Link]
Dept of CSE-AIML
Aditya College of Engineering & Technology
Surampalem.
8/14/2025 Introduction to Machine Learning [Link] DIVYA [Link]
Processes :
Process Concept
Process scheduling
Operations on processes
Inter-process communication.
Department of CSE, Aditya College of
Engineering & Technology, Surampalem 2
Process is a program in execution.
Examples of "processes" include:
◦ opening a web browser,
◦ playing a music file,
◦ running a word processor document,
◦ compiling code,
◦ sending an email,
◦ downloading a file, or
◦ even just the background processes that keep your computer running smoothly
Process and job can be used interchangeably.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 3
The status of the current activity of a process is represented by the
value of the program counter and the contents of the processor’s
registers.
The memory layout of a process is typically divided into multiple
sections:
◦ Text section— the executable code
◦ Data section—global variables
◦ Heap section—memory that is dynamically allocated during program
run time
◦ Stack section— temporary data storage when invoking functions (such
as function parameters, return addresses, and local variables)
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 4
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 5
As a process executes, it changes state. The state of a process is defined in part by the current
activity of that process. A process may be in one of the following states:
◦ new: The process is being created
◦ running: Instructions are being executed
◦ waiting: The process is waiting for some event to occur
◦ ready: The process is waiting to be assigned to a processor
◦ terminated: The process has finished execution
Only one process can be running on any processor core at any instant. Many processes may be
ready and waiting.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 6
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 7
Each process is represented in the operating system by a process control block (PCB)—also
called a task control block.
It contains many pieces of information associated with a specific process, including these:
◦ Process state :The state may be new, ready, running, waiting, halted, and so on.
◦ Program counter : The counter indicates the address of the next instruction to be executed for
this process
◦ CPU registers : The registers vary in number and type, depending on the computer architecture.
They include accumulators, index registers, stack pointers, and general-purpose registers, plus
any condition-code information
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 8
CPU-scheduling information : This information
includes a process priority, pointers to scheduling
queues, and any other scheduling parameters
Memory-management information : This information
may include such items as the value of the base and
limit registers and the page tables, or the segment
tables, depending on the memory system used by the
operating system
Accounting information : This information includes
the amount of CPU and real time used, time limits,
account numbers, job or process numbers, and so on.
I/O status information : This information includes the
list of I/O devices allocated to the process, a list of open
files, and so on
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 9
In general, most processes can be described as either -
◦ I/O bound : An I/O-bound process is one that spends more of its time doing I/O than it
spends doing computations
◦ CPU bound : A CPU-bound process, in contrast, generates I/O requests infrequently, using
more of its time doing computations.
The number of processes currently in memory is known as the degree of multiprogramming
The objective of multiprogramming is to have some process running at all times so as to
maximize CPU utilization.
The objective of time sharing is to switch a CPU core among processes so frequently that users
can interact with each program while it is running.
To meet these objectives, the process scheduler selects an available process (possibly from a set
of several available processes) for program execution on a core.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 10
The various scheduling queues of processes are :
◦ Job queue – set of all processes in the system
◦ Ready queue – set of all processes residing in main memory, ready and waiting to execute
◦ Wait queue - Processes that are waiting for a certain event to occur — such as completion of
I/O
Processes migrate among the various queues
These queues are generally stored as a linked list;
◦ a queue header contains pointers to the first PCB in the list, and each PCB includes a pointer
field that points to the next PCB in the queue
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 11
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 12
A common representation of process scheduling is a queueing diagram.
Two types of queues are present:
◦ the ready queue and
◦ a set of wait queues.
The circles represent the resources that serve the queues, and the arrows indicate the flow
of processes in the system.
A new process is initially put in the ready queue. It waits there until it is selected for
execution, or dispatched.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 13
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 14
Once the process is allocated a CPU core and is executing, one of several events could occur:
◦ The process could issue an I/O request and then be placed in an I/O wait queue.
◦ The process could create a new child process and then be placed in a wait queue while it
awaits the child’s termination.
◦ The process could be removed forcibly from the core, as a result of an interrupt or having its
time slice expire, and be put back in the ready queue.
A process continues this cycle until it terminates, at which time it is removed from all queues
and has its PCB and resources deallocated.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 15
2.2 CPU Scheduling
The role of the CPU scheduler is to select from among the processes that are in the ready
queue and allocate a CPU core to one of them.
The CPU scheduler must select a new process for the CPU frequently
A process can be “swapped out” from memory to disk, where its current status is saved,
and later “swapped in” from disk back to memory, where its status is restored. This is
called swapping .
◦ This is used to reduce degree of multiprogramming.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 16
When an interrupt occurs, the system needs to save the current context of the process running on
the CPU core so that it can restore that context when its processing is done, essentially
suspending the process and then resuming it.
◦ The context includes the value of the CPU registers, the process state , and memory-
management information(which is represented in the PCB of the process)
We perform a state save of the current state of the CPU core, be it in kernel or user mode, and
then a state restore to resume operations.
Switching the CPU core to another process requires performing a state save of the current
process and a state restore of a different process. This task is known as a context switch.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 17
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 18
When a context switch occurs, the kernel saves the context of the old process in its PCB
and loads the saved context of the new process scheduled to run.
Drawback:
Context switch time is pure overhead, because the system does no useful work while
switching.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 19
3. Operations on Processes
The processes in most systems can execute concurrently, and they may be created and
deleted dynamically. Thus, these systems must provide a mechanism for
1. Process creation
2. Process termination
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 20
During the course of execution, a process may create several new processes. The creating
process is called a parent process, and the new processes are called the children of that
process.
Most operating systems (including UNIX, Linux, and Windows) identify processes according to
a unique process identifier (or pid), which is typically an integer number.
◦ The pid provides a unique value for each process in the system, and it can be used as an index
to access various attributes of a process within the kernel.
On UNIX and Linux systems, we can obtain a listing of processes by using the ps command.
For example, the command :
ps -el
will list complete information for all processes currently active in the system.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 21
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 22
The systemd process (which always has a pid of 1) serves as the root parent process for
all user processes
◦ It is the first user process created when the system boots.
Two children of systemd — logind and sshd.
◦ The logind process is responsible for managing clients that directly log onto the
system.
◦ a client has logged on and is using the bash shell, which has been assigned pid 8416.
◦ Using the bash command-line interface, this user has created the process ps as well as
the vim editor.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 23
When a process creates a child process, that child process will need certain resources (CPU
time, memory, files, I/O devices) to accomplish its task.
A child process may be able to obtain its resources directly from the operating system, or it
may be constrained to a subset of the resources of the parent process.
The parent may have to partition its resources among its children, or it may be able to share
some resources (such as memory or files) among several of its children.
Restricting a child process to a subset of the parent’s resources prevents any process from
overloading the system by creating too many child processes
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 24
When a process creates a new process, two possibilities for execution exist:
◦ 1. The parent continues to execute concurrently with its children.
◦ 2. The parent waits until some or all of its children have terminated.
There are also two address-space possibilities for the new process:
◦ 1. The child process is a duplicate of the parent process (it has the same program and data as
the parent).
◦ 2. The child process has a new program loaded into it.
◦ when a child process is created, it can either be a mirror image of the parent, or it can be told
to run a completely different program.
System Calls used to create processes :
◦ fork() : Unix/Linux
◦ CreateProcess() :Windows
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 25
The fork system call is used for creating a new process in Linux, and Unix systems, which is
called the child process, which runs concurrently with the process that makes the fork() call
(parent process). Different values returned by fork():
◦ Negative Value: The creation of a child process was unsuccessful.
◦ Zero: Returned to the newly created child process.
◦ Positive value: Returned to parent or caller. The value contains the process ID of the newly
created child process.
getpid() function returns the process ID of the calling process.
getppid() function returns the parent process ID of the calling process
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 26
wait(NULL) will block the parent process until any of its children has finished.
◦ If the child terminates before the parent process reaches wait(NULL) then the child process turns to
a zombie process until its parent waits on it and its released from memory.
◦ If the parent process doesn't wait for its child, and the parent finishes first, then the child process
becomes an orphan and is assigned to init as its child. And init will wait and release the process
entry in the process table.
execlp() system call is used to replace the current process image with a new process image
specified by a filename, essentially allowing you to run a new program within the current
process.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 27
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 28
3.2 Process Termination
A process terminates when it finishes executing its final statement and asks the
operating system to delete it
System Calls used to terminate processes :
◦ exit() : Unix/Linux
◦ TerminateProcess():Windows
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 29
A parent may terminate the execution of one of its children for a variety of reasons, such as
these:
The child has exceeded its usage of some of the resources that it has been allocated.
The task assigned to the child is no longer required.
The parent is exiting, and the operating system does not allow a child to continue if its
parent terminates.
Some systems do not allow a child to exist if its parent has terminated. In such systems, if a
process terminates (either normally or abnormally), then all its children must also be
terminated. This phenomenon, referred to as cascading termination, is normally initiated by
the operating system
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 30
Processes executing concurrently in the operating system may be either
◦ independent process : A process is independent if it does not share data with any other
processes executing in the system , or
◦ cooperating process : A process is cooperating if it can affect or be affected by the
other processes executing in the system.
Reasons for providing an environment that allows process cooperation:
Information sharing
Computation speedup.
Modularity
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 31
Cooperating processes require an interprocess communication (IPC) mechanism that will allow
them to exchange data— that is, send data to and receive data from each other.
There are two fundamental models of inter process communication:
◦ shared memory : a region of memory that is shared by the cooperating processes is
established. Processes can then exchange information by reading and writing data to the
shared region .
◦ message passing : communication takes place by means of messages exchanged between
the cooperating processes.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 32
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 33
A shared-memory region resides in the address space of the process creating the shared-memory
segment.
◦ Other processes that wish to communicate using this shared-memory segment must attach it
to their address space.
The processes are also responsible for ensuring that they are not writing to the same location
simultaneously.
Producer– Consumer Problem : A producer process produces information that is consumed
by a consumer process .
◦ Ex :- client–server paradigm : a server as a producer and a client as a consumer. a web
server produces (that is, provides) web content such as HTML files and images, which are
consumed (that is, read) by the client web browser requesting the resource.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 34
One solution to the producer–consumer problem uses shared memory. To allow producer and
consumer processes to run concurrently, we must have available a buffer of items that can be
filled by the producer and emptied by the consumer
A producer can produce one item while the consumer is consuming another item. The producer
and consumer must be synchronized, so that the consumer does not try to consume an item that
has not yet been produced.
Two types of buffers can be used :
◦ The unbounded buffer places no practical limit on the size of the buffer. The consumer may
have to wait for new items, but the producer can always produce new items.
◦ The bounded buffer assumes a fixed buffer size. In this case, the consumer must wait if the
buffer is empty, and the producer must wait if the buffer is full.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 35
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 36
Message passing provides a mechanism to allow processes to communicate and to synchronize
their actions without sharing the same address space.
It is particularly useful in a distributed environment, where the communicating processes may
reside on different computers connected by a network.
A message-passing facility provides at least two operations:
◦ send(message)
◦ receive(message)
If processes P and Q want to communicate, they must send messages to and receive messages
from each other: a communication link must exist between them.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 37
Several methods for logically implementing a link and the send()/receive() operations:
Direct or indirect communication
Synchronous or asynchronous communication
Automatic or explicit buffering
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 38
Processes that want to communicate must have a way to refer to each other. They can use
either direct or indirect communication.
Under direct communication, each process that wants to communicate must explicitly name
the recipient or sender of the communication.
In this scheme, the send() and receive() primitives are defined as:
symmetry in addressing
◦ send(P, message) — Send a message to process P.
◦ receive(Q, message)—Receive a message from process Q.
asymmetry in addressing
◦ send(P, message)—Send a message to process P.
◦ receive(id, message)—Receive a message from any process.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 39
With indirect communication, the messages are sent to and received from mailboxes, or
ports.
A mailbox can be viewed abstractly as an object into which messages can be placed by
processes and from which messages can be removed.
A process can communicate with another process via a number of different mailboxes,
but two processes can communicate only if they have a shared mailbox.
The send() and receive() primitives are defined as follows:
◦ send(A, message)—Send a message to mailbox A.
◦ receive(A, message)—Receive a message from mailbox A.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 40
4.2.2 Synchronization
Message passing may be either blocking or nonblocking— also known as synchronous
and asynchronous.
◦ Blocking send. The sending process is blocked until the message is received by the
receiving process or by the mailbox.
◦ Nonblocking send. The sending process sends the message and resumes operation.
◦ Blocking receive. The receiver blocks until a message is available.
◦ Nonblocking receive. The receiver retrieves either a valid message or a null.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 41
4.2.3 Buffering
Whether communication is direct or indirect, messages exchanged by communicating processes
reside in a temporary queue.
Basically, such queues can be implemented in three ways:
◦ Zero capacity: The queue has a maximum length of zero; thus, the link cannot have any
messages waiting in it. In this case, the sender must block until the recipient receives the
message.
◦ Bounded capacity : The queue has finite length n; thus, at most n messages can reside in it. If
the queue is not full when a new message is sent, the message is placed in the queue and the
sender can continue execution without waiting. If the link is full, the sender must block until
space is available in the queue
◦ Unbounded capacity : The queue’s length is potentially infinite; thus, any number of
messages can wait in it. The sender never blocks.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 42
CPU Scheduling :
Basic Concepts,
Scheduling criteria,
Scheduling algorithms,
Multiple Processor scheduling.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 43
The objective of multiprogramming is to have some process running at all times, to
maximize CPU utilization
When one process has to wait for an I/O or an event, the operating system takes the
CPU away from that process and gives the CPU to another process.
Every time one process has to wait, another process can take over use of the CPU
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 44
The success of CPU scheduling depends on an observed
property of processes: process execution consists of a
cycle of CPU execution and I/O 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 anotherI/O burst, and so on.
Eventually, the final CPU burst ends with a system request
to terminate execution
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 45
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 CPU scheduler, which selects a process from
the processes in memory that are ready to execute and allocates the CPU to that process.
There are three types of process schedulers:
◦ Long term Scheduler or Job Scheduler
◦ Short term Scheduler or CPU Scheduler.
◦ Medium term Scheduler
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 46
Long term Scheduler or Job Scheduler
◦ Long Term Scheduler loads a process from disk to main memory for execution. i.e., a
new process to the ‘Ready State’.
◦ It mainly moves processes from Job Queue to Ready Queue.
◦ It controls the Degree of Multi-programming, i.e., the number of processes present in
a ready state or in main memory at any point in time.
Short term Scheduler or CPU Scheduler :
◦ CPU Scheduler is responsible for selecting one process from the ready state for
running state (or assigning CPU to it).
◦ It mainly calls dispatcher.
◦ Fastest among the three (that is why called Short Term).
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 47
Medium Term Scheduler :
◦ Medium Term Scheduler (MTS) is responsible for moving a process from memory to disk
(or swapping).
◦ Running processes may become suspended if it makes an I/O request.
◦ A suspended processes cannot make any progress towards completion. In this condition, to
remove the process from memory and make space for other processes, the suspended
process is moved to the secondary storage. This process is called swapping, and the process
is said to be swapped out or rolled out. Swapping may be necessary to improve the process
mix (of CPU bound and I/O bound)
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 48
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 49
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 50
Preemptive Scheduling :The operating system can interrupt or preempt a running process to
allocate CPU time to another process, typically based on priority or time-sharing policies.
Mainly a process is switched from the running state to the ready state.
◦ EX : Round Robin (RR) , Shortest Remaining Time First (SRTF) , Priority (preemptive
version)
Non-preemptive scheduling : A running process cannot be interrupted by the operating
system; it voluntarily relinquishes control of the CPU.
◦ In this scheduling, once the resources (CPU cycles) are allocated to a process, the process
holds the CPU till it gets terminated or reaches a waiting state
◦ First Come First Serve, Shortest Job First (SJF basically non preemptive) and Priority (non
preemptive version)
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 51
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 time it takes for the dispatcher to stop one
process and start another running is known as the
dispatch latency
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 52
The scheduling 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 loaded system).
Throughput : 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.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 53
Waiting time : Waiting time is the sum of the periods spent waiting in the ready queue
Response time : Time from the submission of a request until the first response is produced
is called response time .
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.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 54
Preemptive Scheduling Algorithms Formulas
1. Completion Time = Time at which Process terminates (see Gantt chart)
2. Turnaround Time = Completion time- Arrival time OR waiting time+ Burst Time
3. Waiting Time = Turnaround time – Burst Time
4. Response Time (In Preemptive) = When Process Get the CPU first Time (from Gantt chart) –
Arrival time
5. Response Ratio = (Waiting_time + Service_Time) / service_time
6. Avg. Turnaround Time = Sum of turnaround time of all processes/ total processes
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 55
Non-Preemptive Scheduling Algorithms Formulas
1. Completion Time = Time at which Process terminates (see Gantt chart)
2. Turnaround Time = Completion time- Arrival time OR waiting time+ Burst Time
3. Waiting Time = Turnaround time – Burst Time
4. Response Time = In Non-Preemptive Response Time= Waiting time
5. Response Ratio = (waiting_time + service_time) / service_time
6. Avg. Turnaround Time = Sum of turnaround time of all processes/ total processes
Note: The amount of time a process needs to utilize the CPU to complete its execution is called
service time or burst time
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 56
First Come, First Serve (FCFS) is one of the simplest types of CPU scheduling algorithms.
The process that requests the CPU first is allocated the CPU first. The implementation of the
FCFS policy is easily managed with a FIFO queue.
FCFS Scheduling is a non-preemptive algorithm, meaning once a process starts running, it
cannot be stopped until it voluntarily relinquishes the CPU, typically when it terminates or
performs I/O.
This method treats all processes equally, without priority distinctions.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 57
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 58
DRAWBACK:
CONVOY EFFECT :
◦ As it is a Non-preemptive CPU Scheduling Algorithm, FCFS can result in long waiting times,
especially if a long process arrives before a shorter one. This is known as the convoy effect,
where shorter processes are forced to wait behind longer processes, leading to inefficient
execution.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 59
Problem 1: 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
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 60
This algorithm associates with each process the length of the process’s next CPU burst time.
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.
This scheduling method can be
◦ Non preemptive :SJF
◦ Preemptive : SRTF (Shortest Remaining Time First/Next)
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 61
Non preemptive SJF
◦ The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with
the smallest execution time to execute next. SJF is also known as Shortest Job Next (SJN).
◦ Shortest Job first has the advantage of having a minimum average waiting time among all scheduling
algorithms.
Preemptive SJF SRTF (Shortest Remaining Time First)
◦ Preemptive version of Shortest Job First (SJF) scheduling is called Shortest Remaining Time First (SRTF).
◦ In SRTF, the process with the least time left to finish is selected to run. The running process will continue
until it finishes or a new process with a shorter remaining time arrives. This way, the process that can finish
the fastest is always given priority.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 62
Non preemptive SJF
Note: If burst time is the same for two processes then check their the arrival time. if arrival time is also same
then check their the process ID. Any process having less burst time or arrival time or process ID is executed
first.
63
Department of CSE, Aditya College of Engineering & Technology, Surampalem
Preemptive SJF SRTF (Shortest Remaining Time First)
Note: The shortest remaining Time or job depends on Burst time.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 64
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 slice.
It is called "round robin" because the system rotates through all the processes, allocating each of
them a fixed time slice or "quantum", regardless of their priority.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 65
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 66
Priority scheduling is one of the most common scheduling algorithms used by the operating system
to schedule processes based on their priority.
Each process is assigned a priority. The process with the highest priority is to be executed first and
so on.
◦ Processes with the same priority are executed on a first-come first served basis.
Priority Scheduling can be implemented in two ways:
◦ Non-Preemptive Priority Scheduling
◦ Preemptive Priority Scheduling
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 67
Non-Preemptive Priority Scheduling
◦ InNon-Preemptive Priority Scheduling, the CPU is not taken away from the running process.
Even if a higher-priority process arrives, the currently running process will complete first.
◦ Ex: A high-priority process must wait until the currently running process finishes.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 68
Preemptive Priority Scheduling :
◦ In Preemptive Priority Scheduling, the CPU can be taken away from the currently running
process if a new process with a higher priority arrives.
◦ Ex: A low-priority process is running, and a high-priority process arrives; the CPU
immediately switches to the high-priority process.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 69
STARVATION :
A major problem with priority scheduling algorithms is indefinite blocking, or starvation.
A process that is ready to run but waiting for the CPU can be considered blocked.
A priority scheduling algorithm can leave some low priority processes waiting indefinitely.
In a heavily loaded computer system, a steady stream of higher-priority processes can prevent a
low-priority process from ever getting the CPU
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 70
AGING :
◦ A solution to the problem of indefinite blockage of low-priority processes is aging. Aging
involves gradually increasing the priority of processes that wait in the system for a long time
Initial Priority
Process Arrival CPU Burst
(0=high)
P1 0 ms 10 20 ms
P2 0 ms 15 10 ms
P3 (low) 0 ms 45 5 ms
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 71
AGING Example
Initial Priority
Process Arrival CPU Burst
(0=high)
P1 0 ms 10 20 ms
P2 0 ms 15 10 ms
P3 (low) 0 ms 45 5 ms
Aging policy: every 5 ms in the ready queue, priority is decremented by 5 (min = 0).
Timeline
During this, P2 and P3 each wait, so every 5 ms their priority improves:
At 5 ms: P2 → 10, P3 → 40
At 10 ms: P2 → 5, P3 → 35
At time = 10 ms, P2 priority (5) becomes higher than P1 (10), so P2 preempts and runs.
Meanwhile P3 has fallen to 35, still too low.
After P2 completes (~time=20 ms), priorities are:
P3 → 35 + aging if it waits more (two rounds: at 15 and 20 ms → 30).
P1 currently at 10 (still running it would have greater priority than P3).
P1 resumes or if P1 is done, P3 eventually crosses priority threshold and gets scheduled.
📌 Even though P3 started way behind, aging pushes it closer until it finally executes — no starvation.
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 72
AGING Example
Initial Priority
Process Arrival CPU Burst
(0=high)
P1 0 ms 10 20 ms
P2 0 ms 15 10 ms
P3 (low) 0 ms 45 5 ms
If P3 waited further:
t = 25 ms: P3 = 25
t = 30 ms: P3 = 20
t = 35 ms: P3 = 15
…
t = 40 ms: P3 = 10 ⇒ ties with P1 or P2 (then FCFS used)
Department of CSE, Aditya College of Engineering &
Technology, Surampalem 73
Threads and Concurrency:
Multithreading models
Thread libraries
Threading issues
Thread Scheduling
Multiple processor scheduling
Real-Time CPU Scheduling
Example is a web server - Multiple threads allow for multiple requests to be satisfied
simultaneously, without having to service requests sequentially or to fork off separate processes for
every incoming request.
Definition of Thread
1. A thread is the unit of execution within a process.
(Or)
Thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of
registers, ( and a thread ID. )
3. Threads are also known as Lightweight processes.
4. A traditional (or heavyweight) process has a single thread of control.
5. If a process has multiple threads of control, it can perform more than one task at a time.
6. Most software applications that run on modern computers are multithreaded.
7. An application typically is implemented as a separate process with several threads of control
Example: word processor (MS Word)
The MS-Word process could involve many threads:
1. Interaction with the keyboard § Display of characters on the display page
2. Regularly saving file to disk
3. Controlling spelling and grammar Etc.
4. All these threads would share the same document
Benefits To Multi-threading:
There are four major categories of benefits to multi-threading:
1. Responsiveness - One thread may provide rapid response while other threads are blocked or slowed down
doing intensive calculations.
2. Resource sharing - By default threads share common code, data, and other resources, which allows
multiple tasks to be performed simultaneously in a single address space.
3. Economy – Allocating memory and resources for process creation is costly. Because threads share
resources of the process to which they belong, it is more economical to create and context-switches threads.
4. Scalability: Utilization of multiprocessor architectures - A single threaded process can only run on one
CPU, no matter how many may be available, whereas the execution of a multi-threaded application may be
split amongst available processor. In less time we will get output.
1) Process-based Multitasking (Multiprocessing)
1. Each process have its own address in memory i.e. each process allocates separate memory area.
2. Process is heavyweight.
3. Cost of communication between the process is high.
4. Switching from one process to another require some time for saving and loading registers,
memory maps, updating lists etc.
2) Thread-based Multitasking (Multithreading)
1. Threads share the same address space.
2. Thread is lightweight.
3. Cost of communication between the thread is low.
Types of Thread
There are two types of threads:
1. User Threads 2. Kernel Threads
1. User threads are above the kernel and without kernel support. These are the threads that application programmers use
in their programs.
Implementation of User Level thread is done by a thread library and is easy.
Thread creation, switching, and synchronization are handled by the user space library.
Example of User Level threads: Java thread, POSIX threads.
2. Kernel threads are supported within the kernel of the OS itself.
All modern OSs support kernel-level threads, allowing the kernel to perform multiple simultaneous tasks
and/or to service multiple kernel system calls simultaneously.
While the Implementation of the kernel-level thread is done by the operating system and is complex.
Example of Kernel level threads: Window Solaris.
Multithreading Models
1. The user threads must be mapped to kernel threads, by one of the following strategies:
1. Many to One Model
2. One to One Model
3. Many to Many Model
[Link]-To-One Model
• Here many user-level threads are all mapped onto a single kernel thread.
• Thread management is handled by the thread library in user space, which is very efficient.
Disadvantage: the entire process will block if a thread makes a blocking system call.
• Because a single kernel thread can operate only on a single CPU, the
many-to-one model does not allow individual processes to be split
across multiple CPUs. In multiprocessor system as only one kernel is present
we can’t achieve the parallelism
One-To-One Model
• The one-to-one model creates a separate kernel thread to
handle each user thread.
• One-to-one model overcomes the problems listed above
involving blocking system calls and the splitting of processes
across multiple CPUs.
•This model provides more concurrency and if one thread blocks, other threads can still run.
• However the overhead of managing the one-to-one model is
more significant, involving more overhead and slowing down the system.
• Most implementations of this model place a limit on how many threads can be created.
Many-To-Many Model
• The many-to-many model multiplexes any number of user threads onto an equal or smaller
number of kernel threads.
• Here 4 user level threads are multiplexing with
3 kernel level threads.
• In this model, developers can create as many user threads as
necessary and the corresponding Kernel threads can run in
parallel on a multiprocessor machine.
• This model provides the best accuracy on concurrency and
when a thread performs a blocking system call, the kernel
can schedule another thread for execution.
This model combines the advantages of the other two models.
Thread libraries
• Thread libraries provide programmers with an API for creating and managing threads.
• Thread libraries may be implemented either in user space or in kernel space. The former
involves API functions implemented solely within user space, with no kernel support. The latter
involves system calls, and requires a kernel with thread library support.
• There are three main thread libraries in use today:
1. POSIX Pthreads - may be provided as either a user or kernel library, as an extension to the
POSIX standard.
2. Win32 threads - provided as a kernel-level library on Windows systems.
3. Java threads - Since Java generally runs on a Java Virtual Machine, the implementation of
threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or
Win32 threads depending on the system.
• There are a variety of issues to consider with multithreaded programming
1. Semantics of fork() and exec() system calls
2. Thread cancellation
3. Signal handling
4. Thread pooling
5. Thread-specific data
1. Semantics of fork() and exec()
• The fork() and exec() are the system calls. The fork() call creates a duplicate process of the process
that invokes fork(). The new duplicate process is called child process and process invoking the
fork() is called the parent process.
• Let us now discuss the issue with the fork() system call.
Consider that a thread of the multithreaded program has invoked the fork().
ISSUE:
1. Here the issue is whether the new duplicate process created by fork() will duplicate all the threads of the parent
process or
2. The duplicate process would be single-threaded.
Solution:
There are two versions of fork() in some of the UNIX systems. Either the fork() can duplicate all the threads of the
parent process in the child process or the fork() would only duplicate that thread from parent process that has invoked it.
Which version of fork() must be used totally depends upon the application.
Next system call i.e. exec() system call when invoked replaces the program along with all its threads with the program
that is specified in the parameter to exec(). Typically the exec() system call is lined up after the fork() system call.
Here the issue is if the exec() system call is lined up just after the fork() system call then duplicating all the threads of
parent process in the child process by fork() is useless.
As the exec() system call will replace the entire process with the process provided to exec() in the parameter.
Thread Cancellation
Thread cancellation involves terminating a thread before it has completed.
A thread that is to be cancelled is often referred to as the target thread.
Cancellation of a target thread may occur in two different scenarios:
1. Asynchronous cancellation:
One thread immediately terminates the target thread.
2. Deferred cancellation.
The target thread periodically checks whether it should terminate, allowing it an opportunity to
terminate itself in an orderly fashion.
The issue related to the target threads are listed below:
1. What if the resources had been allotted to the cancel target thread?
2. What if the target thread is terminated when it was updating the data, it was sharing with some
other thread.
Here the asynchronous cancellation of the thread where a thread immediately cancels the target
thread without checking whether it is holding any resources or not creates troublesome.
However, in deferred cancellation, the thread that indicates the target thread about the
cancellation, the target thread crosschecks its flag in order to confirm that it should it be cancelled
immediately or not. The thread cancellation takes place where they can be cancelled safely such
points are termed as cancellation points by Pthreads.
Signal Handling
A signal is used in UNIX systems to notify a process that a particular event has occurred. A signal may be
received either synchronously or asynchronously,
Synchronous signals are delivered to the same process that performed the operation that caused the signal
Examples of synchronous signal include illegal memory access and division by 0.
Asynchronous Signal are generated by an event external to a running process, that process receives the
signal asynchronously.
Examples of such signals include terminating a process with specific keystrokes (such as <control><C>)
and having a timer expire.
Handling signals in single-threaded programs is straightforward: Signals are always delivered to a process.
However, delivering signals is more complicated in multithreaded programs, where a process may have
several threads. Where, then, should a signal be delivered?
In general, the following options exist:
1. Deliver the signal to the thread to which the signal applies.
2. Deliver the signal to every thread in the process.
3. Deliver the signal to certain threads in the process.
4. Assign a specific thread to receive all signals for the process.
So if the signal is synchronous it would be delivered to the specific thread causing the generation of the
signal.
If the signal is asynchronous it cannot be specified to which thread of the multithreaded program it
would be delivered.
If the asynchronous signal is notifying to terminate the process the signal would be delivered to all the
thread of the process.
The issue of an asynchronous signal is resolved up to some extent in most of the multithreaded UNIX
system.
Here the thread is allowed to specify which signal it can accept and which it cannot.
However, the Window operating system does not support the concept of the signal instead it uses
asynchronous procedure call (ACP) which is similar to the asynchronous signal of the UNIX system.
Thread Pool
When a user requests for a webpage to the server, the server creates a separate thread to service the request.
Although the server also has some potential issues. Consider if we do not have a bound on the number of
actives thread in a system and would create a new thread for every new request then it would finally result in
exhaustion of system resources.
The idea is to create a finite amount of threads when the process starts. This collection of threads is referred to
as the thread pool.
A thread pool is a group of threads that have been pre-created and are available to do work as needed
1. Threads may be created when the process starts
2. A thread may be kept in a queue until it is needed
3. After a thread finishes, it is placed back into a queue until it is needed again
4. Avoids the extra time needed to spawn new threads when they’re needed
In applications where threads are repeatedly being created/destroyed thread pools might provide a
performance benefit
Example: A server that spawns a new thread each time a client connects to the system and discards that thread
when the client disconnects
Advantages of thread pools:
1. Typically faster to service a request with an existing thread than create a new thread (performance
benefit)
2. Bounds the number of threads in a process
The only threads available are those in the thread pool
If the thread pool is empty, then the process must wait for a thread to re-enter the pool before it can assign
work to a thread Without a bound on the number of threads in a process, it is possible for a process to
create so many threads that all of the system resources are exhausted
Thread Specific data
We all are aware of the fact that the threads belonging to the same process share the data of that process. Here the issue
is what if each particular thread of the process needs its own copy of data. So the specific data associated with the
specific thread is referred to as thread-specific data.
1. Consider a transaction processing system, here we can process each transaction in a different thread. To determine each
transaction uniquely we will associate a unique identifier with it. Which will help the system to identify each transaction
uniquely.
2. As we are servicing each transaction in a separate thread.
So we can use thread-specific data to associate each thread to a specific transaction and its unique id. Thread libraries
such as Win32, Pthreads and Java support to thread-specific data.
So these are threading issues that occur in the multithreaded programming environment. We have also seen how these
issues can be resolved.
Thread Scheduling
Thread scheduling is the process by which the operating system's scheduler selects a thread from
the ready queue to run on the CPU.
Threads are lightweight processes that share the same address space and resources within a process
but have their own program counters, registers, and stacks.
Thread States
Threads can be in one of the following states:
Ready: The thread is ready to execute but waiting for the CPU.
Running: The thread is currently executing on the CPU.
Blocked/Waiting: The thread is waiting for an event (e.g., I/O completion, a lock, or a signal).
Thread Scheduling Algorithms
Thread scheduling can be preemptive (the scheduler can interrupt a running thread) or non-
preemptive (a thread runs until it voluntarily yields the CPU).
Common algorithms include:
a. First-Come, First-Served (FCFS)
Non-preemptive.
Threads are executed in the order they arrive.
Simple but can lead to long waiting times for short threads.
b. Shortest Job First (SJF)
Non-preemptive or preemptive (Shortest Remaining Time First, SRTF).
The thread with the shortest execution time is scheduled next.
Minimizes average waiting time but requires knowledge of execution times.
c. Priority Scheduling
Threads are assigned priorities; higher-priority threads run first.
Can be preemptive or non-preemptive.
Risk of starvation (low-priority threads never run).
d. Round Robin (RR)
Preemptive.
Each thread gets a fixed time slice (quantum) to execute.
Fair but can have high overhead due to frequent context switches.
e. Multilevel Queue Scheduling
Threads are grouped into multiple queues (e.g., foreground/interactive and background/batch).
Each queue can use a different scheduling algorithm.
f. Multilevel Feedback Queue (MLFQ)
Threads can move between queues based on their behavior (e.g., aging to prevent starvation).
Combines the benefits of priority and round-robin scheduling.
Multiple processor scheduling
What is Multiple-Processor Scheduling?
In systems containing more than one processor,
multiple-processor scheduling addresses task
allocations to multiple CPUs.
It will involve higher throughputs since several tasks
can be processed concurrently in separate processors.
It would also involve the determination of which
CPU handles a particular task and balancing loads
between available processors.
Multiple processor scheduling
Approaches to Multiple-Processor Scheduling
Symmetric Multiprocessing : where each processor is self scheduling. All processes may be in a
common ready queue or each processor may have its own private queue for ready processes. The
scheduling proceeds further by having the scheduler for each processor examine the ready queue
and select a process to execute.
Multiple processor scheduling
Approaches to Multiple-Processor Scheduling
Asymmetric Multiprocessing: when all the scheduling decisions and I/O processing are handled
by a single processor which is called the Master Server and the other processors executes only
the user code.
It is simple and reduces the need of data sharing.
Multiple processor scheduling
Key Challenges in Multiprocessor Scheduling
Load Balancing: Distributing threads evenly across processors to avoid idle cores.
Cache Affinity: Minimizing cache misses by keeping threads on the same processor (reusing
cached data).
Synchronization Overhead: Managing shared resources (e.g., locks, memory) without
excessive contention.
Scalability: Ensuring scheduling decisions remain efficient as the number of cores grows.
Multiple processor scheduling
Common Multiprocessor Scheduling Algorithms
a. Gang Scheduling
Groups of related threads (gangs) are scheduled together on multiple processors.
Use case: Parallel applications (e.g., scientific computing).
Example: All threads of a process run simultaneously to avoid waiting.
b. Dedicated Processor Assignment
Each thread is assigned a dedicated CPU for its entire lifetime.
Use case: Real-time systems with strict deadlines.
Drawback: Poor CPU utilization if threads block frequently.
Multiple processor scheduling
c. Dynamic Scheduling
Threads are dynamically assigned to processors based on current load.
Examples:
Load Sharing: Global ready queue shared by all CPUs (e.g., Linux CFS).
Work Stealing: Idle processors "steal" threads from busy ones (e.g., Java ForkJoinPool).
d. Affinity Scheduling
Threads are preferred to run on the same CPU to exploit cache locality.
Types:
Hard Affinity: Thread is pinned to a specific CPU (e.g., for real-time apps).
Soft Affinity: OS tries to reuse the same CPU but can migrate if needed.
Real-Time CPU Scheduling
Systems where the correctness of results depends not only on the logical correctness of computations
but also on the time at which the results are produced.
Hard Real-Time:
Systems where missing a deadline is a critical failure (e.g., controlling an aircraft's flight control
system).
Soft Real-Time:
Systems where occasional missed deadlines are acceptable, though performance degrades (e.g.,
multimedia streaming).
Deadlines:
Specific points in time by which a task must be completed.
Schedulability:
The ability of a scheduling algorithm to ensure all tasks meet their deadlines.
Examples of Real-Time CPU Scheduling
Industrial Control Systems: Controlling machinery, robotics, and other automated systems.
Multimedia Applications: Streaming audio and video, where timing is crucial for smooth playback.
Embedded Systems: In cars, appliances, and other devices where real-time performance is essential.
In essence, real-time CPU scheduling ensures that critical tasks in time-sensitive systems are executed
promptly and efficiently, preventing delays and potential failures.