Operating System Notes For Interview Preparation
Operating System Notes For Interview Preparation
Interview Preparation
These are notes for the Operating System which are specially designed for
interview preparation. In this, you will get to know every aspect of the
operating system from its introduction to security.
These notes are divided into 5 modules along with a lot of sub-topics.
INDEX
2 Process Management
3 Memory Management
4 Storage Management
Module 1: Introduction to Operating System
Introduction
Now, we will understand the difference between the generation and types
of operating systems:
Generation refers to the ‘evolution’ in any particular thing but ‘types’ refers
to the different types of that thing in a particular generation.
Advantages:
1. It is easy to manage large work repeatedly in batch systems.
2. Multiple users can share the batch systems.
3. The idle time for the batch system is very less.
Disadvantages:
1. It is sometimes costly.
2. The computer operator should be well known with batch systems.
3. These systems are hard to debug.
4. The other jobs will have to wait for an unknown time if any job fails.
Advantages:
1. Utilization of the CPU is increased because the CPU never
goes to an idle state.
2. Less response time which further increases performance.
3. It helps to improve the turnaround time for short time jobs.
Disadvantages:
1. Every management is required like memory, file kind of
management.
2. Scheduling is required.
3. It is a highly complex task.
Advantages:
1. Each task gets an equal opportunity.
2. Fewer chances of duplication.
3. CPU idle time can be reduced.
4. Idle time reduces means the CPU always processes
multiple tasks that means every task gets a response for
further processing i.e. response time increases.
Disadvantages:
1. A specially appointed person always needs to take care
of security.
2. Data communication issues.
3. Reliability issues.
Advantages:
1. Maximum utilization of devices and systems.
2. The time assigned for shifting tasks in these systems is very
less.
3. These types of systems are error free.
Disadvantages:
1. The system’s resources are expensive.
2. The algorithms in these operating systems are very complex
because it is necessary to make this system error-free so this
error freeness makes it complex.
3. These systems are programmed in such a way that they don’t
prioritize their threads because these systems are very less
prone to task switching.
Examples:
● Scientific experiments, Medical Imaging Systems, Weapons
systems
Advantages:
1. Millions of applications are available for users.
2. Android is open source.
3. Sharing things is very easy as compared to other OS.
Disadvantages:
1. This OS requires more battery for processing of various tasks in
the background which makes the smartphone's battery drained
after a regular interval of time.
2. Virus protection is not better.
3. Low specifications devices run slowly.
Examples: ANDROID
Services provided by Operating System:
● An operating system provides an environment for the execution of the
programs.
● It provides certain services to programs and users of those programs.
● Here are the OS Services:
○ User Interface
○ Program Execution
○ I/O Operations
○ File System Manipulation
○ Communications
○ Error Detection
○ Resource Allocation
○ Accounting
○ Protection and Security
1. User Interface:
In simple words, operating systems have a user interface that is
commonly of two types: CLI (Command Line Interface) and GUI
(Graphical User Interface).
Command Line Interface:
● On systems with multiple command interpreters to choose from,
the interpreters are known as Shells.
○ For example, on UNIX and Linux systems, a user can
choose among several different shells, including the
Bourne Shell, C-Shell, Bourne-Again Shell, Korn
Shell, and others. Third-party shells and free user-written
shells are also available.
● It needs commands(input/actions) for further work/task.
● Rather than having a User-friendly nature, it has developer
friendly nature. (Context for saying, it is not user friendly
because it needs a knowledge of programming and commands.
● All the commands are written by the developer in the
environment, commonly known as ‘Terminal’.
Graphical User Interface:
● It is made by the developers having in mind that all types of
users will be able to use computers whether they know about
programming or commands related stuff or not.
● It has a very beautiful environment for implementing the daily
tasks of users.
● It is the best example of Abstraction.
5. Communications:
In simple words, there usually occurs when one program needs to
exchange information with another process. So, here the operating
system implements the methods called “Shared Message” or
“Message Passing” in which information is exchanged between
processes by OS.
8. Accounting:
Simply, in order to improve anything, take out the statistics and
accounting data. It is very eminent which tracks which task or user
uses the most resources of the computer. Through this, computer
scientists are able to improve the services.
Example 1: The user has to copy the file from one place to another place.
So, the following hierarchy is followed.
● User copy file from source place to destination place.
● User executes the process and here a software interrupt is called.
● This software interrupt tells the OS that one more high-priority task
comes for switching to kernel mode and then System Call Interface
(SCI) comes into picture.
● Then SCI switches the user mode to kernel mode.
● After this, it goes towards the required management like process
management, and file management for the implementation part and
completes the required sequence with the help of kernel and
hardware like:
● And as soon as the process completes, this kernel mode
switches to user mode and you will see the file at the
destination place.
● File Management
○ create file, delete file
○ open, close
○ read, write, reposition
○ get file attributes, set file attributes
● Device Management
○ request device, release device
○ read, write, reposition
○ get device attributes, set device attributes
○ logically attach or detach devices
● Information Maintenance
○ get time or date, set time or date
○ get system data, set system data
○ get process, file or device attributes
○ set process, file or device attributes
● Communications
○ create, delete communication connection
○ send, receive messages
○ transfer status information
○ attach or detach remote devices
2. Now, the CPU loads the BIOS by fetching the sequence of
instructions into the RAM from the BIOS(which is originally stored in
ROM).
3. Now, BIOS runs a test (which is commonly known as POST- Power
On Self Test) and initializes the hardware.
- Basically, start all the attached devices to make sure that the computer is working
properly or not (basically, it looks for RAM), if not working properly then produces
a beep sound.
4. Now, after performing the test, it will load some settings from a
memory area(which is backed by CMOS - Complementary Metal
Oxide Semiconductor).
5. After this, it will begin the search for MBR (Master Boot Record)
which exists on the zeroth index of a disk.
6. After reaching MBR, BIOS loads the program called Boot loader.
- Boot loader is also known as a Boot device where actually the operating system
boot’s instructions are present.
7. And after loading the boot loader, finally it loads the actual operating
system.
Above is the process followed by the CPU box and its components after
switching on the power button.
Micro Kernel: In this, it has different address spaces allotted for different
facilities like management, scheduling, services, etc.
Sr. No. Monolithic Kernel Micro Kernel
1. It is larger in size It is smaller in size
2. It is faster. It is slower.
3. Less reliable More reliable
4. Communication is faster because of Communication is not
the centralisation of the modules. performing well
because of context
switching, it makes
overhead between
user mode and kernel.
Thread:
● A Thread is a unit of execution within a process.
● A process can have many threads either it has one or more.
Context switching:
● A Context Switching is defined as a term in which a CPU is switched
towards another process that requires performing a state save of a
current process and a state restore of a different process.
● These are highly dependent on the system hardware support.
● Interrupts cause the OS to change a CPU from its current task and to
run a kernel routine, these interrupts type of operations happens
frequently.
● In simple words, when an interrupt occurs, the system needs to save
the current context of the process running on the CPU so that it can
restore that context when its processing is done (means suspending
that process and resuming it after some time).
● This context is saved in the PCB of the process.
● Context Switching speed varies from machine to machine, depending
on the memory speed, and the number of registers( simply depends
on hardware). The typical speed is a few milliseconds.
Zombie Process:
● It is defined as a process whose execution is completed but it still has
an entry in a process table.
Note: Only need to remember is that a zombie process is a process whose
execution is completed, all the resources are deallocated by the operating
system but still have an entry in a process table.
● To remove the zombie process from the process table, the parent
process needs to call the wait() so that the parent process reads the
exit() status of the child process then the zombie process removes
from the process table.
Orphan Process:
● An orphan process is a process whose parent process no longer
exists.
● In a zombie process, the parent process needs to call wait and reads
the exit() status of the zombie process so that it removes from the
process table but here in the case of the orphan process, the parent
did not call wait(), instead it got terminated or completes its execution
and goes out of scope and the child process becomes an orphan
process.
Process Synchronization
Race Condition
● A situation where several processes access and manipulate the
shared data concurrently.
● This can result in the incorrect value of shared data as various
processes try to change it.
● The final value of that shared data depends upon which process
finishes last.
● Let’s understand it with an example:
○ Consider we have a function that performs the following task:
Read (shared) //Line 1
shared= shared + 1 //Line 2
○ Let’s say we have two processes P1 and P2 that will run
parallelly and run the above function. Initially consider the value
of ‘shared’ = 5 (i.e. the variable that is shared by both
processes).
○ So, let’s say P1 starts executing line 1, so it will read the value
of ‘shared’ which comes out to be 5.
○ Since P2 is also running concurrently it also reads the value of
‘shared’ which also comes out to be 5.
○ After this P1 executes line 2, which makes the value of ‘shared’
= ‘shared’ +1 i.e. 5+1= 6, ‘shared’ becomes 6.
○ Now, P2 continues execution, as P2 also read ‘shared’ as 5,
after executing line 2 ‘shared’ becomes 6.
○ So, finally the value of the shared data that is ‘shared’ here
becomes 6. But actually, it should have been 7 as two
processes executed the function, which would have led to a +1
increment in the value of ‘shared’ two times.
○ Since here both the processes are competing to modify the
value of ‘shared’ concurrently is leading to a race condition.
Peterson's solution
● It is a classic software-based solution to the critical-section problem.
● Peterson’s solution is restricted to two processes that alternate
execution between their critical sections and remainder sections.
● There are two shared variables between the processes
○ int turn: this variable indicates whose turn it is to enter its
critical section.
■ If turn == i, then Process Pi is allowed to execute in its
critical section.
○ boolean flag[2]: flag array is used to indicate if a process is
ready to enter its critical section.
■ If flag[i] == true, this indicates that Pi is ready to enter its
critical section.
● Let’s understand how Peterson’s solution preserves all three
conditions:
○ Mutual exclusion: We can see that using the while( flag[j] &&
turn == j) statement when one process is executing in its critical
section no other process is allowed to enter the critical section
until the other process has exited from the critical section.
Hence, mutual exclusion is preserved.
○ Progress: When the processes are not executing in the
remainder section they are allowed to make changes in the turn
variable, hence they can decide which process will enter the
critical section, and this decision depends on the process that
finally updated the value of turn. Hence progress is fulfilled.
○ Bounded-waiting:
■ Let’s say Pi and Pj try to enter the critical section so both
will send the request, Pi will set turn = j, and Pj will set turn
= i, and finally Pi will execute the critical section after it
exits Pj will now start executing the critical section.
■ Hence both processes get a chance to execute critical
sections without waiting indefinitely.
Synchronization in hardware
● Software-based solutions such as Peterson’s are not guaranteed to
work on modern computer systems.
● Operating systems make use of locks to protect the critical section.
● For a single processor system we can solve the critical section
problem using a test and set lock function.
● Here we can prevent interrupts from occurring while a shared variable
is being modified and this will ensure that the current sequence of
instructions is executed in order without preemption.
Mutex locks
● One of the simplest hardware-based solutions to critical section
problem is the mutex lock.
● Mutex is a short form of mutual exclusion.
● There are two functions namely acquire() and release() .
● The acquire()function acquires the lock before entering the critical
section.
● The release() function releases the lock when it exits the critical
section.
● A boolean variable available indicates the availability of the lock.
● If the lock is available a call to acquire() succeeds, and the lock is
then marked unavailable.
Disadvantages of Mutex locks:
● Spinlocks are created i.e. when a process is in its critical
section and another process tries to enter the critical section,
then it must loop continuously in the call to acquire() and this
situation is called busy waiting.
● Busy waiting leads to wastage of CPU time and lowers
productivity.
Semaphore
● A semaphore S is an integer variable used to solve the critical section
problem.
● It can be accessed only through two standard atomic operations:
wait() and signal().
● Wait() operation also referred as P(from the Dutch proberen, ‘to test’).
● Signal() also referred to as V (from verhogen, ‘to increment’).
● Following are the definitions of wait() and signal() function:
○ Binary Semaphore:
■ The value can range only between 0 and 1.
■ Binary semaphores behave similarly to mutex locks.
■ When the value of the semaphore is 1 then wait() works
i.e. then only the process can enter the critical section.
■ signal() only works when the semaphore is 0.
Disadvantages of Semaphores:
● Semaphore also suffers the problem of busy waiting.
○ This problem can be resolved if the process is blocked after it
goes to the wait() operation and detects that the semaphore
value is less than 0.
○ Blocking the process will stop it from looping and the process
will be pushed into the waiting queue.
○ A wakeup() operation is used to restart the process, which
changes its state from waiting to ready.
● The implementation of a semaphore with a waiting queue can lead to
some processes waiting indefinitely because they are dependent on
some event that can be only caused by any of the waiting processes
only, this situation leads to a deadlock (discussed further in the
module).
○ The deadlock situation also leads to the starvation of
processes.
● Semaphore can lead to priority inversion, i.e. low priority processes
execute the critical section before the high priority processes.
Fig: Situation of Dining-Philosophers
CPU Scheduler
● The operating system needs to select a process from the ready
queue to be executed whenever the CPU is idle.
● The selection process is carried out by the short-term scheduler, or
CPU scheduler.
● It selects a process from the memory that is ready to execute and
allocates the CPU to that process.
Dispatcher
The dispatcher is the module that gives control of the CPU to the process
selected by the short-term scheduler. This function involves the following:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that program
Scheduling Criteria:
We must consider the properties of the various algorithms while choosing
which algorithm to use in a particular situation.
Many criteria have been suggested for comparing CPU-scheduling
algorithms. The criteria include the following:
● Waiting time: It is the sum of the periods spent waiting in the ready
queue. CPU-scheduling algorithm does not affect the amount of time
during which a process executes or does I/O, it can only minimize this
waiting time.
● Response Time: Turn Around time may not be the best criterion for
interactive systems. A process can produce results earlier and
continue to calculate new results while previous results are passed
on to the user. Thus, another metric is the time elapsed from sending
a request to giving the first response. This measure is called
response time. The goal of CPU is to minimize this time.
● Non-Preemptive Scheduling:
○ In non-preemptive scheduling, a new process is executed only
after the current process has completed its execution.
○ The process retains CPU resources until it terminates or is
switched to waiting state.
○ Once the process completes its execution, the CPU is allocated
to the next process in the ready queue.
● Preemptive Scheduling:
○ Preemptive scheduling is used when a process switches from
running state to ready state or from waiting state to ready state.
○ In simple words, in preemptive scheduling, CPU can switch
from a process that is not yet terminated to the next process in
the queue due to various reasons.
○ CPU resources are allocated to a process only for a limited
period of time, after which those resources are taken over and
assigned to another process.
○ If the process was yet to complete its execution, it remains in
the ready queue, till it gets a chance to execute once again.
Let us discuss different CPU-scheduling algorithms:
P1 0 3
P2 2 2
P3 4 1
1) At time = 0,
P1 arrives and starts executing.
2) At time = 2,
P2 arrives and is kept in the queue because P1 is still executing.
3) At time = 3,
P1 finishes execution. Since P2 is waiting in the queue, its execution
will start now. Currently, no process in the queue.
4) At time = 4,
P3 arrives and is kept in the queue since P2 is executing.
5) At time = 5,
P2 finishes execution. Since P3 is waiting in the queue it will be
executed now.
After the processes 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:
Now let us calculate the Average waiting time for the above example:
Advantages of FCFS:
● It is the simplest scheduling algorithm and easy to implement.
● Works on a first come first serve basis.
Disadvantages of FCFS:
● Average waiting time under FCFS policy is high.
● It is a non-preemptive algorithm, once the CPU is allocated to a
process, that process retains the CPU until it terminates or requests
I/O.
● FCFS algorithm is not suitable for time-sharing systems.
● It suffers from the Convoy effect.
○ A phenomenon in which some smaller processes wait for one
big process to execute first.
○ This effect results in lower CPU and device utilization than
might be possible if the shorter processes were allowed to go
first.
2) Shortest Job First Scheduling
● In this scheduling algorithm, the process present in the ready
queue with the shortest burst time is selected next for
execution.
● In simple words, the selection criteria is the process's burst
time.
● It is a non-preemptive scheduling algorithm.
● If the next CPU bursts of two processes are the same, FCFS
scheduling is used to break the tie.
● Preemptive SJF is known as Shortest Remaining Time First
scheduling, which we will discuss in the upcoming parts.
P1 0 3
P2 2 1
P3 0 2
P4 4 4
1) At time = 0,
Both P1 and P3 arrive, as per SJF scheduling, P3 will be selected
since it has a smaller burst time, and P1 will be kept in the ready
queue.
2) At time = 2,
P3 finishes execution. P2 arrives and starts executing since the burst
time of P2 is smaller than P3, which is currently in the ready queue.
3) At time = 3,
P2 finishes execution. Since P1 is waiting in the queue, its execution
will start now. Currently, no process in the queue.
4) At time = 4,
P4 arrives and is kept in the queue since P1 is executing.
5) At time = 6,
P1 finishes execution. Since P4 is waiting in the queue it will be
executed now.
Now let us calculate the Average waiting time for the above example:
P1 4 0 5
P2 3 1 4
P3 2 2 2
P4 1 4 1
1) At time = 0,
P1 arrives and starts executing since there is no other process in the
queue.
2) At time = 1,
P2 arrives. P1 is yet to finish, but the priority of P2 is higher than that
of P1, it will take over the CPU and start executing. So, P1 is pushed
to the ready queue.
3) At time = 2,
P3 arrives and preempts the process P2 since it has a higher priority.
Since P2 is yet to complete, it will be pushed to the ready queue.
4) At time = 4,
P3 completes its execution. P4 arrives and starts executing.
5) At time = 5,
P4 finishes execution. Since P2 and P1 are waiting in the queue, P2
will be selected for execution since it has a higher priority and it will
execute till it completes.
6) At time = 6,
Only P1 is left in the queue, so it will continue its execution.
Now let us calculate the Average waiting time for the above example:
Process Priority Arrival Time Burst Time Completion Turn Around Waiting
(ms) (ms) Time (ms) Time (ms) Time (ms)
P1 4 0 5 12 12 7
P2 3 1 4 8 7 3
P3 2 2 2 4 2 0
P4 1 4 1 5 1 0
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around time - Burst Time
P1 0 4
P2 2 2
P3 1 3
P4 4 1
2) At time = 1,
P3 arrives and is pushed to the ready queue, since P1 is executing.
3) At time = 2,
P2 arrives and is pushed to the ready queue. At this point, P1 has run
for the time slice of 2 units, now we need to preempt it, since it has
not completed its execution yet it will be pushed to the ready queue.
And P3 will start executing now.
4) At time = 4,
P4 arrives and is pushed to the ready queue. P3 has run for a period
of 2 units and yet has not completed its execution, so it moves to the
ready queue. Since the first process waiting in the ready queue is P2
now, it will be assigned to the CPU now.
5) At time = 6,
P2 had a burst time of 2 units so it finishes execution. P1 (remaining
burst time = 2 units) is executed next.
6) At time = 8,
P1 completes its execution. P4 waiting in the queue continues its
execution.
7) At time = 9,
P4 executes for 1 unit time and terminates (since it had a remaining
burst time of 1 unit). P3 waiting in the queue is assigned to the CPU
and it executes for 1 unit time and terminates (since it had a
remaining burst time of 1 unit).
Process Arrival Time Burst Time Completion Turn Around Waiting Time
(ms) (ms) Time (ms) Time (ms) (ms)
P1 0 4 8 8 4
P2 2 2 6 4 2
P3 1 3 10 9 6
P4 4 1 9 5 4
P1 0 4
P2 1 5
P3 2 1
P4 3 2
1) At time = 0,
P1 arrives and starts executing since there is no other process in the
queue.
2) At time = 1,
P2 arrives and is pushed to the ready queue, since at this point in
time, the remaining burst time of P1 is 3 units while that of P2 is 5
units.
3) At time = 2,
P3 arrives and preempts process P1 since the burst time of P3 is 1
unit which is lesser than the remaining burst time of P1 i.e. 2 units.
So P1 is pushed to the queue.
4) At time = 3,
P3 terminates. P4 arrives and starts executing since it has the
smallest burst time compared to other processes in the queue.
5) At time = 4,
P4 still has the lowest remaining burst time so it will continue
executing.
6) At time = 5,
P4 completes its execution. P1 waiting in the queue continues its
execution since it has a lesser remaining burst time compared to
other processes.
7) At time = 7,
P1 terminates. Only P2 is there in the queue, so it will continue its
execution till time = 12.
Process Arrival Time Burst Time Completion Turn Around Waiting Time
(ms) (ms) Time (ms) Time (ms) (ms)
P1 0 4 7 7 3
P2 1 5 12 11 6
P3 2 1 3 1 0
P4 3 2 5 2 0
Let us consider that Queue 1 and Queue 2 follow Round Robin scheduling
and Queue 3 follows FCFS.
● A process entering the ready queue is put in Queue 1. A process in
Queue 1 is given a time quantum of 4 ms. If it does not finish within
this time, it is moved to the tail of Queue 2.
● If Queue 1 is empty, the process at the head of Queue 2 is given a
quantum of 16 ms. If it does not complete, it is preempted and is put
into Queue 3.
● Processes in Queue 3 are run on an FCFS basis but are run only
when Queues 1 and 2 are empty.
● A process having burst time less than or equal to 4ms is given the
highest priority (because they are in Queue 1).
● Processes with burst time greater than 4ms but less than 20ms are
also served quickly.
● Long processes automatically sink to Queue 3 and are served in
FCFS order.
Ch-6
Deadlocks
What is a deadlock?
● Deadlock is a situation in which some processes are blocked
because a process is holding a resource and also requesting some
resource that is already acquired by some other process.
1) Mutual exclusion: It means that resources are non-sharable i.e. only
one process at a time can use the resource. Any other process can
access that resource only when it is released by the previous
process.
2) Hold and wait: A process must be holding at least one resource and
waiting to acquire other resources that are currently held by other
processes.
3) No preemption: Resources cannot be preempted i.e. a resource can
only be taken from a process when it voluntarily releases it.
4) Circular wait:
○ A set of processes waiting for each other in a circular form.
○ Let's say we have the following set {P0, P1, ..., Pn} of waiting
processes that exist such that P0 is waiting for a resource held
by P1, P1 is waiting for a resource held by P2, similarly Pn−1 is
waiting for a resource held by Pn, and Pn is waiting for a
resource held by P0.
○ This leads to a circular dependency and the processes are
never able to execute.
Resource Allocation Graph (RAG)
● Deadlocks can be explained more properly using a graph called a
resource-allocation graph.
● This graph consists of a set of vertices V and a set of edges E.
Where the set of vertices V is divided into two types: PP = {P1, P2,
..., Pn}, the set consisting of all the active processes in the system,
and R = {R1, R2, ..., Rm}, the set consisting of all resource types in
the system.
● A directed edge from process Pi to resource Rj is denoted by Pi → Rj,
which means that process Pi has requested an instance of resource
type Rj and is currently waiting for that resource. This edge is called a
request edge.
● A directed edge from resource Rj to process Pi is denoted by Rj→ Pi, it
signifies that an instance of resource type Rj has been allocated to
process Pi. This edge is called an assignment edge.
● The system can have single instance resources like CPU and
multiple instance resources like registers. In RAG, we show the
multiple instance resources with multiple dots i.e. the number of
instances of that resource, for example, R3 in the above diagram has
two instances.
● In the above diagram, there are three types of resources and three
processes. Process P1 is requesting an instance of resource type R1.
Process P2 is holding an instance of resources R1 and R3 and
waiting for an instance of Resource R2. Process P3 is holding an
instance of resources R2 and R3.
● As per the definition of the resource-allocation graph, if the graph
contains no cycles, then no process in the system is deadlocked. If
the graph contains a cycle, then a deadlock may exist.
● No Preemption
○ To violate no-preemption we can use the following
approach. If a process is holding some resources and
requesting some new resources which cannot be
immediately allocated to it, then all the resources
currently held by the process are preempted.
○ A process will restart only when it is allocated the new
resources and recovers its preempted resources as well.
○ This approach is not efficient since it cannot be applied to
resources whose state cannot be saved and restored
later, for example, printers and tape drivers.
● Circular Wait
○ To ensure that the circular wait condition doesn’t hold we
need to assign an order to all the resource types and
each process can request resources only in increasing
order.
○ Let us understand with an example,
Resource type Ordering
Printer 2
Tape driver 3
Register 4
Process A B C
P1 2 1 4
P2 3 2 2
P3 5 3 3
P4 4 2 3
Then we have another table that shows the number of resources
currently allocated to each process:
Process A B C
P1 1 0 0
P2 2 1 2
P3 3 0 1
P4 2 1 0
The following table gives the total number of resources present on the
system:
A B C
10 6 5
The following table gives the currently available resources (that is, Total
resources - Resources currently allocated to each process):
A B C
2 4 2
Process A B C
P1 1 1 4
Process A B C
P2 1 1 0
P3 2 3 2
P4 2 1 3
○ From the above tables we can infer that the resources quantity
of A, B, and C available are 2, 4, and 2 respectively.
○ If we scan through the last table that contains the remaining
need we can see that the remaining need of process P1(A, B, C
→ 1, 1, 4) cannot be fulfilled as available resources are (A, B,
C → 2, 4, 2), so we check for P2 and we see that need of P2
can be fulfilled, so we allocate the remaining resources to P2.
○ After P2 completes execution now the available resources will
be: currently available resources (A, B, C → 2, 4, 2) +
resources released by P2 i.e. the resources initially allocated to
P2 (A, B, C → 2, 1, 2).
So now we have remaining resources= (A, B, C → 4, 5, 4).
○ In a similar way now we will scan the remaining table, the
remaining need of P3 is (A, B, C → 2, 3, 2) and the resources
we currently have are (A, B, C → 4, 5, 4). So, P3 can execute.
○ After P3 executes we are now left with the following resources:
Currently available (A, B, C → 4, 5, 4) + resources released by
P3(A, B, C →3, 0, 1) = (A, B, C →7, 5, 5).
○ Check for P4 next, need of P4 is (A, B, C →2, 1, 3) and we
currently have (A, B, C →7, 5, 5). So P4 can execute next.
○ Once P4 terminates we are left with these resources we are left
with: Currently available (A, B, C → 7, 5, 5) + resources
released by P3(A, B, C →2, 1, 0) = (A, B, C →9, 6, 5).
○ Since the above resources are sufficient to fulfill P1’s need (A,
B, C →1, 1, 4), P1 executes next.
○ At last we are now left with the following resources:
Currently available (A, B, C → 9, 6, 5) + resources released by P1(A,
B, C →1, 0, 0) = (A, B, C →10, 6, 5), which are actually equal to the
total number of resources our system had.
○ So, the safe sequence we get is (P2, P3, P4, P1). If we execute
the process in the following manner then deadlock will not
occur.
○ There can be multiple safe sequences for a set of processes.
● Resource-Allocation-Graph Algorithm
○ We discussed RAG above in-depth and similarly by
creating Resource Allocation Graph we can detect a
deadlock.
○ We can check for a safe state using a cycle-detection
algorithm.
■ If there is no cycle in RAG, then the allocation of
resources will leave the system in a safe state.
■ If a cycle is found, then the allocation will put the
system in an unsafe state.
■ A cycle indicates that the system is in an unsafe
state.
● Resource Preemption
Here we preempt the resources of a process by giving the
resources to other processes until the deadlock cycle breaks.
In this case, these three issues need to be addressed:
Now, before diving into much, kindly try to understand what the OS has to
manage in memory; basically OS deals with memory address space so that
it can manage the task accordingly in address spaces.
Swapping:
● As the name suggests, it does swapping of processes, it is that
simple.
● Now, carefully observe the following figure:
● Definition: A process can be swapped temporarily out of memory to
a backing store and then brought back into memory to continue
execution.
● What is happening basically here is that,
Let’s suppose process P1 wants I/O so in that set of time interval OS
did the process of swapping, it swaps out the P1 and take it into the
secondary memory and with the help of the dispatcher next process
P2 which is in the ready queue comes for execution and when P1
comes back then the P2 swaps out and P1 swaps in.
● If you remember the previous concept or whenever you hear about
that a process goes here and there then you must remember an
important concept i.e. Context Switching.
● So, note that context switch time in the case of swapping is fairly
high.
Free Space:
● Free space is created in two situations, when the size of the process
is less than the size of partitions then there is a free space leftover
another case is when a process executes and immediately goes out
from the memory then after leaving that process leaves free space in
memory.
● Till this point, you fairly have an idea of free space.
● OS manages the free space with the help of a free list (which itself is
a type of linked list).
Fragmentation:
● It is defined as the process in which when the process is loaded and
removed from memory, it creates a small free space( commonly
known as Holes).
● To achieve a degree of multiprogramming, we must reduce the
number of holes.
● There are two types of fragmentation which if you understand first
then you can easily understand and apply in further topics as well:
Paging:
● Paging is the memory management technique that is used under the
non-contiguous memory allocation.
● This offers the physical address space of a process to be
non-contiguous.
● It avoids external fragmentation and it needs a process called
compaction.
● It is implemented through the cooperation between the operating
system and the computer hardware.
● Paging involves:
○ Breaking of physical memory into fixed-sized blocks called
frames; and
○ Breaking of logical memory into blocks of the same size called
pages.
● Page Size = Frame Size (Always)
● The logical address is generated by the CPU commonly represented
in bits while the logical address space is a set of all logical addresses
commonly represented in bytes or words.
● The Physical address actually exists on a memory unit commonly
represented in bits while the physical address space is a set of all
physical addresses corresponding to logical addresses commonly
represented in bytes or words.
● The Logical address generated by the CPU is divided into:
○ Page Number (p): Number of bits required to represent the
pages in Logical Address Space or Page Number.
○ Page Offset (d): Number of bits required to represent a
particular word in a page or page size of Logical Address Space
or word number of a page or page offset.
● The Physical Address available in the memory unit is divided into:
○ Frame Number (f): Number of bits required to represent the
frame of Physical Address Space or Frame number.
○ Frame offset (d): Number of bits required to represent a
particular word in a frame or frame size of Physical Address
Space or word number of a frame or frame offset.
● The mapping of logical to physical address is done by the memory
management unit (MMU).
● MMU is a hardware and the mapping process called Paging.
● Remember:
○ 1K = 210, 1M = 220, 1G = 230 (okay!)
○ If Logical Address is in bits then Logical Address Space is in
words.
■ To convert logical/physical address to logical/physical
address space, take the logical/physical address to the
power of 2 like Logical/Physical Address = k bits then
Logical/Physical Address Space = 2k words or bytes.
■ To convert logical/physical address space to
logical/physical address, take the logical/physical address
space with (log.2) like Logical/Physical Address = 2k words
then Logical/Physical Address = log2 2k = k (log22) = k.
● Let us consider an example:
○ Logical Address = 11 bits then Logical Address Space = 211 =
210 * 2 = 2K bytes. (210 = 1K).
○ Physical Address = 12 bits then Physical Address Space = 212 =
210 * 22 = 4K bytes. (210 = 1K).
○ Page Size = Frame Size = 1K.
● Now look into the following figure:
Dedicated registers are present and used to implement the page table in
hardware. However, using a register for the page table is only useful if the
page table is tiny. So, if in any case, the page table is large, then we use
TLB (Translation Look-Aside Buffer), a particular device that is fast
hardware that acts as a cache.
● The TLB is a high-speed, associative memory.
● It is used to speed up the paging process.
● It has a key-value pair.
● When the memory is accessed, the whole TLB process and if any key
is found then the value associated with it is returned and if not found
then it looks in the Page Table and stores that in TLB for future use
and simultaneously goes to the physical address.
● If the page number is found in TLB called TLB hit and if not found
called TLB Miss.
● TLB also stores ASIDs (Address Space Identifier) in each TLB entry.
An ASID uniquely identifies each process and is used to provide
address-space protection for that process.
Important to Note:
● Every process has a different page table because the logical address
space which is mapped on physical address space is different.
● Page Table is stored in memory.
● TLB is used to speed up the paging process.
● TLB will have multiple entries and even it can have entries of different
processes. To handle this, we can use ASIDs.
Advantages of Paging:
1. Paging reduces external fragmentation.
2. It is easy to implement and assumed as an efficient memory
management technique.
3. Due to the equal size of the pages and frames, swapping becomes
very easy.
Disadvantages of Paging:
1. It suffers from internal fragmentation.
2. It is slow, and it adds overhead due to too many memory references.
3. Page Table requires extra memory space, so may not be good for a
system having small size RAM.
Segmentation:
● Segmentation is defined as a memory management technique.
● A process is divided into chunks that are not necessarily all of the
same sizes called Segments.
● Segmentation gives the user’s view of the process means here the
user’s view is mapped to physical memory.
● Here we do variable partitioning of logical address space while on the
other hand in paging we do fixed partitioning of address space.
● Like in Paging, we have a page table, in segmentation, we have a
segment table.
● Segment Table stores the information about all such segments of a
process.
● Segment Table has two entries i.e. Segment Base and Segment
Limit which means it is a two-dimensional table mapped into
one-dimensional physical address.
○ Segment Base: It contains the starting physical address where
the segments reside in memory.
○ Segment Limit: It specifies the length of the segment.
● Logical Address generated by the CPU is divided into:
○ Segment Number (s): Number of bits required to represent the
segment.
○ Segment Offset (d): Number of bits required to represent the
size of the segment.
● MMU translates the two-dimensional logical address to
one-dimensional physical address.
Advantages of Segmentation:
● No internal fragmentation.
● Segment Table consumes less space as compared to page table in
paging.
● No need of Compaction in case of Segmentation while Paging needs
compaction.
Disadvantages of Segmentation:
● External Fragmentation.
● As the size of the segment is different it is not good at the time of
swapping.
Virtual Memory:
● Definition: Virtual Memory is a technique that allows the execution of
processes that are not completely in memory.
● It frees programmers from the concerns of memory storage
limitations.
● It is not easy to implement.
● It allows processes to share files easily and to implement the shared
memory.
● It is an efficient mechanism for process creation.
● It is a technique that allows the execution of processes that are not
completely in the memory.
● It provides an illusion to the user that they have a very big main
memory and you know what you will find interesting that this is done
with the help of treating the part of secondary memory as the main
memory and this part of secondary memory, commonly known as the
Swap Space.
● You can remember it as: RAM + Swap Area === Virtual Memory.
● Sometimes, the size of programs can be larger than the size of
physical memory.
● As we know, it is required that instructions must be in physical
memory to be executed. You must be wondering that, in many cases,
the entire memory is not needed at the same time. So, we want the
ability to execute a program that is only half in memory that would
give many benefits.
○ The size of the program would no longer be constrained by the
size of available physical memory.
○ As, more programs would be able to take memory for their
execution means the degree of multiprogramming increases.
○ The concept of virtual memory benefits the user and the
system.
○ Programmers take much more advantage of this scheme as
they do not bother about the memory limitations.
● The concept of Swapping helps you to understand more about this
topic.
● Basically, the phenomenon swaps out the process from the main
memory to secondary memory and swaps in the process from
secondary memory to main memory for further execution and
vice-versa, all this comes under the task called Swapping.
● The area of secondary memory which is used for swapping is called
Swap Space.
● But, here the process of swap in-and-out works with pages (page
means a division of process).
Demand Paging:
● It is a popular method of Virtual Memory Management.
● In simple terms, Demand Paging refers to a phenomenon of keeping
all the pages of a process in the secondary memory and loading
one-by-one as the demand of any particular page occurs.
● Page Fault refers to a situation when any page is not in main
memory and a demand has to be made in order for delivery of that
page from secondary memory to main memory for execution.
● And, when demand or page fault is made, that page is made
available to main memory with the help of a process called Swapping.
● Actually to identify which pages of a process will be replaced, various
Page Replacement Algorithms will be used.
● Remember, Lazy Swapper is used to swap the page of a process
and it makes the page available to the main memory as soon as the
demand or page fault occurs.
● Note: Here, we use the term, Swapping in the context of a page of a
process, not a whole process.
Thrashing:
● Thrashing is defined as a situation when the system is giving its
precious time for servicing the page fault(means in swapping) rather
than giving its time for the execution of pages.
● You know better that the process of Swapping is a costly process in
terms of time.
● In simple terms, Thrashing is a high paging activity that means the
system gives its time for generating the page fault again and again.
● A system is in Thrashing when it spends more time servicing the
page faults than executing the page of process.
● Kindly, carefully observe the following graph:
Causes of Thrashing:
1. Initially, there is low CPU utilization, so the system tries to increase
the degree of multiprogramming so that it gives better performance.
2. Now, we have more processes than the available number of frames
in memory. Allocation of a limited amount of frames to each process.
3. When any higher priority process arrives in memory, and if the frame
is not freely available at that time then the other process that
occupied the frame which resides in the frame will move to secondary
memory and this free frame is now allocated to a newly arrived higher
priority process.
4. In simple terms, as memory is full then the process starts to spend a
lot of time for the required pages to be swapped in; again, CPU
utilization becomes low because more of the processes are waiting
for pages.