OS Notes
OS Notes
Detailed Syllabus
Unit 1 : Introduction - Operating system Structure, layered structure, System component,
operating system function, classification of operating systems, batch, interactive, time
sharing, real time system, multi-user system, multi process system, multi processor system,
multi-threaded system, operating system services, renterant kernels, monolithic and micro
kernel systems.
Unit 2 : Concurrent Processes - Process concept, principle of concurrency, producer
consumer problem, mutual exclusion, critical section problem, dekker's solution, Peterson
solution Semaphores, test and set operation, classical problem in concurrency, dining
philosopher problem, sleeping barber problem inter process communication model and
schemes process generation.
Unit 3 : CPU Scheduling - Scheduling concept, performance criteria, process state, process
transition diagram, schedulers, process control block(PCB), process address space, process
identification information, thread and their management, scheduling algorithm multi
processor scheduling, deadlock system model deadlock characterization, prevention,
avoidance and detection recovery from deadlock.
Unit 4 : Memory Management - Basic bare machine, Resident monitor, multi programming with
fixed partition, multi programming with variable partition, protection scheme, paging,
segmentation , paged segmentation, virtual memory concept, demand paging, performance of
demand paging, page replacement algorithm, thrasing, cache memory organization, locality of
reference.
Unit 5 : Management & Disk Scheduling - I/O devices, and I/O subsystem I/O buffering, disk
storage and disc scheduling, RAID, file system, file concept, file organisation and access
mechanism, file directories and file sharing, file system implementation issues file system
protection and security.
Page 1
Page 2
Unit 1
The term "operating system" was first used in the early 1960s to describe the software
that controls the execution of computer programs and manages hardware resources such
as memory and input/output devices.
The term was coined by John W. Tukey in 1958, in his book "An Introduction to
Mathematical Methods of Statistics," where he used it to describe the software that
manages the operation of a computer system.
John W. Tukey
Page 3
What is Operating System
Operating system is a system software.
It act as an intermediary between the computer user and computer hardware.
An operating system (OS) is a type of software that manages computer hardware and
software resources.
Provide a platform on which other application program installed.
Ques : Can we access the hardware without operating system?
Ans : YES, but it is not convenient neither efficient.
OPERATING
SYSTEM
COMPUTER
HARDWARE
CPU Program
I/P data
Input OS Output
Control instruction
User
Program
COMPUTER
Disadvantages
No concept of memory hierarchy.
Very less interactive systems.
Very poor CPU Utilization, speed mismatch between I/O device & CPU.
Every jobs have the different requirement.
Advantages Disadvantages
In Batch OS, Jobs are executed after
Limited memory.
one another, saving time from the
interaction of input and output device
activity like loading, linking and
directly with CPU.
compiling.
Very less buffer size, leads to less
During execution no manual intervention
utilization of CPU
is needed, less human interaction.
Page 5
SPOOLING : Simultaneous Peripheral operations online
Spooling refers to putting data of various I/O jobs in a buffer. This buffer is a special area in
memory or hard disk which is accessible to I/O devices.
COMPUTER
CPU
MAIN
MEMEORY
I/P DEVICE O/P DEVICE
DISK
Input and output devices are relatively slow compared to CPU, as CPU is digital device.
In Spooling, data is stored first on to the disc(digital device) and then CPU interact with
disc via main memory.
Spooling is capable of overlapping input and output operation for one job with CPU
operation of other jobs.
Advantages Disadvantages
No interaction of input output devices In starting spooling was uni
with CPU. programming.
CPU utilisation is more as CPU is busy No concept of context switching
most of the time. CPU sit idle for the second event
OS
P1 SECONDARY
CPU MEMORY
P2
Pn
Advantages Disadvantages
High CPU utilisation. Difficult scheduling of the process.
Less waiting time & response time. Main memory management required.
Load is very high, hence it is very useful. Memory fragmentation.
Multitasking/Time shearing operating system
Multi-tasking is an extension of multi-programming.
There is only a CPU but the context switching between the process so quickly, they give the
illusion that all are executing at the same time.
It improves a better response time and execution of multiple processes together.
Fixed Time Quantum
One processor but Multiple processes, the processor is so fast that it can complete all the
process alone.
Sometimes it is called multitasking with round robin.
Games OS CPU
Youtube
Page 7
Advantages Disadvantages
Increased throughput(processing per unit More complex as too many processor
time). are available.
Increased reliability. Overhead and coupling reduces
Cost saving(as their is only multiple copies throughput.
of CPU, other devices are same. Large main memory.
True parallel processing
Eg. A word processor may have a thread for displaying graphics, another thread for
responding to keystrokes from the user, and a third thread for performing spelling and
grammar checking in the background.
NOTE : Most operating-system kernels are now multi threaded. Several threads operate in the
kernel, and each thread performs a specific task, such as managing devices, managing
memory, or interrupt handling.
Page 8
Multi-threaded Model
There is a relationship exist between the user threads and Kernel thread. There are three
common ways of establishing the relationship between kernel and user thread.
Many to one model
The many-to-one model maps many user-level User thread
threads to one kernel thread. Thread
management is done by the thread library in
user space, so it is efficient;
Drawback : the entire process will block if a
thread makes a blocking system call. Also,
because only one thread can access the kernel
at a time, multiple threads are unable to run in K Kernel thread
parallel on multiprocessors.
Device Drivers
Hardware
Kernel Space
Page 10
Only essential component such as IPC, memory management and scheduling are
included in kernel space.
If any one of the service fails, rest of the OS will work fine.
Small in size, execution is slow as compared to monolithic kernel.
New services can be added easily, and easy to debug.
eg. Mac, Windoows.
Page 11
CPU
SCHEDULING
STS
LTS
Priority
Suspend I/O complete I/O request
resume
MTS
Waiting/Block
Suspend/
Ready
resume
Suspend
Suspend/
Process completed I/O, but Wait
still in suspended
Scheduler
It is a special type of software which handles the process scheduling in various ways.
Long term scheduler
It select the process from the secondary memory and put them into the ready queue of
main memory. It also increase the degree of multi-programming.
Short term scheduler
Short term scheduler is also known as CPU scheduler. It selects one of the Jobs from the
ready queue and dispatch to the CPU for the execution.
Mid term scheduler
If the running state processes needs some IO time for the completion then there is a need to
change its state from running to waiting. It also decrease the degree of multi-programming
Process Control Block
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.
Page 13
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 : These are the CPU register which include accumulators, index registers, stack
pointers, and general-purpose registers, plus any condition-code information.
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.
When a process complete its execution. If a process enter into ready state either
Process leaves CPU voluntarily to perform from new or waiting state and it is high
some I/O operation or to wait for an event . priority process.
FCFS (first comes first serve) SRTF (shortest remaining time first)
SJF (shortest job first) LRTF (longest remaining time first)
LJF (largest job first) Round Robin
HRRN (highest response ratio next) Priority queue.
Multilevel queue.
Page 14
Types of Scheduling Criteria in an Operating System
CPU utilization. We want to keep the CPU as busy as possible. Conceptually, CPU utilisation
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. If the CPU is busy executing processes, then work is being done. One measure
of work is the number of processes that are completed per time unit, called throughput.
Turnaround time. It determines 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.
Waiting time. It affects only the amount of time that a process spends waiting in the ready
queue.
Response time. A process can produce some output fairly early and can continue
computing new results while previous results are being output to the user. It is the time it
take start responding.
Scheduling Algorithms
CPU scheduling deals with the problem of deciding which of the processes in the ready queue
is to be allocated the CPU. There are many different CPU-scheduling
algorithms.
A 3 4 7 7-3=4 4-4=0
B 5 3 13 13 - 5 = 8 8-3=5
C 0 2 2 2-0=2 2-2=0
D 5 1 14 14 - 5 = 9 9-1=8
E 4 3 10 10 - 4 = 6 6-3=3
AVGwt = (5+8+3)/5
Gantt Chart C A E B D = 16/5 = 3.2
0 2 3 7 10 13 14
Q2
Process AT BT CT TAT = CT - AT WT = TAT-BT
P1 0 2 2 2–0=2 2–2=0
P2 3 1 4 4–3=1 1–1=0
P3 5 6 11 11- 5 = 6 6–6=0
AVGwt = 0
Page 15
Q3 Process AT BT CT TAT = CT - AT WT = TAT-BT
P1 0 2 2 2–0=2 2–2=0
P2 1 2 4 4–1=3 3–2=1
P3 5 3 8 8- 5 = 3 3–3=0
P4 6 4 12 12 - 6 = 6 6-4=2
0 2 4 5 8 12
P0 2 4 9 9–2=7 7–4=3
P1 1 2 5 5–1=4 4–2=2
P2 0 3 3 3 -0=3 3–3=0
P3 4 2 12 12 - 4 = 8 8-2=6
P4 3 1 10 10 - 3 = 7 7-1=6
0 3 5 9 10 12
P0 0 3 3 3–0=3 3–3=0
P1 2 2 5 5–2=3 3–2=1
P2 6 4 10 10 - 6 = 4 4–4 =0
P1 0 24 24 24 – 0 = 24 24 – 24 = 0
AVGwt = (0+23+25)/3
P2 1 3 27 27 – 1 = 26 26 – 3 = 23 = 48/3 = 16
P3 2 4 31 31- 2 = 29 29 – 4 = 25
Gantt Chart P1 P2 P3
0 24 27 31
The convoy effect in FCFS scheduling occurs when a long-running CPU-bound process occupies
the CPU for an extended period. As a result, shorter processes that arrive after the long-running
process but are ready to run must wait for the CPU to become available and many shorter
processes are forced to wait for the long-running process to complete. Page 16
Shortest job first(SJF)
In this approach out of all the available processes, CPU is assign to the process which have the
shortest burst time. If their is a tie between the two process, FCFS is used to break it. Shortest
job first can be either preemptive or non-preemptive. Preemption mode of shortest job first is
called shortest remaining time first(SRTF). SRTF is guarantees the minimal waiting time.
P1 3 1 7 7-3=4 4-1=3
P2 1 4 16 16 - 1 = 15 15 - 4 = 11
P3 4 2 9 9- 4 = 5 5- 2 = 3
P4 P1 P3 P5 P2
0 6 7 9 12 16
P1 3 1 7 4-3=1 1-1=0
P4 0 6 5 6 16 - 0 = 16 16- 6 = 10
P5 2 3 12 11 - 2 = 9 9-3=6
P4 P2 P2 P1 P2 P3 P5 P4
0 1 2 3 4 6 8 11 16
Process Arrival
Burst time CT TAT = CT - AT WT = TAT-BT
Id time
P1 0 7 6 19 19 – 0 = 19 19 – 7 = 12
P2 1 5 4 13 13 – 1 = 12 12 – 5 = 7
P3 2 3 2 6 6–2=4 4–3=1
P4 3 1 4 4–3=1 1–1=0
P5 4 2 9 9–4=5 5–2=3
P6 5 1 7 7–5=2 2–1=1
Page 17
PID AT BT CT TAT = CT - AT WT = TAT-BT
P1 0 9 13 13 – 0 = 13 13 – 9 = 4
AvgWT = (4 + 0 + 11) / 3
P2 1 4 5 5–1=4 4–4=0 = 15 / 3 = 5 unit
P3 2 9 22 22- 2 = 20 20 – 9 = 11
Advantages-
SRTF 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.
Priority Scheduling
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 is, the larger the CPU burst, the lower the
priority, and vice versa. Priority scheduling can be either preemptive or non preemptive.
When a process arrives at the ready queue, its priority is compared with the priority of the
currently running process.
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.
A non preemptive priority scheduling algorithm will simply put the new process at the
head of the ready queue.
P1 0 4 2 4 4–0=4 4–4=0
P2 1 3 3 15 15 – 1 = 14 14 – 3 = 11
P3 2 1 4 12 12 – 2 = 10 10 – 1 = 9
P4 3 5 5 9 9–3=6 6–5=1
P5 4 2 5 11 11 – 4 = 7 7–2=5
P1 0 4 3 2 15 15 – 0 = 15 15 – 4 = 11
P2 1 3 2 3 12 12 – 1 = 11 11 – 3 = 8
P3 2 1 4 3 3–2=1 1–1=0
P4 3 5 5 8 8–3=5 5–5=0
P5 4 2 5 10 10 – 4 = 6 6–2=4
P1 0 5 3 1 13 13 – 0 = 13 13 – 5 = 8
P2 1 3 1 12 12 – 1 = 11 11 – 3 = 8
P3 2 1 5 5–2=3 3–1=2
P4 3 2 9 9–3=6 6–2=4
P5 4 3 1 14 14 – 4 = 10 10 – 3 = 7
CT - AT TAT - BT
Page 20
Multilevel feedback queue scheduling algorithm, allows a process to move between queues.
The method used to determine when to upgrade a process to a higher priority queue and
when to demote a process to a lower priority queue.
This algorithm help solve the problem of starvation.
NOTE : Starvation is a problem of resource management where in the OS, the process does
not have resources because it is being used by other processes.
It is a problem when the low-priority process wait for a long duration of time because of high-
priority requests being executed.
DEADLOCK
In a multi programming , 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 waiting state. Sometimes, a waiting process is never again able to change state,
because the resources it has requested are held by other waiting processes. This situation is
called a deadlock.
System model R2
Request : Every process will requested for resources.
Use : If entertained then, process will use the resources. P1 P2
Release : Process must release the resources after use.
P1 P2 P1 P2 R1 R2
R1 R1 R1 R2
R2 R2 R2 R1 P1 P2
If a process request for the in between resources, then it must release all the
resources hold from the resources number in between requested.
NOTE : Prevention is very restrictive approach to handle the deadlock, although it
guarantees that system will never come under dead lock, but it is very expensive.
Page 22
Problem with Prevention
Prevention approach, prevent the deadlock by limiting how requests can be made. The limits
ensure that at least one of the necessary conditions for deadlock cannot occur. Possible side
effects of preventing deadlocks by this method, however, are low device utilization and
reduced system throughput.
Deadlock Avoidance
In Deadlock avoidance, we require additional information about how resources are to be
requested. With this knowledge of the complete sequence of requests and releases for each
process, the system can decide for each request whether or not the process should wait in
order to avoid a possible future deadlock.
In order to avoid the deadlock in runtime, system try to maintain some books like a banker,
whenever someone ask for a loan(resource), it is granted only when the books is allowed.
P2
2 0 0
P4
2 1 1
P5
0 0 2
0 1 0 P1
P3
7 2 5 10 5 7
P0 0 0 1 2 0 0 1 2 1 5 2 0 0 0 0 0 P2
0 0 1 2
P1 1 0 0 0 1 7 5 0 1 5 3 2 0 7 5 0
1 3 5 4 P3
P2 1 3 5 4 2 3 5 6 2 8 8 6 1 0 0 2
0 6 3 2
P4
P3 0 6 3 2 0 6 5 2 2 14 11 8 0 0 2 0
0 0 1 4
P4 0 0 1 4 0 6 5 6 2 14 12 12 0 6 4 2 P1
1 0 0 0
2 9 10 12 3 14 12 12
Safe sequence
A state of the system is called safe if the system can allocate all the resources requested by
all the processes without entering into deadlock. If the system cannot fulfill the request of all
processes then the state of the system is called unsafe.
Available. It indicates the number of available resources of each type.
Max. It defines the maximum number of resources demand of each process.
Allocation. It defines the number of resources of each type currently allocated to each
process.
Need. It indicates the remaining resource need of each process to complete its task.
Page 23
Deadlock Detection
Deadlock detection is a technique used in operating systems to identify and resolve situations
where multiple processes are blocked and unable to continue executing because they are
waiting for each other to release resources. A deadlock can occur when two or more processes
are waiting for resources that are being held by each other, resulting in a circular dependency.
Resource-allocation graph
It is one of the simplest way to represent the state of the system like how the resources are
allocated to the process and how process have been assigned the multiple resources. It can be
used to visualise the resources being used by different processes.
Vertex
Vertex
R1 , R2
Availability = (0 0)
P1 P2
current need (1 1)
Deadlock found
R2
P1 1 0 0 1
P1 P2
P2 0 1 1 0
P3 P3 0 1 0 0
R2
If every resource have only one resource instance in resource allocation graph, then
detection of graph is necessary and sufficient condition for deadlock detection.
If any resource have the multiple copy of the instance in resource allocation graph then
cycle is necessary but not sufficient condition for deadlock.
Page 24
MEMORY MANAGEMENT
Memory management is an important aspect of an operating system that is responsible for
managing the primary memory of a computer system. The main goal of memory management is
to allocate and deallocate memory resources to different processes in a way that maximizes
the utilization of available memory while ensuring that each process has enough memory to
execute its tasks.
Main
CPU memory
Note. Functionalities of any computer depend upon both CPU and memory.
There are several key functions involved in memory management in an operating system,
including:
Memory Allocation: The process of assigning a section of memory to a process or program.
The operating system must keep track of which parts of memory are currently in use and
which are available for allocation.
Memory De-allocation: The process of releasing memory that is no longer required by a
process or program. The operating system must ensure that memory is properly deallocated
to prevent memory leaks or fragmentation.
Memory Protection: The process of preventing one process from accessing the memory of
another process. This is important for security and stability reasons.
Virtual Memory Management: The process of managing the allocation of virtual memory,
which is a technique that allows a computer to use more memory than it physically has
available. Virtual memory management involves the use of paging and swapping to move
data between physical memory and disk.
Criteria for the memory
1. Size 2. Access time 3. Per unit cost
These properties are contradict to each other. We cannot have the less access time with large search
space we can’t have the large memory size with low cost.
Hierarchy of memory
MAIN SECONDARY
CPU
MEMEORY MEMEORY
Explanation : When the program executes, it execute sequentially, e.g if instruction number n is
currently executing, then it is the probability that next instruction will be n+1. So, the next instruction
which is about to execute will be fetched and store into main memory. So, when we access the
memory most of the time we get data into the main memory. This will help to meet all the desired
criteria for memory.
How the process comes from secondary memory to primary memory? What are the policies?
How the address translation occur, as CPU generate logical address, and to access main memory we
need physical address?
Page 25
Memory management technique
Contigous
Contiguous memory allocation is a technique used by operating systems to allocate and
manage memory. When the allocation of the process in main memory in contiguous fashion.
If a new process of size 5KB arrive, it can not be occupied as space is not available in contiguous
fashion, called external fragmentation.
Disadvantages of Contiguous Memory Allocation:
External fragmentation: External fragmentation can occur when the total free memory space
is sufficient to satisfy a memory request, but the available memory is divided into small non-
contiguous blocks that are unusable by the requesting process.
Fixed partitioning (static)
In this technique, the memory is divided into fixed size block, each block may have same
or different size and the process is allocated to the fixed size block.
For every process, the entire memory block will be allocated
10KB
3KB
4KB 2KB 4KB
Suppose the process size 3KB is allocated to fixed size memory block, then 1KB of memory
can not be used again, this is called internal fragmentation Page 26
Variable partitioning (dynamic)
In the variable size partitioning technique, whenever the process is coming into RAM, then only the space
is allocating for the process, it helps to remove internal fragmentation.
10KB
4KB
4KB 6KB
Allocation method in contiguous memory management(variable partitioning)
First fit : Allocate the first hole that is big enough. Searching can start either at the beginning of
the set of holes or at the location where the previous first-fit search ended. We can stop
searching as soon as we find a free hole that is large enough
Best fit: Allocate the smallest hole that is big enough. We must search the entire list, unless
the list is ordered by size. This strategy produces the smallest leftover hole.
Worst fit : Allocate the largest hole. We must search the entire list. This strategy produces the
largest leftover hole, which may be more useful than the smaller leftover hole from a best-fit
approach.
Best fit
25
P2 50
125 300
P1
Worst fit P3
P2
Address Translation
The process of converting the logical address generated by CPU into physical to access the
main memory, is called address translation. CPU generate logical address and to access the
data available in main memory we need physical address, this translation of address is called
address translation.
SM
i0
i1
i2
i3
i4
i5
.
n.99
The relocation register contains the value of the smallest physical address, or we can say
that it holds the base address of the instruction available in main memory.
The limit register contains the range of logical addresses or we can say that it holds the size of
the process.
Page 28