Operating System

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Organization of system:

The major high-level components of computer system are:


 Hardware – which provides computing recourses (CPU, memory, I/O devices)
 Operating system – which manages the use of the hardware among various application programs for
various users and provides the users relatively simple machine to use.
 Application program – defines the way in which system recourses are used to solve computational
problems.
 Users – which includes the machine, people and other program.
Primary purpose of computer system:
The primary purpose of computer system is producing executable programs and executes them.
Operating system:
Mainly there are two vies about an operating system that are:
1. Top-down view
2. Bottom-up view
Top-Down View:
In this view, we see the operating system as a program that acts as intermediatory between the user of the
computer and the computer hardware and makes the computer system convenient to use.
Bottom-Up view:
In this type of view, we see the operating system as resource manager who manages the hardware and
software resources in the computer system.
Types of Operating system:
There are several types of operating system but main types of operating system are:
 Single-user systems
 Batch systems
 Multi-programmed systems
 Time-share systems
 Real-times systems
 Multi-processing systems
Operating system structures:
Like other software operating system code can also be structures in different ways. Following are reg some
common type of structure:
 Simple/Monolistic structure
 Micro-kernels structures
 Layered structure
 Virtual Machines
Process:
A process can be thought of as a program in execution. A process will need certain resources such as CPU,
memory etc to accomplish its tasks.
Or
A process is the program in the execution including the current value of the program counter, registers and
variables. The difference between the program and the process is that the program is the group of instruction
where as process is an active entity and program is the passive entity. When a program is loaded in the
memory it becomes the process, it can be divided four sections stack, heap, text and data.
 Stack – maintains the temporary data structure such as functions, parameters etc.
 Heap – dynamically allocates the memory for the process during the runt time.
 Text – this includes the current activity represented by the
Types of process:
Process can be of two types:
 I/O bound process
 CPU bound process
I/O bound process:
These types of process spend more time doing the I/O than computation and have many short CPU busts.
Such as word processor, text editor etc.
CPU bound process:
These types of process spend more time doing computation and have very long CPU burst.
CPU Burst:
A CPU burst is a period during which a process or program demands and actively utilizes the Central
Processing Unit (CPU) for computation. It's when the CPU executes a sequence of instructions for a specific
task.
Process control Block (PCB):
A process control block (PCB) in Operating system is a data structure that is used by the operating system to
manage the information about the process. It is also known as task control block.
Process scheduling:
Process scheduling refers to the overall process of determining the execution order of processes in a system.
It encompasses the policies, algorithms, and mechanisms used by the scheduler to manage process
execution.
Types of process schedular:
 Long term schedular or job schedular – responsible for bringing the process form the job pool to
the ready queue.
 Short term schedular or CPU schedular – responsible which process should be executed next and
allocates the CPU.
 Mid term schedular – responsible of swapping the processes (it removes the process from the main
memory and at some point, reintroduce the process called swapping)
Interprocess Communication (IPC):
It is a mechanism that allows the allows the process to communicated with each other so that they can
synchronize their actions.
Methods:
Following is some methods for IPC:
 Direct or indirect communication
 Symmetric or asymmetric communication
 Automatic or explicit buffering
 Send by copy or send by reference
 Fixed size or variable size message.
Scheduling:
The scheduler is a component of the operating system responsible for making decisions about which
processes to execute and when to execute them on the CPU. It selects processes from the ready queue (a
queue of processes that are ready to execute) and allocates CPU time to them based on a scheduling
algorithm.
Scheduling Queue:
In a Process Control Block (PCB), the scheduling queue refers to the various queues used by the operating
system scheduler to manage the execution of processes. Some of the queues are as follow:
 Job Queue – when a process enters the system it is kept in this queue, this queue contains all the
process in the system.
 Ready Queue – when a process is residing in the main memory and ready to be execute and are
waiting are kept in this queue. This queue is generally kept as a linked list.
 Device Queue – processes that are waiting for particular I/O devices are kept in this queue.
Thread:
A thread refers to the execution unit in the process which has its own program counter, stack as well as set of
registers.
Types of threads:
User Level thread (ULT) – Is implemented in the user level library, they are not created using the system
calls. Thread switching does not need to call OS and to cause interrupt to Kernel. Kernel doesn’t know about
the user level thread and manages them as if they were single-threaded processes.
Kernel Level Thread (KLT) – Kernel knows and manages the threads. Instead of thread table in each
process, the kernel itself has thread table (a master one) that keeps track of all the threads in the system. In
addition, kernel also maintains the traditional process table to keep track of the processes. OS kernel
provides system call to create and manage threads.
Note: Both of these threads share data and code.
Short term schedular or CPU schedular:
CPU schedular responsible which process should be executed next and allocates the CPU. CPU scheduling
can take place under following circumstance:
1. When a process switches from the running state to the waiting state
2. When a process switches from the running state to the ready state.
3. When a process switches from the waiting state to the ready state
4. When a process terminates
When scheduling takes place only under 1 and 4, we say, scheduling is non-pre-emptive; otherwise, the
scheduling scheme is pre-emptive
Pre-emptive and non-pre-emptive:
Pre-emptive scheduling allows a running process to be interrupted by a high priority process, whereas in
non-pre-emptive scheduling, any new process has to wait until the running process finishes its CPU cycle.
Criteria of CPU Scheduling:
CPU utilization:
The main objective of any CPU scheduling algorithm is to keep the CPU as busy as possible.
Throughput:
A measure of the work done by the CPU is the number of processes being executed and completed per unit
of time.
Turnaround Time
It is the time difference between the time takes for a process to complete and the time arrival time of the
process.
Turn Around Time = Completion Time – Arrival Time.
Waiting Time
A scheduling algorithm does not affect the time required to complete the process once it starts execution. It
only affects the waiting time of a process i.e. time spent by a process waiting in the ready queue.
Waiting Time = Turnaround Time – Burst Time.
Bust Time:
Time required by a process for CPU execution.
Response Time:
The time taken from submission of the process of the request until the first response is produced. This
measure is called response time.
Response Time = CPU Allocation Time (when the CPU was allocated for the first) – Arrival Time
Scheduling Algorithms:
 First-Come-First-Served (FCFS) Scheduling
 Shorted Job First (SJF) Scheduling
 Shortest Remaining Time First (SRTF) Scheduling
 Priority Scheduling
 Round-Robin Scheduling
 Multilevel Queues Scheduling
 Multilevel Feedback Queues Scheduling
 UNIX System V Scheduling
First-Come, First-Served (FCFS) Scheduling:
The process that requests the CPU first (i.e., enters the ready queue first) is allocated the CPU first. The
implementation of an FCFS policy is managed with a FIFO queue. FCFS is a non- pre-emptive scheduling
algorithm.
Shortest-Job-First Scheduling:
When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If two
processes have the same length next CPU burst, FCFS scheduling is used to break the tie. A pre-emptive SJF
algorithm pre-empts the currently executing process, whereas a non-pre-emptive SJF algorithm will allow
the currently running process to finish its CPU burst. Pre-emptive SJF scheduling is sometimes called
shortest-remaining-time-first scheduling
Priority Scheduling:
A priority is associated with each process, and the CPU is allocated to the process with the highest priority
(smallest integer ≡ highest priority). Equal priority processes are scheduled in FCFS order. Priority
scheduling can either be pre-emptive or non-pre-emptive.
Major Problem:
A major problem with priority- scheduling algorithms is indefinite blocking (or starvation). A priority-
scheduling algorithm can leave some low priority processes waiting indefinitely for the CPU which is
known as starvation.
Solution for starvation:
Aging is solution to the problem of indefinite blockage of low-priority processes. It involves gradually
increasing the priority of processes that wait in the system for a long time.
Round-Robin Scheduling:
The round-robin (RR) scheduling algorithm is designed especially for time-sharing systems. It is similar to
FCFS scheduling but pre-emption is added to switch between processes. A small unit of time, called a time
quantum (or time slice) is defined. The performance of RR algorithm depends heavily on the size of the time
quantum. If the time quantum is very large (infinite), the RR policy remains the same as the FCFS policy. If
the time quantum is very small, the RR approach is called the processor sharing.
Multilevel Queue Scheduling:
Processes are easily classified into different groups. For example, a common division is made between
foreground (or interactive) processes and background (or batch) processes. A multilevel queue-scheduling
algorithm partitions the ready queue into several separate queues. Each queue has its own priority and
scheduling algorithm
UNIX System V scheduling algorithm:
UNIX System V scheduling algorithm is essentially a multilevel feedback priority queues algorithm with
round robin within each queue, the quantum being equal to1 second. The priorities are divided into two
groups/bands:
1. Kernel Group
2. User Group
Process Synchronization:
Process synchronization is the task of ensuring that multiple processes can safely share resources without
interfering with each other. It is a critical part of operating system design, as it ensures that data integrity and
resource efficiency are maintained. It aims to resolve the problem of race conditions and other
synchronization issues in a concurrent system.
Main objective:
The main objective of process synchronization is to ensure that multiple processes access shared resources
without interfering with each other and to prevent the possibility of inconsistent data due to concurrent
access. To achieve this, various synchronization techniques such as semaphores, monitors, and critical
sections are used.
Categories of process:
On the basis of synchronization, processes are categorized as one of the following two types:
 Independent Process: The execution of one process does not affect the execution of other processes.
 Cooperative Process: A process that can affect or be affected by other processes executing in the
system.
Race Condition:
When more than one process is executing the same code or accessing the same memory or any shared
variable in that condition there is a possibility that the output or the value of the shared variable is wrong so
for that all the processes doing the race to say that my output is correct this condition known as a race
condition. A race condition is a situation that may occur inside a critical section.
Example:
Initially Shared = 10

Process 1 Process 2

int X = shared int Y = shared

X++ Y--

sleep(1) sleep(1)

shared = X shared = Y

Explanation:
We are assuming the final value of a common variable(shared) after execution of Process P1 and Process P2
is 10 (as Process P1 increment variable (shared=10) by 1 and Process P2 decrement variable (shared=11) by
1 and finally it becomes shared=10). If the order of execution of the process (first P1 -> then P2) then we
will get the value of common variable (shared) =9. If the order of execution of the process (first P2 -> then
P1) then we will get the final value of common variable (shared) =11. Here the (value1 = 9) and (value2=10)
are racing, if we execute these two processes in our computer system then sometime, we will get 9 and
sometime we will get 10 as the final value of a common variable(shared). This phenomenon is called race
condition.
Critical Section Problem
Critical Problem – A critical section is a code segment that can be accessed by only one process at a time.
The critical section contains shared variables that need to be synchronized to maintain the consistency of
data variables.
Critical Section Problem – when a process executes the code that manipulates the shared data, (or
recourses), we say that the process is in critical section problem.
Entry section – only one process is allowed to execute in its critical section (even with multiple processors).
So, each process must first request permission to enter its critical section. The section of code implementing
this request is called the entry section
Remainder section – The remaining code is the remainder section.
Solutions:
There are three kinds of solution to critical section problem:
1. Software based solutions
2. Hardware based solutions
3. Operating system based solution
Criteria for a solution:
A solution to the critical section problem must satisfy the following three requirements:
Mutual Exclusion: If process Pi is executing in its critical section, then no other process can be executing in
their critical section.
Progress: If no process is executing in its critical section and some processes wish to enter their critical
sections, then only those processes that are not executing in their remainder section can participate in the
decision on which will enter its critical section next.
Bounded Waiting There exists a bound on the number of times that other processes are allowed to enter
their critical sections after a process has made a request to enter its critical section and before that request is
granted.
2-Process Solutions to the Critical Section Problem:
This is solution we will be using two process Pi and Pj.
Algorithm 1:
The first approach is to let the processes share a common integer variable turn initialized to 0 or 1. If turn =
= i, then process Pi is allowed to execute in its critical section. Following is the code:
do {
while (turn! =j);
critical section
turn=j;
remainder section}
while (1)
However, it does not satisfy the progress requirement, since it requires strict alternation of processes in the
execution of the critical section.
Algorithm 2:
In algorithm two, the variable turn is replaced with an array Boolean flag [2] whose elements are initialized
too false. If flag is true for a process that indicates that the process is ready to enter its critical section.
do {
flag[i]=true;
while(flag[j]);
critical section
flag[i]=false;
remainder section
}
while (1)
In this solution, the mutual exclusion requirement is satisfied. Unfortunately, the progress condition is not
met; consider the following execution sequence: T0: P0 sets flag [0] = true T1: P1 sets flag [1] = true Now
both the processes are looping forever in their respective while statements
Algorithm 3:
The processes share two variables: Boolean flag [2]; int turn; The Boolean array of ‘flag’ is initialized to
false, whereas ‘turn’ maybe 0 or 1.
do {
flag[i]=true;
turn=j;
while(flag[j] && turn==j);
critical section
flag[i]=false;
remainder section
} while (1)
While the algorithm aims to achieve mutual exclusion, bounded waiting, and progress, it relies on
assumptions that may not hold in all practical systems, particularly regarding the atomicity of operations and
the behaviour of modern processors and memory systems.
The Bakery Algorithm:
(Prerequisite – Critical Section, Process Synchronization, Inter Process Communication)
The Bakery algorithm is one of the simplest known solutions to the mutual exclusion problem for the
general case of N process. Bakery Algorithm is a critical section solution for N processes. The algorithm
preserves the first come first serve property.
Working:
In the Bakery Algorithm, each process is assigned a number (a ticket) in a lexicographical order. Before
entering the critical section, a process receives a ticket number, and the process with the smallest ticket
number enters the critical section. If two processes receive the same ticket number, the process with the
lower process ID is given priority.
Notation – lexicographical order (ticket #, process id #) – Firstly the ticket number is compared. If same
then the process ID is compared next. The numbering scheme always generates numbers in increasing order
of enumeration; i.e., 1, 2, 3, 3, 3, 3, 4, 5, …
Fairness:
The Bakery Algorithm ensures fairness by assigning a unique ticket number to each process based on a
lexicographical order. This ensures that processes are served in the order they arrive, which guarantees that
all processes will eventually enter the critical section.
if i < j
Pi is served first;
else
Pj is served first.
Advantages of Bakery Algorithm:
 Fairness
 Easy to implement
 No deadlock
 No starvation
Disadvatnges:
 Not scalable
 High time complexity
 Busy waiting
 Memory Overhead
Hardware based solution for critical section problem:
Process Synchronization problems occur when two processes running concurrently share the same data or
same variable. The value of that variable may not be updated correctly before its being used by a second
process. Such a condition is known as Race Around Condition. There is a software as well as hardware
solutions to this problem. Hardware instructions in many operating systems help in the effective solution of
critical section problems.
There are three algorithms in the hardware approach of solving Process Synchronization problem:
1. Test and Set
2. Swap
3. Unlock and Lock
Test and Set:
There is a shared variable called lock that starts as false. The first process checks the lock. It finds the lock is
false and sets it to true. This process enters the critical section (a special part of the code). While the first
process is in the critical section. Any other process that checks the lock finds it true. These processes have to
wait. When the first process leaves the critical section. It sets the lock back to false. Now, any waiting
process can check the lock. The first one to find it false sets it to true and enters the critical section.
Pseudo code:
boolean lock;
Boolean TestAndSet (boolean &target) {
boolean rv = target;
target = true;
return rv;
}
while (1){
while (TestAndSet(lock));
critical section
lock = false;
remainder section
}
Swap:
Swap algorithm is a lot like the TestAndSet algorithm. Instead of directly setting lock to true in the swap
function, key is set to true and then swapped with lock.
The first process sets its key to true and swaps it with lock. Initially, lock is false, so after the swap. lock
becomes true (because key was true). The process's key becomes false (because lock was false). The process
checks its key, finds it false, so it breaks out of the while loop and enters the critical section. Any other
process sets its key to true and swaps it with lock. Since lock is now true (set by the first process), after the
swap. lock remains true (since the new process's key was true). The process's key becomes true (because
lock was true). The process checks its key, finds it true, and stays in the while loop, waiting. When the first
process leaves the critical section, it sets lock back to false. Any waiting process sets its key to true again
and swaps it with lock. Now, lock is false (since the first process reset it), so after the swap. lock becomes
true (because the new process's key was true). The process's key becomes false (because lock was false). The
process checks its key, finds it false, and enters the critical section.
Only one process in the critical section at a time. If the critical section is empty, a process can enter. Not
guaranteed. There's no order, so some processes might wait longer.
Pseudo Code:
boolean lock;
Individual key;
void swap (boolean &a, boolean &b) {
boolean temp = a;
a = b;
b = temp;
}
while (1) {
key = true;
while(key)
swap(lock,key);
critical section
lock = false;
remainder section
}
Unlock and Lock:
Uses Test and Set but adds a waiting array for each process and maintains a queue to manage the order of
processes. Uses TestAndSet to check and set lock. It also checks the waiting array and the queue to see if
other processes are waiting. Added to the queue if they can't enter the critical section immediately. the queue
and sets waiting for the next process to false, allowing it to enter. If no processes are waiting, it sets lock to
false.
Only one process in the critical section at a time. If the critical section is empty, a process can enter.
Guaranteed. Processes are handled in a queue, ensuring each gets a turn.
Pseudo code:
boolean lock;
Individual key;
Individual waiting[i];
while(1){
waiting[i] = true;
key = true;
while(waiting[i] && key)
key = TestAndSet(lock);
waiting[i] = false;
critical section
j = (i+1) % n;
while(j != i && !waiting[j])
j = (j+1) % n;
if(j == i)
lock = false;
else
waiting[j] = false;
remainder section
}
Semaphore:
Semaphore is an integer variable which is used in mutual exclusive manner by various concurrent
cooperative process in order to achieve synchronization. A semaphore is a synchronization tool used to
control access to a shared resource in concurrent programming. It is an integer variable that can only be
changed using two atomic operations: wait (P) and signal (V).
Types of semaphore:
There are two kinds of semaphore:
 Binary semaphore – whose integer value cannot be > 1; can be simpler to implement.
 Counting semaphore – whose integer value can range over an unrestricted integer domain.
Basic Operation:
Wait (P): This operation decreases the semaphore value by 1 if the semaphore value is positive. If the value
is 0 or negative, the process waits.
Pseudo code:
wait(S) {
while (S <= 0) ;
// no operation (busy wait)
S--;
}
Signal (V):
This operation increases the semaphore value by 1, possibly allowing a waiting process to proceed.
Pseudo code:
signal(S)
{
S++;
}
Mutex:
For multiple processes to safely share a resource, they use a semaphore called mutex (short for mutual
exclusion). The semaphore mutex is initialized to 1. Each process performs the following steps:
 Wait on mutex: This ensures that only one process can enter the critical section at a time.
 Critical Section: The part of the code where the shared resource is accessed.
 Signal mutex: This allows other processes to enter the critical section.
Pseudo code:
Do
{
wait(mutex);
// Critical section
signal(mutex);
// Remainder section
}
while (1);
Spinlock:
During implementing the mutex, if a process can't enter the critical section, it keeps checking (busy waiting).
This wastes CPU cycles, especially in multiprogramming systems where many processes share a single
CPU. This type of semaphore is also called a spinlock. Because the process spins while waiting for the lock.
Improving Semaphores to Avoid Busy Waiting:
To avoid busy waiting, semaphores can be modified to make processes block themselves when they can't
proceed.
Working:
Blocking: The process suspends itself and is added to the waiting queue.
Wakeup: A blocked process is moved to the ready queue when another process signals the semaphore.
Wait Operation:
 Decrease the semaphore value.
 If the value is negative, add the process to a waiting queue and block it.
Pseudo code:
void wait(semaphore S) {
S.value--;
if (S.value < 0) {
add this process to S.L;
block(); }
}
Signal Operation:
 Increase the semaphore value.
 If the value is still negative, remove a process from the waiting queue and wake it up.
Pseudo code:
void signal (semaphore S) {
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
}
Semaphore Structure
A semaphore can be represented as follows:
typedef struct {
int value;
struct process *L; // list of waiting processes
} semaphore;
value: The current value of the semaphore.
L: A list of processes waiting on the semaphore.
Disadvantages of semaphore:
While semaphores help with mutual exclusion and progress, issues like deadlock, starvation, and violation
of mutual exclusion can still occur.
Deadlocks
A deadlock occurs when a set of processes are waiting for each other to release resources, causing them to
be stuck forever.
Starvation
Starvation occurs when a process waits indefinitely because other processes keep getting priority for the
required resources.
Violation of Mutual Exclusion
If semaphores are misused, mutual exclusion can be violated, allowing multiple processes to enter the
critical section simultaneously.
Classical Problems of synchronization:
There are three classical problems of synchronization:
 Bounded buffer problem
 Reader and writers’ problem
 Dining Philosophers problem
Bounded buffer problem:
The bounded-buffer problem involves a fixed-size buffer that producers and consumers share. Producers add
items to the buffer, and consumers remove items from it.
Semaphores Used:
 mutex: Ensures mutual exclusion when accessing the buffer (initialized to 1).
 empty: Counts the number of empty slots in the buffer (initialized to the buffer size, n).
 full: Counts the number of full slots in the buffer (initialized to 0).
Reader and writers’ problem:
This problem involves a shared data object that can be read by multiple readers simultaneously, but only
written by one writer at a time.
Semaphores Used:
mutex: Ensures mutual exclusion when updating the count of readers (initialized to 1).
wrt: Ensures mutual exclusion for writers or a writer and readers (initialized to 1).
readcount: Keeps track of the number of readers currently accessing the data (initialized to 0).
Dining Philosophers problem:
In this problem, five philosophers sit at a table with five chopsticks. Philosophers need two chopsticks to eat
and release them after eating. The challenge is to prevent deadlock and starvation.
Semaphores Used:
chopstick [5]: Represents the chopsticks (each initialized to 1).
Solution:
This simple solution can lead to deadlock if all philosophers pick up their left chopstick simultaneously.
Solutions to prevent deadlock include:
 Allowing only four philosophers to sit at the table.
 Ensuring a philosopher picks up both chopsticks only if both are available.
 Using an asymmetric solution where odd philosophers pick up the left chopstick first, and even
philosophers pick up the right chopstick first.

High level synchronization construct:


High-level synchronization constructs like critical regions and monitors provide more structured and error-
resistant ways to handle synchronization compared to low-level semaphores. They help prevent issues like
deadlocks and ensure proper mutual exclusion, making concurrent programming safer and easier to manage.
Monitors:
A monitor is a high-level synchronization construct that encapsulates shared data and operations that can
access this data. Only one process can be active within a monitor at a time.
Monitor Structure:
monitor monitor_name
{
// Shared variable declarations
// Procedure bodies
procedure body P1(..) { ... }
procedure body P2(..) { ... }
... procedure body Pn(..) { ... }
{ initialization code }
}
Condition Variables:
They allow processes to wait for certain conditions to be true and also known as condition construct.
Operations:
x.wait(): The process is suspended until another process calls x.signal().
x.signal(): Resumes exactly one suspended process (if any).
Deadlock:
Deadlock occurs in a system of processes where each process is waiting for a resource that is held by
another process in the same set.
Condition for deadlock:
For a deadlock to happen, four specific conditions must all be true at the same time:
 Mutual Exclusion: At least one resource must be held in a way that only one process can use it at a
time. If another process wants the resource, it must wait until the first process releases it.
 Hold and Wait: A process must hold at least one resource and be waiting to acquire additional
resources held by other processes.
 No Preemption: Resources cannot be forcibly taken away from a process; they can only be
voluntarily released by the process holding them.
 Circular Wait: There must be a circular chain of two or more processes, each of which is waiting for
a resource that's held by the next process in the chain. This creates a cycle of dependencies.
Safe state:
A safe state ensures the system can manage resources in a way that avoids deadlock, allowing processes to
complete their tasks and release resources back to the system effectively.
Deadlock Detection:
The resource allocation graph algorithm is a method used to detect and manage deadlocks in a system
where processes compete for resources. It visualizes the allocation of resources and their relationships with
processes, helping to identify potential deadlocks and decide how to resolve them.
Graph Representation:
Vertices (Nodes):
Represent processes and resource types.
Process vertices: Each process that is currently running or waiting for resources.
Resource vertices: Each type of resource in the system.
Edges (Directed and Undirected):
Represent relationships between processes and resources.
 Directed edge from process to resource: Process is currently holding that resource.
 Directed edge from resource to process: Process is waiting for that resource.
 Undirected edge between process and resource: Indicates a request for allocation.
Cycle Detection:
A cycle in the resource allocation graph indicates a potential deadlock. If there exists a cycle where each
process in the cycle is waiting for a resource held by another process in the cycle, then a deadlock is
possible.
Deadlock Avoidance:
Deadlock avoidance refers to strategies and algorithms used by operating systems to prevent the occurrence
of deadlock. The goal is to ensure that the system remains in a safe state, where processes can continue to
execute and complete their tasks without getting stuck in a deadlock situation.
Solution for the deadlock avoidance:
Following is some algorithms for deadlock avoidance.
 Banker’s algorithm
 Safety algorithm
 Safe sequence
Banker’s Algorithm:
Banker's algorithm is a deadlock avoidance algorithm that was developed by Edsger Dijkstra. It works by
dynamically allocating resources to processes in a way that avoids deadlock.
Safe sequence:
A safe sequence is a sequence of processes that can be executed in such a way that each process can acquire
its necessary resources, execute, and release resources, ultimately allowing all processes to complete
successfully without deadlock. The Banker's algorithm finds a safe sequence by simulating the allocation of
resources and checking if there exists a sequence where each process can execute to completion.
Available:
Available [j] = = k
Max:
Max [i, j] = = k
Allocation:
Allocation [i, j] = = k
Need:
Need [i, j] = = k
Need = Max – Allocation
Safety Algorithm:
The safety algorithm checks whether a system is in a safe state before allocating resources. If a safe
sequence exists, the system proceeds with resource allocation; otherwise, it waits until resources are
available.
Memory management:
Computers have different types of memory, ranging from super-fast but expensive registers inside the CPU,
to slower but larger main memory (RAM), and even slower but vast storage like hard drives and internet
storage. Each level of memory serves a different purpose: registers for immediate data access, cache for fast
access to frequently used data, RAM for general purpose storage, and secondary storage for long-term data
retention.
Memory management:
The goal of memory management is to efficiently use and organize memory resources. It involves tasks like
tracking which parts of memory are in use or free, deciding when and how much memory to allocate to
programs, and handling the swapping of programs in and out of RAM to manage limited resources.
Addressing in memory:
Logical address: Address generated by the CPU during program execution. Programs use logical addresses
to access instructions and data. These addresses are typically represented as pointers or offsets within the
program's address space.
Physical Address: A physical address refers to the actual location in physical memory (RAM) where data is
stored. Every byte of data in memory has a unique physical address. These addresses are managed by the
memory management unit (MMU) in conjunction with the operating system. When a program references a
logical address, the MMU translates it into a corresponding physical address to retrieve or store data.
Virtual Address: Often used interchangeably with logical address; represents an abstraction of memory
space used by processes. It represents a location in the logical address space of a process. The term "virtual
address" is often used interchangeably with "logical address" to emphasize its abstraction from physical
memory.
MMU: To translate logical addresses to physical addresses, a hardware component called the Memory
Management Unit (MMU) is used.
Addressing binding:
Compile Time: Addresses are fixed during compilation if the program's memory location is known
beforehand.
Load Time: Addresses are determined during loading if the program can be loaded into any available
memory location.
Execution Time: Addresses are dynamically mapped during runtime if the program can move between
memory locations while running (requires special hardware support).
Dynamic loading:
Only loads parts of a program when they are needed, rather than loading the entire program into memory
upfront. It helps to save memory and reduce load times initially. But it is not supported by Operating system.
Dynamic Linking:
A shared library can be defined as an object module that during run time or load time can be loaded at any
random location in memory and can be linked with the program in memory. The process of linking and
loading the dynamic library is known as dynamic linking It is the process that links dependant libraries or
routines at runtime. It is supported by operating system.
Memory management technique:
Some of the memory management techniques are as follow:
 Overlays
 Swapping
 Contiguous allocation (MFT)
 Variable allocation (MVT)
 Paging
a) Overlays:
Overlays are used to run programs larger than the available memory by dividing them into smaller parts
(overlays).
Working:
Only the necessary parts of the program (like code segments or data) are loaded into memory at any given
time.
Example:
Imagine a two-pass compiler that has a total size of 200k but the available memory is only 150k. The first
pass (70k) is loaded into memory first. When it finishes, it's replaced by the second pass (80k), using the
same memory space.
Challenges:
Not all programs can be easily divided into overlays. Requires manual programming effort to manage the
loading and unloading of overlays.
b) Swapping:
Swapping allows processes to be temporarily moved out of main memory to a backing store (like a disk) and
then brought back when needed.
Working:
In a multiprogramming environment, when a process is swapped out, another process can take its place in
memory. This helps manage limited memory resources efficiently.
Example:
When a process finishes its time quantum in a round-robin scheduling system, it might be swapped out to
allow another process with higher priority to run.

Disadvantages:
Swapping involves transferring data between memory and the backing store, which can be time-consuming.
Care must be taken to handle situations like pending I/O operations that could conflict with swapped-out
processes.
c)Contiguous Memory Allocation (MFT Multiprogramming with fixed tasks):
Memory is divided into fixed partitions, with each process requiring a single contiguous block of memory.
Working:
Processes are loaded into partitions of predetermined sizes. Each process gets a base and limit register pair
to define its memory boundaries.
Example:
BM's OS/MFT (Multiprogramming with Fixed Tasks) allocates fixed partitions for processes. Once a
partition is free, a new process can be loaded into it.
Advantages and Disadvantages:
 Advantage: Simple to implement and understand.
 Disadvantage: Can lead to internal fragmentation (wasted space within partitions) and lacks
flexibility for dynamically changing memory needs.
d) Multiprogramming with Variable Tasks (MVT):
An evolution of MFT where memory partitions are variable in size, adapting to the needs of different
processes.
Working:
Processes can occupy memory regions of varying sizes, reducing internal fragmentation compared to MFT.
Example:
Instead of fixed partitions, MVT adjusts memory allocations dynamically, accommodating processes of
different sizes more efficiently.
Disadvantages:
MVT eliminates internal fragmentation but can lead to external fragmentation (unused memory scattered in
small chunks). Compaction (shuffling processes in memory) can reduce external fragmentation but at the
cost of slower performance.
e) Paging:
Paging is a way for computers to manage memory that allows a program's memory to be stored in non-
contiguous blocks. Instead of keeping all parts of a program together in memory, paging divides memory
into small fixed-sized blocks called "pages". These pages are typically 4 KB or 8 KB each.
Why use paging?
 Avoids Fragmentation: In traditional memory management, fitting different-sized chunks of
memory (like programs or data) into available spaces can leave unused gaps (fragmentation). Paging
helps avoid this issue.
 Efficient Use of Memory: By breaking memory into fixed-sized pages, the computer can allocate
and manage memory more efficiently.
 Protection: Each process gets its own page table, which maps virtual addresses (used by programs)
to physical addresses (actual locations in memory). This separation helps protect one program's
memory from being accessed by another.
Working:
Pages and Frames: Physical memory is divided into fixed-sized blocks called frames, and logical memory
(how programs see memory) is divided into pages of the same size (process is divided into same size pages).
Page Table: Each process has a page table that keeps track of which pages of its memory are stored in which
frames of physical memory.
Address Translation: When a program accesses memory, the CPU translates its virtual address (from the
program) into a physical address (where data is actually stored). This translation is done using the page
table.
Advantages:
 Simplicity: Simplifies memory management by treating memory as small fixed-sized units.
 Flexibility: Allows different programs to use memory efficiently without worrying about the
physical location of each piece of data.
 Security: Ensures one program cannot access another's memory directly, improving system security.
Example:
Imagine a book where each page is a fixed size (like 4 KB). Instead of putting the entire book in one place
on a shelf (contiguous memory), you store each page wherever there's space. A table tells you which page is
where, so you can find any page quickly when needed.
Implementation of Page Table in Different Places:
Page Table in CPU Registers
Suitable for small processes and large page sizes.
Advantage: The time to access memory (effective memory access time) is nearly the same as just accessing
the memory directly. This is fast.
Example: The PDP-11 computer.
2. Page Table in Main Memory
 Uses a special register called the Page Table Base Register (PTBR) to point to the page table in main
memory.
 Disadvantage: The effective memory access time becomes double the normal memory access time,
which is too slow for efficient program execution.
3. Page Table in Translation Look-Aside Buffer (TLB)
 TLB: A small, fast cache that holds a few entries from the page table.
 Entries: Each entry is a key-value pair (page number, frame number).
 When the CPU generates a logical address (page number, offset):
o TLB Hit: If the page number is found in the TLB, the corresponding frame number is
quickly retrieved to form the physical address.
o TLB Miss: If the page number is not found in the TLB, the page table in main memory is
checked, and the TLB is updated with this new (page number, frame number) pair.
 Context Switch: When the CPU switches between different processes, the TLB is cleared (flushed)
and reloaded with entries relevant to the new process.
Paging Hardware with TLB
 Hit: Accessing the memory involves the time to check the TLB and then the memory.
 Miss: Checking the TLB, then accessing the memory twice (once to get the frame number from the
page table, and once to access the actual data).
Performance of Paging
 Effective Memory Access Time: How fast memory can be accessed on average, considering hits
and misses in the TLB.
o Hit Ratio (HR): The percentage of times the needed page number is found in the TLB.
o Miss Ratio (MR): The percentage of times the needed page number is not found in the TLB
(MR = 1 - HR).
Formula:

You might also like