0% found this document useful (0 votes)
2 views

notes (2)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

notes (2)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

Unit- II

Concurrent Process

Process Concept- A process is a sequential program in execution.It represents the basic unit
of execution in a computer system. Each process has its own memory space, program code, data,
and system resources. When a program is executed, the operating system creates a process for it
to run in.
Processes are managed by the operating system's kernel, which schedules them for execution
on the CPU, allocates resources to them, and provides mechanisms for communication and
synchronization between processes.

Principle of concurrency-

Concurrency refers to the ability of a system to handle multiple tasks simultaneously. The
principles of concurrency aim to ensure that multiple tasks can make progress efficiently and
correctly, especially in systems where there are multiple processes or threads executing
concurrently. Concurrency can be implemented and is commonly used even on single processing
units. In a single processing unit system, concurrency can still be achieved through techniques
such as time-sharing or interleaved execution, where the CPU switches between executing
different tasks rapidly, giving the illusion of simultaneous execution.

Concurrency Control: This principle involves managing access to shared resources in a


way that prevents conflicts and ensures consistency. Techniques such as locking, transactions,
and synchronization primitives (e.g., mutexes, semaphores) are used to coordinate access to
shared data and resources among concurrent processes or threads.

Producer- Consumer Problem

Producer processes produce data items that consumer processes consume later. A buffer is used
between the producer and consumer processes. The buffer size may be fixed or variable. The
producer portion of the application generates data and stores it in the buffer, while the consumer
reads data from the buffer. The producer cannot deposit its data if the buffer is full, similarly, a
consumer cannot retrieve any data if the buffer is empty. In the producer-consumer problem, the
challenge is to ensure that producers and consumers can work concurrently without interfering
with each other, avoiding issues such as data corruption, race conditions, and deadlock.
Ex-
//producer
int count = 0;
void producer(void)

{
int itemP;
while(1)
{
Produce_item(item P)
while(count == n);
buffer[in=] item P;
in=(in + 1)mod n
count = count + 1;
}
}
//consumer
void consumer(void)
{
int itemC;
while (1)
{
while (count == 0);
itemC = buffer[out];
out = (out + 1) % n;
count = count - 1;
Consume_item(itemC);
}
}

Key aspects of the producer-consumer problem include:

Shared Buffer: There is a shared, finite-size buffer or queue where items produced by producers
are stored for consumption by consumers. The buffer allows producers to enqueue items and
consumers to dequeue items.

Producer Operations: Producers produce items and add them to the shared buffer. If the buffer
is full, producers may need to wait until space becomes available.

Consumer Operations: Consumers remove items from the shared buffer and process them. If
the buffer is empty, consumers may need to wait until items become available.
Synchronization: Producers and consumers need to synchronize their access to the shared buffer
to avoid issues such as race conditions (where the outcome depends on the timing of
uncontrollable events) and buffer overflow/underflow.

Critical Section Problem

The critical section problem is a fundamental challenge in concurrent programming and


operating systems. It refers to the situation where multiple processes or threads share a common
resource, such as memory, files, or hardware devices, and they need to access this resource in a
way that avoids conflicts and maintains consistency.

The critical section problem can be summarized as follows:

1.​ Mutual Exclusion: Only one process can execute in its critical section at a time.If any
other process requires the critical section, they must wait until it is free.
2.​ Progress: If no process is executing in its critical section,then it should not stop any other
process from accessing it. In other words,any process can enter a critical section if it's
free.
3.​ Bounded Waiting: We should be able to predict the waiting time for every process to get
into the critical section. The process must not be endlessly waiting for getting into the
critical section.

Difference between process and program

Program Process
A program is a set of instructions written in a A process is an instance of a program that is
programming language that performs a being executed by the operating system.
specific task when executed.

Programs serve as a set of instructions to be Processes are responsible for executing the
executed by a computer system. instructions provided by the program,
managing system resources, and interacting
with other processes

It's a static entity Processes are dynamic entities.

Programs are stored on disk or in memory Processes are created and managed by the
operating system during runtime.
Difference between busy wait and blocking wait

Busy wait Blocking wait

Busy wait can lead to high CPU utilization Blocking wait is more efficient and allows the
and inefficiency CPU to be utilized by other processes.

It consumes CPU cycles continuously, even It doesn't consume CPU cycles while waiting
when there is no work to be done, hence the for the condition to become true.
term "busy" wait.

Typically used in situations where the wait Commonly used in situations where the wait
time is expected to be very short, and time is expected to be longer, such as waiting
relinquishing the CPU and context switching for I/O operations to complete.
overhead is not desirable.

Mutual Exclusion- Mutual exclusion ensures that only one process or thread can access a
shared resource at a time. This principle prevents concurrent access that could lead to data
corruption or inconsistent behavior. Mutexes and locks are commonly used to implement mutual
exclusion.Mutual exclusion is a fundamental concept in concurrent programming and operating
systems, ensuring that only one process or thread accesses a shared resource at any given time.
This prevents conflicts and data corruption that can occur when multiple processes attempt to
access the same resource simultaneously. Mutual exclusion is typically achieved using
synchronization mechanisms such as locks, semaphores, or mutexes.

The conditions for achieving mutual exclusion are as follows:

1.​ Mutual exclusion mechanisms should ensure that processes do not remain indefinitely
blocked from accessing the shared resource. If a process holding the resource is
interrupted or delayed, other processes should not be prevented from accessing the
resource indefinitely.
2.​ Deadlock occurs when two or more processes are waiting indefinitely for each other to
release resources, resulting in a deadlock situation where no progress can be made. To
achieve mutual exclusion effectively, mechanisms should be designed to prevent
deadlock.
Dekker’s Algorithm
It is the first known algorithm that solves the mutual exclusion problem in concurrent
programming. It is used in process queuing and allows two different threads to share the same
single-user resources without conflict by using shared memory for communication.
Dekker's algorithm will allow only a single process resource if two processes are trying to use it
at the same time. It succeeds in preventing the conflict by enforcing mutual exclusion, meaning
that one process may use the resource at a time and will wait for another process. is using it. This
is achieved with the use of two " flags" and a "token".

Working
1.​ Turn-taking: Each process takes turns going into the critical section. They do this by
using a flag to indicate when they want to go in.
2.​ Respectful Waiting: If one process wants to go in but sees that the other has already set
its flag, it waits politely until the other process is done.
3.​ Alternating Flags: The tricky part is making sure both processes don't try to go in at the
same time. They use a clever trick with flags to handle this. Each process sets its own
flag when it wants to go in, but before doing so, it checks if the other process wants to go
in too. If it does, the process that went in first waits until the other is done before trying
again.
4.​ Clearing the Way: After finishing their work in the critical section, a process clears its
flag, indicating that it's done and the other process can go in if it wants.

Peterson’s Solution - Peterson's solution is a classic algorithm used for achieving mutual
exclusion in concurrent programming. It's named after Gary L. Peterson, who introduced it in
1981. The algorithm is primarily designed for two processes (or threads) sharing a critical
section, ensuring that they do not execute that section simultaneously, thus avoiding conflicts and
maintaining data integrity.

Peterson's solution typically works:


1.​ Shared Variables: There are typically two shared variables:
●​ Flags: Each process has a flag associated with it. The flag indicates whether the
process is interested in entering the critical section.
●​ Turn: This variable indicates whose turn it is to enter the critical section.
2.​ Initialization: Initially, both flags are set to indicate that neither process is interested in
entering the critical section. The initial value of the turn variable can be arbitrary or
predefined.
3.​ Entering the Critical Section:
●​ A process indicates its interest in entering the critical section by setting its flag to
true.
●​ The process then sets the turn variable to indicate that it's the other process's turn.
●​ The process checks if the other process is also interested in entering the critical
section and if it's their turn. If it is, the process yields, waiting for its turn.
4.​ Leaving the Critical Section:
●​ After completing its work in the critical section, the process resets its flag,
indicating that it's no longer interested in entering the critical section.
●​ This action allows the other process to enter the critical section.
5.​ Turn Switching:
●​ If both processes are interested in entering the critical section, but it's not their
turn, they yield control to each other, ensuring that one of them eventually gets
the chance to enter.

Semaphores -It's a variable used to solve the critical section problem and to solve the critical
section problem and to achieve process synchronization in the multiprocessing environment.
The two most common kinds of semaphores are counting semaphores and binary
semaphores.Counting semaphores can take non-negative integer values and binary semaphores
can take 0 or 1.
Semaphores are synchronization primitives used in concurrent programming to control access to
shared resources by multiple processes or threads. They are integer variables that are accessed
via two standard atomic operations: wait (P) and signal (V).

Advantages of Semaphores:

1.​ Resource Synchronization: Semaphores allow for the orderly access of shared resources,
preventing race conditions and ensuring data integrity.
2.​ Flexibility: They can be used to solve a variety of synchronization problems, such as
producer-consumer, readers-writers, and dining philosophers.
3.​ Efficiency: Semaphores are typically efficient and lightweight, as they rely on
hardware-supported atomic operations.
4.​ Scalability: They can scale to handle large numbers of concurrent processes or threads
without significantly impacting performance.

Disadvantages of Semaphores:

1.​ Complexity: Improper use of semaphores can lead to subtle bugs, such as deadlocks and
livelocks, which can be challenging to debug.
2.​ Overhead: In certain situations, the use of semaphores can introduce overhead due to
context switching and synchronization costs.
3.​ Potential for Priority Inversion: In preemptive scheduling environments, semaphores can
lead to priority inversion, where a low-priority task holds a semaphore required by a
high-priority task, delaying its execution.
4.​ Lack of Enforcement: Semaphores provide mechanisms for synchronization but do not
enforce proper usage. It's up to the programmer to ensure correct implementation and
usage.

Reader Writer Problem

The reader writer problem relates to an object such as a file that is shared between multiple
processes. Some of these processes are readers, they only want to read data from objects and
some of the processes are writers, they want to write into the object.

The reader-writer problem is used to manage synchronization so that there are no problems with
the object data. If two readers access the object at the same time there is no problem. However if
the writers or a reader and writer may access the can access at same time, may be a problem.

To solve this problem, a writer should get exclusive access to an object, when a writer is
accessing the object, no reader or writer may access it. However multiple readers can access the
object at the same time.

1.​ Readers: Multiple readers can access the shared resource simultaneously without any
problem. They don't modify the resource, so allowing concurrent access by multiple
readers is generally safe.
2.​ Writers: Writers, on the other hand, must have exclusive access to the shared resource.
When a writer wants to modify the resource, no other readers or writers should be
allowed to access it.

Dining Philosophers problem-

The Dining Philosophers problem is another classic synchronization problem.The problem is


framed around a group of philosophers sitting around a dining table with a bowl of noodles in
front of each of them. There are also forks placed between each pair of adjacent philosophers.
Philosophers spend their time thinking and eating noodles. However, to eat, they need two forks
- the one to their left and the one to their right.
Here are the main constraints of the problem:

1.​ Each philosopher must alternately think and eat.


2.​ A philosopher can only eat if they have both forks.
3.​ Forks can be shared between adjacent philosophers.
4.​ The system should avoid deadlock (where all philosophers are holding one fork and
waiting for the other).
5.​ The system should avoid starvation (where a philosopher may never get the chance to
eat).

To Solve the Dining Philosophers problem involves designing a strategy for the philosophers to
pick up and release forks in a way that prevents deadlock and starvation while allowing them all
to eat as much as possible.

Some common solutions to the Dining Philosophers problem include:


1.​ Chopstick as a shared resource: Each fork is considered a shared resource, and
philosophers must request and release them in a way that prevents deadlock. Techniques
like using mutex locks or semaphores to coordinate access to the forks can be employed.
2.​ Resource hierarchy: Assign a unique index to each fork and allow philosophers to pick up
forks only in a specific order (e.g., always pick up the lower-indexed fork first). This
prevents circular wait conditions and guarantees progress.
3.​ Asymmetric solution: For example, having one philosopher pick up the left fork first and
another philosopher pick up the right fork first can break symmetry and prevent deadlock.
4.​ Timeouts and resource preemption: Allow philosophers to hold onto forks for a limited
time. If a philosopher cannot obtain both forks within a certain time frame, they release
any forks they currently hold and retry later.
5.​ Philosopher prioritization: Allow certain philosophers to have priority access to forks or
enforce a rule where a philosopher must wait until both neighboring philosophers have
finished eating before attempting to eat themselves.

Sleeping Barber Problem -


This problem is analogy based upon the hypothetical barber shop with one barber and a waiting
room with a limited number of chairs. The barber alternates between cutting hair and taking
breaks. When a customer arrives:

1.​ If there are empty chairs in the waiting room, the customer sits and waits for the barber.
2.​ If all chairs are occupied, the customer leaves the shop.

The problem arises when multiple customers arrive simultaneously and try to access the barber
and the waiting room concurrently. To prevent conflicts and ensure proper behavior,
synchronization mechanisms must be implemented.

Key elements of the problem:

1.​ Barber: Can cut hair or take breaks.


2.​ Waiting Room: Has a limited number of chairs where customers can wait.
3.​ Customers: Arrive, wait if there are available chairs, or leave if the waiting room is full.

To solve the Sleeping Barber Problem, synchronization techniques like semaphores, mutexes, or
monitors can be employed to coordinate access to the barber and waiting room, ensuring mutual
exclusion and preventing race conditions.

A common solution involves using semaphores to control access to shared resources:

●​ A semaphore for the barber chair to ensure exclusive access by either the barber or a
customer.
●​ Another semaphore for the waiting room to limit the number of customers allowed inside.

The logic typically involves:

1.​ Customer arrives:


●​ If the waiting room is not full, enter and wait.
●​ If the waiting room is full, leave.
2.​ Barber:
●​ If a customer is waiting, cut hair.
●​ If no customer is waiting, sleep.
3.​ When the barber finishes cutting hair, the customer leaves the chair, and another customer
may take their place if one is waiting.

Inter-Process Communication-
Processes executing concurrently in the operating system may be either independent processes or
cooperating processes. A process is independent if it does not share data with any other processes
executing in the system. A process is cooperating if it can affect or be affected by the other
processes executing in the system.
There are several reasons for providing an environment that allows process cooperation:
• Information sharing. Since several applications may be interested in the same piece of
information (for instance, copying and pasting), we must provide an environment to allow
concurrent access to such information
. • Computation speedup. If we want a particular task to run faster, we must break it into
subtasks, each of which will be executed in parallel with the others. Notice that such a speedup
can be achieved only if the computer has multiple processing cores.
Unit-3
CPU SCHEDULING
----------
Cpu Scheduling is the process which allows one process to use the cpu while the execution of
another process is on hold due to unavailability of any resources.The aim of scheduling is to
make the system efficient and fast.

Whenever the cpu becomes idle, the operating system must select one of the processes in
the ready queue to be executed.

Purpose: CPU scheduling ensures efficient utilization of the CPU and fairness among
processes.

Process Selection: When the CPU becomes idle, the operating system selects a process
from the ready queue to execute.
Scheduling Algorithms: Various algorithms determine the order in which processes are
scheduled. Some common ones include:

First Come First Serve (FCFS): Processes are executed in the order they arrive.

Shortest Job First (SJF): Prioritizes processes with the shortest burst time.

Round Robin: Each process gets a fixed time slice (quantum) before moving to the next
process.

Priority Scheduling: Assigns priorities to processes based on their importance.

Multilevel Queue Scheduling: Divides processes into different queues with varying priorities.

Key performance criteria for CPU scheduling:

1.​ CPU Utilization: This measures the percentage of time the CPU is busy executing
processes. A good scheduling algorithm should aim to keep the CPU as busy as
possible to maximize throughput.
2.​ Throughput: Throughput is the total number of processes that are completed per unit of
time. A scheduler should strive to maximize the number of processes that are finished
within a given period.
3.​ Turnaround Time: Turnaround time is the total time taken to execute a particular process
from the time of submission to the time of completion. Lower turnaround time indicates
better performance since processes are completed faster.
4.​ Waiting Time: Waiting time is the total time a process spends waiting in the ready queue
before getting CPU time for execution. Minimizing waiting time helps in improving the
efficiency of the CPU scheduler.
5.​ Response Time: Response time is the time taken from when a request was submitted
until the first response is produced. For interactive systems, where responsiveness is
critical, minimizing response time is essential.
6.​ Fairness: Fairness ensures that all processes get a fair share of CPU resources over
time. A scheduler should prevent starvation (where some processes may never get CPU
time) and ensure that each process gets an equitable amount of CPU time.

Preemptive Scheduling:

Preemptive scheduling allows the operating system to interrupt a currently running


process in order to allocate the CPU to another process with higher priority that has
arrived in the ready queue.It provides better responsiveness in interactive systems and
allows for more dynamic prioritization of processes.

Examples: Round Robin, Shortest Remaining Time First (SRTF), Priority Scheduling with
preemption.

Non-preemptive Scheduling:

Non-preemptive scheduling does not allow the operating system to forcibly remove a
process from the CPU. Once a process starts executing, it continues until it voluntarily
relinquishes the CPU or completes its task. Simpler to implement because context
switching occurs only when a process completes or blocks, but may lead to poor
responsiveness in interactive systems.

●​ Examples: First-Come, First-Served (FCFS), Shortest Job Next (SJN or SJF), Priority
Scheduling without preemption.

Scheduler

Schedulers are special system software which handle process scheduling in various ways. A
scheduler is a component responsible for deciding which process or task should be executed
next on the CPU. Its primary role is to manage the allocation of CPU resources efficiently
among multiple processes, ensuring that the system operates effectively and meets
performance objectives.

Schedulers are of these types-

1. Long-Term Scheduler (Job Scheduler):

The long-term scheduler selects processes from the pool of processes in the job queue and
loads them into the ready queue for execution.It controls the degree of multiprogramming ( how
many processes are in memory at a time).

●​ Typically runs less frequently (seconds to minutes) compared to other schedulers.


●​ Optimize overall system performance by admitting processes that can improve system
throughput and efficiency.

2. Short-Term Scheduler (CPU Scheduler):

It is also known as the CPU scheduler, the short-term scheduler selects which ready process
should be executed next and allocates CPU time to that process.

●​ Maximize CPU utilization and ensure fair allocation of CPU resources among
processes.
●​ Runs very frequently (milliseconds) to make rapid decisions about process execution.
●​ Minimize response time, waiting time, and overall turnaround time for processes.

3. Medium-Term Scheduler:

Sometimes referred to as the swap scheduler, the medium-term scheduler decides which
processes should be swapped in and out of main memory (RAM) to manage memory usage
efficiently.

●​ Improve the degree of multiprogramming by swapping out processes that are not
actively being used (e.g., blocked on I/O) to free up memory.

In this condition,to remove the process from the memory and make space for other
process, the suspended process is moved to the secondary storage.

●​ Helps in controlling the number of processes in memory to avoid overloading and to


optimize memory utilization.

Process control block -

A Process Control Block (PCB) in an operating system is a data structure that


contains information about a specific process. This information includes:

1.​ Process Identification: A unique identifier for the process, typically known
as the Process ID (PID).
2.​ Process State: The current state of the process (e.g., ready, running,
waiting, terminated).
3.​ CPU Registers: The contents of all the CPU registers for the process,
essential for context switching.
4.​ Program Counter: The address of the next instruction to be executed for
the process.
5.​ Memory Management Information: Information about the memory
allocated to the process, including base and limit registers or page tables.
6.​ Accounting Information: Information such as the amount of CPU used,
time limits, job or process numbers, etc.
7.​ I/O Status Information: Information on the process’s I/O devices, list of
open files, etc.
8.​ Scheduling Information: Process priority, pointers to scheduling queues,
and other scheduling parameters.
9.​ Miscellaneous Information: Any other information needed by the
operating system to manage and control the process efficiently.

The PCB is crucial for the operating system's ability to manage processes
effectively, context switching, process scheduling, and resource allocation.

Components of Process Control Block-

A Process Control Block (PCB) contains various components that store all the
necessary information about a specific process. The primary components of a
PCB include:

1.​ Process Identification:


○​ Process ID (PID): A unique identifier for the process.
○​ Parent Process ID (PPID): The identifier of the parent process.
2.​ Process State:
○​ The current state of the process (e.g., new, ready, running, waiting,
terminated).
3.​ CPU Registers:
○​ The contents of all the CPU registers for the process, essential for
context switching (e.g., general-purpose registers, stack pointers,
index registers).
4.​ Program Counter:
○​ The address of the next instruction to be executed by the process.
5.​ Memory Management Information:
○​ Base and limit registers.
○​ Page tables or segment tables.
○​ Information about the memory allocated to the process.
6.​ Accounting Information:
○​ CPU usage statistics.
○​ Execution time limits.
○​ Process start time.
○​ Total CPU time used.
7.​ I/O Status Information:
○​ List of I/O devices allocated to the process.
○​ List of open files and their descriptors.
○​ Information on I/O operations currently being performed.
8.​ Scheduling Information:
○​ Process priority.
○​ Scheduling queue pointers (e.g., pointers to other PCBs in the
scheduling queues).
○​ Information about the scheduling policy.
9.​ Inter-process Communication Information:
○​ Information regarding communication mechanisms, such as
message queues, shared memory, and semaphores.

Process Scheduling

Process scheduling is the mechanism by which an operating system decides


which process runs at any given time. The OS uses information stored in the
PCB, such as process priority, state, and resource usage, to make these
decisions.
●​ Process Priority: Each process is assigned a priority level, which helps the
scheduler determine the order of execution. Higher priority processes are
given preference over lower priority ones.
●​ Process State: The current state of a process (ready, running, waiting, etc.)
is crucial for scheduling. The scheduler will typically select processes from
the "ready" state to execute next.
●​ Resource Usage: The scheduler may also consider the resources a
process has already used and its requirements. This helps in making
decisions that optimize resource utilization and avoid bottlenecks.

The scheduler uses algorithms (like Round Robin, Priority Scheduling, Shortest
Job Next, etc.) to manage the execution order, ensuring efficient CPU utilization
and fair process management.

Multitasking-

Multitasking allows an operating system to execute multiple processes seemingly


simultaneously, enhancing system efficiency and user experience.

●​ Resource Allocation: The OS dynamically allocates resources (CPU time,


memory, I/O devices) to processes. This ensures that each process gets
the necessary resources to execute without significant delays.
●​ Process Scheduling: The scheduler plays a critical role in multitasking by
rapidly switching between processes, giving each one a slice of CPU time.
This switching happens so fast that users perceive the processes as
running concurrently.
●​ Process Synchronization: Mechanisms like semaphores, locks, and
monitors are used to manage dependencies between processes and
ensure data consistency. Synchronization prevents race conditions and
ensures that shared resources are accessed in a controlled manner.

By combining these elements, the OS can handle multiple tasks efficiently,


providing a smooth and responsive user experience.

Context Switching

Context switching is the process of saving the state of a currently running


process and restoring the state of another process. This is a fundamental aspect
of multitasking.
●​ Saving Process State: When the CPU switches from one process to
another, it saves the current state (contents of CPU registers, program
counter, etc.) of the running process in its PCB.
●​ Storing in PCB: A copy of the process state is stored in the PCB, ensuring
that all information needed to resume the process later is available.
●​ Restoring State: When the CPU is ready to switch back to a process, it
fetches the saved state from the PCB and restores the process to its
previous state. This involves loading the CPU registers, program counter,
and other necessary information.

This mechanism ensures that processes can be paused and resumed efficiently,
maintaining the illusion of concurrent execution.

Resource Sharing

Resource sharing is the practice of allowing multiple processes to use the same
resources, like memory, files, and I/O devices, simultaneously.
●​ Tracking Resources: The PCB contains information about the resources
allocated to each process, such as open files, memory segments, and I/O
devices. This tracking is essential for efficient resource management.
●​ Resource Allocation: When a new process needs a resource, the OS
checks the PCB to see if the resource is already in use and manages its
allocation accordingly. This can involve sharing the resource or queuing
the request until the resource becomes available.
●​ Ensuring Fair Access: The OS employs various strategies to ensure that all
processes get fair access to shared resources. This might include
techniques like resource scheduling, priority inheritance, and deadlock
prevention.

Effective resource sharing improves system efficiency, prevents resource


contention, and ensures smooth process execution.

Process Address Space

A process address space is the set of memory addresses that a process can use
to execute instructions and store data. Each process in an operating system has
its own unique address space, ensuring that processes do not interfere with each
other’s memory. This isolation provides both security and stability within the
system.

Key Components of a Process Address Space

1.​ Text Segment:


○​ Definition: This segment contains the executable code of the
program.
○​ Characteristics: It is typically read-only to prevent accidental
modification of instructions.
○​ Usage: Stores the compiled machine code that the CPU executes.
2.​ Data Segment:
○​ Definition: This segment holds global and static variables that are
initialized before execution begins.
○​ Characteristics: This can be further divided into initialized and
uninitialized segments.
■​ Initialized Data Segment: Contains global and static variables
that are explicitly initialized.
■​ Uninitialized Data Segment (BSS): Contains global and static
variables that are initialized to zero or do not have explicit
initialization in the source code.
○​ Usage: Used for variables that retain their values throughout the
program execution.
3.​ Heap Segment:
○​ Definition: This segment is used for dynamic memory allocation.
○​ Characteristics: Memory in the heap is managed via functions like
malloc, calloc, realloc in C, and new and delete in C++.
○​ Usage: Suitable for data structures whose size can change at
runtime, such as linked lists, trees, and other complex data
structures.
4.​ Stack Segment:
○​ Definition: This segment holds local variables, function parameters,
return addresses, and helps with function calling conventions.
○​ Characteristics: It grows and shrinks dynamically as functions are
called and return.
○​ Usage: Used for managing function calls and local variables. Each
function call creates a new stack frame that contains its execution
context.
5.​ Memory Mapping Segment:
○​ Definition: This segment includes memory-mapped files and shared
memory.
○​ Characteristics: It allows files or devices to be mapped into the
process’s address space.
○​ Usage: Used for efficient file I/O operations and inter-process
communication (IPC).

Address Space Layout

The layout of a process address space typically follows a structured format,


though exact details can vary depending on the operating system and
architecture:

●​ Lowest Address: Reserved for the operating system.


●​ Text Segment: Contains the executable code.
●​ Data Segment: Contains initialized and uninitialized data.
●​ Heap Segment: Starts just after the data segment and grows upwards.
●​ Stack Segment: Located at the top of the address space and grows
downwards.
●​ Memory Mapping Segment: Positioned between the heap and stack, used
for dynamically mapped memory areas.

Virtual vs. Physical Address Space

●​ Virtual Address Space: This is the address space that a process sees. The
operating system, with the help of the Memory Management Unit (MMU),
maps these virtual addresses to physical addresses in the computer’s
RAM.

●​ Physical Address Space: The actual RAM addresses that correspond to


the virtual addresses.
Memory Management Techniques

1.​ Paging: Divides the virtual memory into fixed-size blocks called pages and
the physical memory into blocks of the same size called frames. Pages can
be mapped to any available frame in physical memory.
2.​ Segmentation: Divides the memory into segments of variable length, each
corresponding to different logical units like functions or data arrays.

Importance of Process Address Space

1.​ Isolation: Ensures that processes do not interfere with each other’s memory,
enhancing security and stability.
2.​ Efficient Memory Utilization: Allows the operating system to manage
memory dynamically and efficiently, enabling processes to share physical
memory through techniques like paging.
3.​ Flexibility: Provides a framework for dynamic memory allocation, essential
for modern applications that require flexible memory management.

Threads in Operating Systems

A thread is the smallest unit of execution within a process. A process can have
multiple threads sharing the same resources, such as memory, but each thread
has its own execution context, including a program counter, registers, and stack.
Threads allow for parallel execution within a process, improving the performance
and responsiveness of applications.
Types of Threads

1. User Threads:

User-level threads are implemented and managed by the user and the kernel is
not aware of it.User-level threads are implemented using user-level libraries and
the OS does not recognize these threads.User-level thread is faster to create and
manage compared to kernel-level thread.Context switching in user-level threads
is faster.If one user-level thread performs a blocking operation then the entire
process gets blocked. Eg: POSIX threads, Java threads, etc.

2. Kernel level Thread:

Kernel level threads are implemented and managed by the OS.Kernel level
threads are implemented using system calls and Kernel level threads are
recognized by the OS.Kernel-level threads are slower to create and manage
compared to user-level threads.Context switching in a kernel-level thread is
slower.Even if one kernel-level thread performs a blocking operation, it does not
affect other threads. Eg: Window Solaris.
Advantages of Threads

1.​ Improved Performance:


○​ Threads allow multiple tasks to be performed simultaneously,
leading to better CPU utilization and improved application
performance, especially on multi-core processors.
2.​ Responsiveness:
○​ In interactive applications, threads can keep the user interface
responsive by offloading time-consuming tasks to separate threads.
3.​ Resource Sharing:
○​ Threads within the same process share the same address space
and resources, making it easier and more efficient to share data and
resources between them without the overhead of inter-process
communication (IPC).
4.​ Simplified Program Structure:
○​ Threads enable simpler and more modular program designs. For
instance, different functionalities can be encapsulated in different
threads.
5.​ Concurrency:
○​ Threads can execute concurrently, improving the throughput of
applications and allowing better utilization of system resources.

Disadvantages of Threads

1.​ Complexity in Synchronization:


○​ Threads share the same address space, leading to potential issues
like race conditions, deadlocks, and priority inversion. Proper
synchronization mechanisms (e.g., mutexes, semaphores) are
necessary, which can be complex to implement and debug.
2.​ Overhead:
○​ Although threads are lighter than processes, creating and managing
threads still incurs some overhead. This includes the cost of context
switching between threads.
3.​ Debugging Difficulty:
○​ Multithreaded programs are generally more difficult to debug and
test compared to single-threaded programs due to issues like race
conditions and deadlocks.
4.​ Resource Contention:
○​ Threads within the same process compete for CPU time and
resources, which can lead to contention and potential performance
bottlenecks if not managed properly.
5.​ Limited by Process:
○​ Since threads share the same process resources, a problem in one
thread (e.g., memory corruption) can affect the entire process,
leading to crashes or undefined behavior.

Thread Cancellation:
Thread cancellation is the process of terminating a thread before it has
completed its execution. This can be necessary in various scenarios, such as
when a thread is no longer needed, when an application is shutting down, or
when a thread is performing a task that is taking too long. Thread cancellation is
a complex operation due to the need to ensure resources are properly cleaned
up and that other threads are not adversely affected.

First Come First Serve (FCFS) Scheduling Algorithm

The First Come First Serve (FCFS) scheduling algorithm is one of the simplest and
easiest-to-understand scheduling algorithms in operating systems. It works on a first-come,
first-served basis, meaning that the process that arrives first gets executed first. This approach
is non-preemptive, meaning that once a process starts executing, it runs to completion before
any other process can start.

Characteristics of FCFS

1.​ Non-Preemptive: Once a process starts executing, it cannot be interrupted until it


completes.
2.​ FIFO (First-In, First-Out): Processes are queued in the order of their arrival, and they are
executed in that same order.
3.​ Simple and Easy to Implement: FCFS is straightforward to understand and implement,
often used as a baseline for comparing more complex algorithms.

How FCFS Works

1.​ Arrival: Processes arrive and are placed in the ready queue in the order they arrive.
2.​ Execution: The CPU picks the first process in the queue, executes it to completion, then
moves on to the next process.
3.​ Completion: Each process runs until it finishes, without being preempted by another
process.

Example

Consider three processes with the following arrival and burst times:

Example

S. No Process ID Arrival Time Burst Time

___ ______ _______ _______

1 P1 0 9

2 P2 1 3

3 P3 1 2

4 P4 1 4

5 P5 2 3

6 P6 3 2

Gantt chart :
Process Start Completion Time Waiting Time Turnaround Time
Time

P1 0 9 0 9

P2 9 12 8 11

P3 12 14 11 13

P4 14 18 13 17

P5 18 21 16 19

Advantages of FCFS

1.​ Simplicity: The algorithm is straightforward and easy to implement.


2.​ Fairness: Each process gets a fair share of CPU time in the order of its arrival.

Disadvantages of FCFS

1.​ Poor Performance with Varying Burst Times: If a process with a long burst time arrives
first, it can delay all subsequent processes (known as the "convoy effect").
2.​ High Waiting Time: Processes arriving later might have to wait a long time, leading to
high average waiting time.
3.​ No Preemption: Once a process starts, it runs to completion, which can be inefficient if
shorter processes are waiting.

Applications of FCFS

●​ Batch Processing: Suitable for batch systems where all processes are of similar
importance and have similar execution times.
●​ Simple Systems: Used in systems where simplicity is more critical than performance,
such as embedded systems with limited processing needs.

Shortest Job First (SJF) Scheduling Algorithm


Shortest Job First (SJF) is a scheduling algorithm that selects the process with
the smallest burst time for execution next. It can be either preemptive or
non-preemptive. In this example, we will use the non-preemptive version of SJF.

Given Data

S. No Process ID Arrival Time Burst Time

1 P1 0 9

2 P2 1 3

3 P3 1 2

4 P4 1 4

5 P5 2 3

6 P6 3 2

Execution Order:

1.​ At time 0, P1 arrives and starts execution.


2.​ At time 1, P2, P3, and P4 arrive, but P3 has the shortest burst time of 2
units.
3.​ P5 arrives at time 2.
4.​ P6 arrives at time 3.

Steps to Determine Execution Order:

1.​ P1 starts at time 0 and completes at time 9.


2.​ At time 9, P2, P3, P4, P5, and P6 have arrived.
3.​ P3 has the shortest burst time (2 units), so it runs next.
4.​ After P3, among the remaining processes, P2 and P5 both have a burst
time of 3, but P2 arrived earlier.
5.​ Next, P5 runs.
6.​ After P5, P4 runs because it has the shortest burst time among the
remaining processes.
7.​ Finally, P6 runs.
Gantt Chart:

| P1 | P3 | P2 |P5 | P4 | P6 |
0 9 11 14 17 21 23

Detailed Breakdown

Proce Start Completion Waiting Turnaround


ss Time Time Time Time

P1 0 9 0 9

P2 11 14 10 13

P3 9 11 8 10

P4 17 21 16 20

P5 14 17 12 15

P6 21 23 18 20

1.​ Total Waiting Time = 0 + 10 + 8 + 16 + 12 + 18 = 64 Average Waiting Time


= 64 / 6 ≈ 10.67 units
2.​ Total Turnaround Time = 9 + 13 + 10 + 20 + 15 + 20 = 87 Average
Turnaround Time = 87 / 6 ≈ 14.5 units

Advantages:

1.​ Optimal for Short Jobs:


○​ SJF minimizes the average waiting time for processes. By selecting
the process with the shortest burst time next, it ensures that shorter
jobs are executed quickly, leading to reduced waiting time.
2.​ Minimized Turnaround Time:
○​ Since shorter jobs are prioritized, the turnaround time for these jobs
is also minimized, which improves overall system performance.
3.​ High Throughput:
○​ SJF scheduling can lead to higher throughput because shorter
processes are completed more quickly, allowing more processes to
be executed in a given time frame.
4.​ Fairness for Short Jobs:
○​ Short processes are less likely to be delayed significantly, ensuring
that they get a fair share of the CPU time.

Disadvantages:

1.​ Starvation:
○​ Longer processes may suffer from starvation, as they can be
perpetually postponed if there are always shorter processes arriving.
This is a major issue in systems where a mix of short and long
processes exists.
2.​ Difficulty in Estimating Burst Time:
○​ SJF requires knowledge of the burst time of processes, which is
often difficult to estimate accurately. Inaccurate predictions can lead
to suboptimal scheduling.
3.​ Not Preemptive:
○​ In its non-preemptive form, once a process starts execution, it runs
to completion. This can be problematic if a very long process starts
executing while several shorter processes arrive subsequently.
4.​ Inflexibility:
○​ SJF is not flexible and does not adapt well to varying workloads. In
real-time systems, where process priorities and burst times can
change dynamically, SJF may not perform efficiently.
5.​ Implementation Complexity:
○​ Maintaining a list of processes sorted by their burst time and
continuously updating it can add overhead and complexity to the
system.

Shortest Remaining Time First (SRTF) Scheduling Algorithm

Shortest Remaining Time First (SRTF) is the preemptive version of the Shortest
Job First (SJF) scheduling algorithm. In SRTF, the process with the smallest
remaining burst time is selected for execution next. If a new process arrives with
a shorter burst time than the remaining time of the current process, the current
process is preempted and the new process is executed.

Given Data

S. Process Process Arrival Burst


No ID Name Time Time

1 P1 A 0 9

2 P2 B 1 3

3 P3 C 1 2

4 P4 D 1 4

5 P5 E 2 3

6 P6 F 3 2

Steps to Determine Execution Order:

1.​ At time 0, only P1 has arrived, so it starts execution.


2.​ At time 1, P2, P3, and P4 arrive. P3 has the shortest burst time (2 units),
so it preempts P1.
3.​ At time 3, P3 completes, and P6 arrives. P6 has the shortest burst time (2
units), so it preempts the others.
4.​ At time 5, P6 completes. Among the remaining processes, P2 has the
shortest remaining time (3 units).
5.​ P2 runs until completion. Then P5 runs next as it has the shortest
remaining time.
6.​ P4 runs next.
7.​ P1 runs last, as it has the longest remaining time.

Gantt Chart

| P1 | | P3 | | P6 | | P2 | | P5 | | P4 | | P1 |

0 1 3 5 8 11 14 18 23

Detailed Breakdown

Proce Start Completion Waiting Turnaround


ss Time Time Time Time

P1 0 1 14 23

P2 5 8 4 7

P3 1 3 0 2

P4 14 18 13 17

P5 8 11 6 9

P6 3 5 0 2

Performance Metrics

1.​ Waiting Time:


○​ P1: 14 units
○​ P2: 4 units
○​ P3: 0 units
○​ P4: 13 units
○​ P5: 6 units
○​ P6: 0 units
2.​ Total Waiting Time = 14 + 4 + 0 + 13 + 6 + 0 = 37 Average Waiting Time =
37 / 6 ≈ 6.17 units
3.​ Turnaround Time:
○​ P1: 23 units
○​ P2: 7 units
○​ P3: 2 units
○​ P4: 17 units
○​ P5: 9 units
○​ P6: 2 units
4.​ Total Turnaround Time = 23 + 7 + 2 + 17 + 9 + 2 = 60 Average Turnaround
Time = 60 / 6 = 10 units

Advantages and Disadvantages of SRTF

Advantages:

1.​ Optimal for Short Jobs: SRTF minimizes the average waiting time by
always executing the process with the shortest remaining time, which is
beneficial for short processes.
2.​ Minimized Turnaround Time: By focusing on the shortest remaining time,
the overall turnaround time is minimized.
3.​ High Responsiveness: Preemptive nature allows for higher
responsiveness, as the scheduler can quickly switch to shorter processes
as they arrive.

Disadvantages:

1.​ Starvation: Longer processes may suffer from starvation if there are
always shorter processes arriving.
2.​ Overhead: Frequent context switching due to preemption can add
overhead and degrade performance.
3.​ Complexity: Implementing SRTF requires maintaining and updating a list
of remaining times, which adds complexity to the scheduler.
4.​ Difficult Estimation: Accurately predicting the remaining time for each
process is challenging, which can lead to inefficiencies.

Priority Scheduling Algorithm

Priority scheduling is a scheduling algorithm where each process is assigned a


priority, and the process with the highest priority is selected for execution next. In
some implementations, a lower numerical value indicates a higher priority. This
can be either preemptive or non-preemptive.

Given Data

Let's assume we have the following processes with their respective priorities
(lower value means higher priority):

S. Process Process Arrival Burst Priorit


No ID Name Time Time y

1 P1 A 0 9 2

2 P2 B 1 3 1

3 P3 C 1 2 4

4 P4 D 1 4 3

5 P5 E 2 3 2
6 P6 F 3 2 1

Execution Order

1.​ At time 0, only P1 has arrived and starts execution.


2.​ At time 1, P2, P3, and P4 arrive. P2 has the highest priority.
3.​ At time 2, P5 arrives. P2 continues execution as it has the highest priority.
4.​ At time 3, P6 arrives with the highest priority, so it preempts P2.
5.​ After P6 completes, P2 resumes execution.
6.​ After P2 completes, P5 runs next due to its high priority.
7.​ P1 resumes execution after P5.
8.​ Finally, P4 and P3 run according to their arrival time and priority.

Detailed Breakdown

Proces Start Completion Waiting Turnaround


s Time Time Time Time

P1 0 1 9 17

P2 1 3 3 8

P3 21 23 20 22

P4 17 21 16 20

P5 9 12 7 10

P6 3 6 0 3
Performance Metrics

1.​ Waiting Time:


○​ P1: 9 units
○​ P2: 3 units
○​ P3: 20 units
○​ P4: 16 units
○​ P5: 7 units
○​ P6: 0 units
2.​ Total Waiting Time = 9 + 3 + 20 + 16 + 7 + 0 = 55 Average Waiting Time =
55 / 6 ≈ 9.17 units
3.​ Turnaround Time:
○​ P1: 17 units
○​ P2: 8 units
○​ P3: 22 units
○​ P4: 20 units
○​ P5: 10 units
○​ P6: 3 units
4.​ Total Turnaround Time = 17 + 8 + 22 + 20 + 10 + 3 = 80 Average
Turnaround Time = 80 / 6 ≈ 13.33 units

Advantages and Disadvantages of Priority Scheduling

Advantages:

1.​ Flexibility:
○​ Priority scheduling allows for a flexible system where critical tasks
can be given precedence over less critical ones.
2.​ Efficient Utilization:
○​ It ensures that high-priority processes get immediate attention, which
can be crucial for time-sensitive tasks.
3.​ Reduced Turnaround Time for Important Processes:
○​ Important processes get completed faster as they are given higher
priority, reducing their turnaround time.

Disadvantages:

1.​ Starvation:
○​ Low-priority processes may suffer from starvation if high-priority
processes continue to arrive and get executed.
2.​ Complexity:
○​ Managing and updating priorities adds complexity to the scheduling
system, requiring more sophisticated data structures and algorithms.
3.​ Indeterminate Waiting Time:
○​ The waiting time for lower-priority processes is indeterminate,
leading to potential unpredictability in process completion times.
4.​ Preemption Overhead:
○​ In the preemptive version, frequent context switching can lead to
increased overhead, affecting overall system performance.
5.​ Potential for Priority Inversion:
○​ Situations can arise where lower-priority processes hold resources
needed by higher-priority processes, leading to priority inversion.
Solutions like priority inheritance are needed to handle this.

Round Robin Scheduling Algorithm

Round Robin (RR) is a preemptive scheduling algorithm where each process is


assigned a fixed time slice or quantum. The CPU executes each process for a
time interval equal to the quantum, then moves to the next process in the ready
queue. If a process's burst time exceeds the quantum, it is preempted and placed
at the end of the queue, allowing the next process to execute. This continues
until all processes are completed.

Given Data

S. Process Process Arrival Burst


No ID Name Time Time

1 P1 A 0 9

2 P2 B 1 3
3 P3 C 1 2

4 P4 D 1 4

5 P5 E 2 3

6 P6 F 3 2

Assumptions

●​ Time Quantum = 2 units

Execution Order

1.​ At time 0, P1 starts execution.


2.​ At time 2, P1 is preempted and P2 starts execution.
3.​ At time 4, P2 is preempted and P3 starts execution.
4.​ At time 6, P3 completes, P4 starts execution.
5.​ At time 8, P4 is preempted and P5 starts execution.
6.​ At time 10, P5 is preempted and P6 starts execution.
7.​ At time 12, P6 completes, P1 resumes.
8.​ At time 14, P1 is preempted, P2 resumes.
9.​ At time 15, P2 completes, P4 resumes.
10.​ At time 17, P4 completes, P5 resumes.
11.​At time 18, P5 completes, P1 resumes.
12.​ At time 21, P1 completes.

Gantt Chart

| P1 | P2 | P3 | P4 | P5 | P6 | P1 | P2 | P4 | P5 | P1 |

0 2 4 6 8 10 12 14 15 17 18 21
Detailed Breakdown

Proce Start Completion Waiting Turnaround


ss Time Time Time Time

P1 0, 12, 18 2, 14, 21 12 21

P2 2, 14 4, 15 11 14

P3 4 6 3 5

P4 6, 15 8, 17 12 16

P5 8, 17 10, 18 10 16

P6 10 12 7 9

Performance Metrics

1.​ Waiting Time:


○​ P1: 12 units
○​ P2: 11 units
○​ P3: 3 units
○​ P4: 12 units
○​ P5: 10 units
○​ P6: 7 units
2.​ Total Waiting Time = 12 + 11 + 3 + 12 + 10 + 7 = 55 Average Waiting Time
= 55 / 6 ≈ 9.17 units
3.​ Turnaround Time:
○​ P1: 21 units
○​ P2: 14 units
○​ P3: 5 units
○​ P4: 16 units
○​ P5: 16 units
○​ P6: 9 units
4.​ Total Turnaround Time = 21 + 14 + 5 + 16 + 16 + 9 = 81 Average
Turnaround Time = 81 / 6 ≈ 13.5 units

Advantages and Disadvantages of Round Robin

Advantages:

1.​ Fairness:
○​ Each process gets an equal share of the CPU time, ensuring
fairness among all processes.
2.​ Simplicity:
○​ Easy to implement and understand.
3.​ Responsiveness:
○​ Good response time for processes, as no process can monopolize
the CPU.
4.​ Preemptive Nature:
○​ Preemptive scheduling allows the system to handle multiple
processes efficiently, making it suitable for time-sharing systems.

Disadvantages:

1.​ High Context Switching Overhead:


○​ Frequent context switches can lead to significant overhead, reducing
CPU efficiency.
2.​ Performance Dependent on Time Quantum:
○​ If the time quantum is too small, the number of context switches
increases, degrading performance. If the time quantum is too large,
it behaves like FCFS, leading to higher waiting times.
3.​ Not Optimal for All Workloads:
○​ Processes with shorter burst times may experience longer waiting
times compared to other scheduling algorithms like SJF.
4.​ Average Waiting Time Can Be High:
○​ Compared to SJF or priority scheduling, the average waiting time in
RR can be higher, especially if processes have varying burst times.

Deadlock- Deadlock is a scenario in computing where two or more processes


are unable to continue because each one is waiting for the other to release a
resource.

Key concepts include:

●​ Mutual Exclusion: At least one resource is non-shareable.


●​ Resource Holding: Processes hold resources and wait for others.
●​ Circular Wait: A circular chain of processes exists where each process
holds at least one resource and is waiting for another resource held by the
next process in the chain.
●​ No Preemption: Resources cannot be forcibly taken from processes; they
must be released voluntarily.

Example of Deadlock:

Consider two trains heading toward each other on a single track. Neither can
move forward once they meet, as each is waiting for the other to move aside.
This illustrates a deadlock situation.
How Does Deadlock Occur in the Operating System?

In an operating system, processes typically follow these steps for resource


usage:

1.​ Request a Resource


2.​ Use the Resource
3.​ Release the Resource

Deadlock occurs when two or more processes hold resources and wait for
resources held by others. For instance, if Process 1 holds Resource 1 and waits
for Resource 2 (held by Process 2), and Process 2 waits for Resource 1, a
deadlock ensues.

Example Scenarios:

1.​ Two Tape Drives: If Process P0 and P1 each hold one tape drive and need
another, they can deadlock if each waits for the other.
2.​ Semaphores: If two processes use semaphores A and B and both wait for
each other’s semaphore, a deadlock occurs.
3.​ Memory Allocation: If two processes request memory in such a way that
their combined needs exceed available memory, deadlock can occur when
each waits for memory held by the other.

Necessary Conditions for Deadlock in OS

Deadlock can arise if the following four conditions hold simultaneously:

1.​ Mutual Exclusion: Resources are non-shareable.


2.​ Hold and Wait: A process is holding at least one resource and waiting for
additional resources.
3.​ No Preemption: Resources cannot be forcibly taken from processes.
4.​ Circular Wait: A circular chain of processes exists, with each process
holding and waiting for resources.

What is Deadlock Detection?

Deadlock detection involves checking if any processes are stuck waiting for each
other indefinitely. Several algorithms can help detect deadlocks, such as:

1.​ Resource Allocation Graphs: Detect cycles indicating deadlocks.


2.​ Banker’s Algorithm: Ensures resource allocation leaves the system in a
safe state.

Methods for Handling Deadlock

There are three main approaches to handling deadlock:

1.​ Deadlock Prevention or Avoidance


2.​ Deadlock Recovery
3.​ Deadlock Ignorance

Deadlock Prevention or Avoidance

Deadlock Prevention aims to ensure at least one of the necessary conditions


for deadlock cannot hold:

1.​ Mutual Exclusion: Use locks only for non-shareable resources.


2.​ Hold and Wait: Require processes to request all needed resources upfront
or ensure they hold no other resources when requesting.
3.​ No Preemption: Preempt resources from processes waiting for additional
resources.
4.​ Circular Wait: Impose an ordering on resource acquisition.

Deadlock Avoidance uses strategies like the Banker’s Algorithm to ensure the
system never enters an unsafe state.

Deadlock Recovery

If prevention or avoidance isn't used, deadlock detection and recovery come into
play. The system periodically checks for deadlocks and, if detected, uses
strategies like:

1.​ Manual Intervention: Operators resolve deadlocks manually.


2.​ Automatic Recovery: The system automatically aborts processes or
preempts resources to break the deadlock.
3.​ Process Termination: Terminate processes to eliminate the circular wait.
4.​ Resource Preemption: Preempt resources and possibly roll back
processes to a safe state.

Example of Deadlock Recovery:


1.​ Process Termination:
○​ Abort all Deadlocked Processes: All processes in the deadlock are
terminated.
○​ Abort One Process at a Time: Processes are terminated one by
one until the deadlock is resolved.
2.​ Resource Preemption:
○​ Selecting a Victim: Choose a process to preempt based on criteria
like resource usage.
○​ Rollback: Rollback processes to safe states to free resources.

What is Deadlock Ignorance?

Deadlock ignorance involves allowing deadlocks to occur but handling them


infrequently and usually through system reboots. This approach is used in
systems where deadlocks are rare and the cost of prevention or detection is high.

Safe State

A safe state is one where no deadlock exists and all processes can complete
their tasks without causing a deadlock. If a process needs an unavailable
resource, it waits until the resource is freed by another process. If such a
sequence does not exist, the system is in an unsafe state.

Deadlock vs. Starvation

Aspect Deadlock Starvation

Definition Processes are blocked A process is perpetually denied


indefinitely, waiting for resources despite their
resources. availability.

Resource Resources are held by Resources are available but


Availability processes in deadlock. allocated to other processes.
Cause Circular dependency among Continuous preference to other
processes. processes, causing indefinite
wait.

Resolution Requires intervention to Mitigated by adjusting scheduling


break the cycle. policies.
Unit 4

Memory Management
Memory management is the process of controlling and coordinating computer memory,
assigning portions called blocks to various running programs to optimize overall system
performance.

Memory management is a critical function of an operating system responsible


for handling the computer's primary memory. It ensures efficient allocation, utilization,
and protection of memory resources, enabling multiple processes to run concurrently
without interference.

Key Functions of Memory Management

1.​ Allocation and Deallocation:


○​ Allocation: Assigns memory to processes when they need it.
○​ Deallocation: Frees up memory when processes no longer need it.
2.​ Protection:
○​ Ensures that processes do not interfere with each other’s memory.
○​ Prevents unauthorized access to memory, maintaining system stability and
security.
3.​ Segmentation and Paging:
○​ Segmentation: Divides memory into different segments based on the
logical divisions of a program (e.g., code, data, stack).
○​ Paging: Divides memory into fixed-size pages, mapping logical addresses
to physical addresses.
4.​ Swapping:
○​ Moves processes between main memory and secondary storage to
manage memory usage efficiently.
5.​ Virtual Memory:
○​ Extends physical memory using disk space, allowing processes to use
more memory than is physically available.
6.​ Fragmentation Management:
○​ Internal Fragmentation: Wasted memory within allocated regions.
○​ External Fragmentation: Wasted memory outside allocated regions.
○​ Uses techniques to minimize both types of fragmentation.

Requirements of Memory Management

1.​ Relocation:
○​ Relocation in memory management refers to the process of assigning a
process's memory address space during its execution. It involves moving
processes around in memory to ensure optimal use of available memory
and to accommodate dynamic memory allocation requirements.
2.​ Protection:
○​ Ensuring that processes are protected from each other to prevent
accidental or malicious interference.
○​ Implements mechanisms like base and limit registers, segmentation, and
paging to ensure that a process cannot access memory outside its
allocated region.
○​ Provides error handling for attempts to access restricted memory areas.
3.​ Sharing:
○​ Allowing multiple processes to access the same memory region safely and
efficiently.
○​ Memory management allows shared access while maintaining protection
and consistency, often using techniques like shared pages or segments.
4.​ Logical Organization:
○​ Memory is organized in a way that matches the logical structure of
applications, facilitating easy access and modification.
5.​ Physical Organization:
○​ Efficient physical arrangement of memory to optimize performance and
resource utilization.
○​ Efficiently arranges physical memory to reduce access times and increase
throughput.
○​ Involves strategies like contiguous allocation, non-contiguous allocation,
and hierarchical memory structures.

Bare machine

A bare machine is a computer system without an operating system or any other system
software. It consists of hardware components like the CPU, memory, and I/O devices,
but it lacks the software layers that manage these resources.

Resident Monitor

The Resident Monitor is a code that runs on Bare Machines. ​


The resident monitor works like an operating system that controls the instructions and performs
all necessary functions. It also works like a job sequencer because it also sequences the job and
sends them to the processor.
After scheduling the job Resident monitors loads the programs one by one into the main memory
according to their sequences.

The Resident monitors are divided into 4 parts as:


1. Control Language Interpreter

2. Loader

3. Device Driver

4. Interrupt Processing

Address binding is the process of mapping logical addresses used by a program


to physical addresses in the computer's memory. This mapping can occur at
various stages of a program’s lifecycle, including compile time, load time, and run
time.

Types of Addresses

1.​ Logical Address (Virtual Address):


○​ The address generated by the CPU during program execution.
○​ Seen by the program, it is a part of the program's address space.
2.​ Physical Address:
○​ The actual address in the computer's memory hardware.
○​ The location where data or instructions reside physically.
Stages of Address Binding

1.​ Compile-Time Binding:


○​ Occurs when the source code is compiled into machine code.
○​ If the program's memory location is known at compile time, absolute
addresses are generated.
○​ Example: Embedded systems where the program always runs at a
specific memory location.
○​ Advantage: Simplicity.
○​ Disadvantage: Inflexibility; the program must always be loaded at
the same location.
2.​ Load-Time Binding:
○​ Occurs when the program is loaded into memory.
○​ The compiler generates relocatable code, which the loader
translates to absolute addresses.
○​ Example: General-purpose computers where the program can be
loaded into different memory locations.
○​ Advantage: Flexibility in memory location.
○​ Disadvantage: Cannot move the program once it starts executing.
3.​ Run-Time Binding:
○​ Occurs during program execution.
○​ Logical addresses are translated to physical addresses dynamically
using hardware.
○​ Example: Modern operating systems with support for virtual memory.
○​ Advantage: Maximum flexibility; processes can be moved during
execution.
○​ Disadvantage: Complexity and overhead.

Dynamic Loading

Dynamic loading is a technique used in operating systems where a program's


subroutines or modules are loaded into memory only when they are needed
during execution. This approach helps optimize memory usage and can enhance
the performance of the system.

Benefits of Dynamic Loading

1.​ Efficient Memory Utilization:


○​ Only the essential components of a program are loaded, reducing
overall memory usage.
○​ Allows more programs to be resident in memory simultaneously.
2.​ Reduced Startup Time:
○​ Initial loading time is minimized because only a small portion of the
program is loaded initially.
○​ The rest of the program is loaded as needed.
3.​ Flexibility:
○​ Programs can be written to dynamically load different modules
based on runtime conditions or user inputs.
○​ Supports modular programming where components can be
independently developed and loaded.
4.​ Simplified Updates:
○​ Updating a module does not require the entire program to be
recompiled or reloaded.
○​ Only the updated module needs to be replaced.

Dynamic Linking

Dynamic linking is a technique used in computer systems where executable


programs are linked to shared libraries at runtime rather than at compile time.
This allows multiple programs to share common libraries, reducing memory
usage and facilitating easier updates to shared code.

Dynamic Linking Working

1.​ Compilation:
○​ The program is compiled with references to the shared libraries.
○​ These references include the names and locations of the required
libraries.
2.​ Linking:
○​ At compile time, a stub is included in the executable, representing
the shared library.
○​ The actual linking occurs at runtime when the program is executed.
3.​ Loading:
○​ When the program starts, the dynamic linker/loader loads the
required shared libraries into memory.
○​ The linker resolves the references to the library functions and data,
updating the program's address space.
4.​ Execution:
○​ The program executes using the functions and data from the shared
libraries.
○​ If multiple programs use the same library, the library code is shared
among them in memory.

Feature Static Linking Dynamic Linking

Definition The process of combining The process of linking external


all necessary library libraries and references at
routines and external runtime, when the program is
references into a single loaded or executed.
executable file at
compile-time.

Linking Time Occurs at compile-time. Occurs at runtime.

File Size Generally results in a Results in a smaller file size, as


larger file size, as all libraries are linked dynamically at
required libraries are runtime.
included in the executable.

Flexibility Less flexible, as any More flexible, as libraries can be


updates or changes to the updated or replaced without
libraries require recompiling the program.
recompilation and relinking
of the entire program.

Performance Faster program startup Slightly slower program startup


and direct execution, as all due to the additional linking
libraries are already linked. process, but overall performance
impact is minimal.

Memory Each program has its own Multiple programs can share the
Usage copy of the library code in same library code in memory,
memory, potentially reducing overall memory usage.
increasing overall memory
usage if multiple programs
use the same library.

Compatibility Ensures compatibility as May face compatibility issues if a


the program and libraries program expects a specific
are compiled together. version of a library, but a different
version is available at runtime.

Development Simplifies debugging as Reduces executable size and


the libraries are part of the simplifies distribution, but can
executable, but increases complicate debugging due to the
the size of the executable separation of the executable and
and distribution complexity. the libraries, requiring proper
versioning and dependency
management.

Security Once compiled, the Dynamic libraries can be replaced


program is self-contained, or tampered with, potentially
making it harder to alter introducing security vulnerabilities
libraries maliciously. if not managed properly.

Multiprogramming with Fixed Partitions

Multiprogramming with fixed partitions is a memory management technique


where the main memory is divided into a fixed number of partitions at system
startup. Each partition can hold exactly one process, and the size and number of
partitions are determined beforehand.

Key Concepts of Fixed Partitions

1.​ Fixed Partitioning:


○​ The memory is divided into fixed-sized partitions.
○​ The number and size of these partitions are set during system
initialization and do not change.
2.​ Process Allocation:
○​ Each partition can hold one process at a time.
○​ Processes are allocated to the first available partition large enough
to accommodate them.
3.​ Internal Fragmentation:
○​ If a process does not use all the space within a partition, the
remaining space is wasted.
○​ This unused space within a partition is called internal fragmentation.

Multiprogramming with Fixed Partitions Works

1.​ Memory Initialization:


○​ The operating system divides the memory into fixed partitions.
○​ Example: A system with 256 KB of memory might be divided into
four partitions of 64 KB each.
2.​ Loading Processes:
○​ Processes are loaded into the partitions.
○​ If a process requires 50 KB of memory, it will be loaded into a 64 KB
partition, leaving 14 KB unused.
3.​ Process Execution:
○​ The operating system manages the execution of processes in these
partitions.
○​ If a partition becomes free (e.g., when a process terminates), a new
process can be loaded into that partition.

Advantages and Disadvantages of Fixed Partitions

Advantages:

●​ Simple to implement and manage.


●​ Easy to understand and predict memory usage.

Disadvantages:

●​ Internal Fragmentation: Significant memory waste due to unused space


within partitions.
●​ Inflexibility: The fixed number and size of partitions can lead to inefficient
use of memory, especially if processes vary greatly in size.

Multiprogramming with Variable Partitions

Definition: Multiprogramming with variable partitions is a memory management


technique where the main memory is divided into partitions dynamically based on
the size of the processes. Partitions are created as needed and can vary in size.

Key Concepts of Variable Partitions

1.​ Dynamic Partitioning:


○​ Partitions are created dynamically as processes are loaded into
memory.
○​ Each partition is exactly the size needed to accommodate the
process.
2.​ Process Allocation:
○​ Processes are allocated to memory blocks of the exact size
required.
○​ When a process terminates, its memory block becomes available for
other processes.
3.​ External Fragmentation:
○​ Over time, the memory can become fragmented with many small
gaps between allocated memory blocks.
○​ This fragmentation can make it difficult to find contiguous memory
blocks for new processes.

Protection Schemes in Memory Management

Protection schemes in memory management refer to mechanisms that ensure


the safe execution of processes by preventing unauthorized access or
modifications to memory. These schemes are crucial for maintaining system
stability, security, and ensuring that processes do not interfere with each other.

Key Concepts of Protection Schemes

1.​ Segmentation:
○​ Memory is divided into segments, each with its own base and limit.
○​ Each segment can correspond to different parts of a program, such
as code, data, and stack segments.
○​ The base address specifies the starting address of the segment, and
the limit specifies the length of the segment.
2.​ Paging:
○​ Memory is divided into fixed-size pages and the corresponding
frames.
○​ Logical addresses are mapped to physical addresses using a page
table.
○​ Ensures that each page can be independently protected.
3.​ Access Control:
○​ Specifies the types of operations that are permissible on a memory
segment or page (e.g., read, write, execute).
○​ Access rights can be set for different segments/pages to control
which processes can read, write, or execute the data.
4.​ Privilege Levels:
○​ Operating systems often use privilege levels to protect critical
system resources.
○​ Processes are executed at different privilege levels, such as user
mode and kernel mode.
○​ Access to certain memory areas or instructions is restricted based
on the privilege level.
5.​ Hardware Support:
○​ Modern CPUs provide hardware support for memory protection
using features like Memory Management Units (MMUs), which help
translate logical addresses to physical addresses and enforce
access control.

Types of Protection Schemes

1.​ Segmentation-Based Protection:


○​ Uses segment tables to store the base and limit for each segment.
○​ Segments can be assigned access rights such as read, write, and
execute permissions.
○​ Helps isolate different parts of a program and protect them from
each other.
2.​ Paging-Based Protection:
○​ Each page can have different access rights set in the page table.
○​ Commonly used in virtual memory systems.
○​ Provides fine-grained control over memory access and helps prevent
unauthorized access.
3.​ Segmentation with Paging:
○​ Combines segmentation and paging to leverage the benefits of both
schemes.
○​ Segments are divided into pages, and both segment and page
tables are used.
○​ Enhances flexibility and protection capabilities.
4.​ Access Control Lists (ACLs):
○​ Used to specify detailed access permissions for different users or
processes.
○​ Each memory segment or page can have an associated ACL that
defines the allowed operations.
○​ Provides a high level of granularity and control over memory access.

Paging: ​
Paging is a method or technique which is used for non-contiguous memory
allocation. It is a fixed-size partitioning theme (scheme). In paging, both main
memory and secondary memory are divided into equal fixed-size partitions.
The partitions of the secondary memory area unit and main memory area unit
are known as pages and frames respectively.

Paging is a memory management method accustomed to fetch processes


from the secondary memory into the main memory in the form of pages. In
paging, each process is split into parts wherever the size of every part is the
same as the page size. The size of the last half could also be but the page
size. The pages of the process area unit hold on within the frames of main
memory relying upon their accessibility.

Example:

●​ If Logical Address = 31 bits, then Logical Address Space = 231

words = 2 G words (1 G = 230)

●​ If Logical Address Space = 128 M words = 27 * 220 words, then

Logical Address = log2 227 = 27 bits

●​ If Physical Address = 22 bits, then Physical Address Space = 222

words = 4 M words (1 M = 220)


●​ If Physical Address Space = 16 M words = 24 * 220 words, then

Physical Address = log2 224 = 24 bits

The mapping from virtual to physical address is done by the memory


management unit (MMU) which is a hardware device and this mapping is
known as the paging technique.

●​ The Physical Address Space is conceptually divided into several

fixed-size blocks, called frames.

●​ The Logical Address Space is also split into fixed-size blocks, called

pages.

●​ Page Size = Frame Size

Let us consider an example:

●​ Physical Address = 12 bits, then Physical Address Space = 4 K

words

●​ Logical Address = 13 bits, then Logical Address Space = 8 K words

●​ Page size = frame size = 1 K words (assumption)


Paging

The 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.

Physical Address is divided into:

●​ Frame Number(f): Number of bits required to represent the frame of

Physical Address Space or Frame number frame


●​ 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 hardware implementation of the page table can be done by using

dedicated registers. But the usage of the register for the page table is

satisfactory only if the page table is small. If the page table contains a large

number of entries then we can use TLB(translation Look-aside buffer), a

special, small, fast look-up hardware cache.

●​ The TLB is an associative, high-speed memory.

●​ Each entry in TLB consists of two parts: a tag and a value.

●​ When this memory is used, then an item is compared with all tags

simultaneously. If the item is found, then the corresponding value is

returned.
Segmentation: ​
Segmentation is another non-contiguous memory allocation scheme like
paging. like paging, in segmentation, the process isn’t divided
indiscriminately into mounted(fixed) size pages. It is a variable-size
partitioning theme. like paging, in segmentation, secondary and main
memory are not divided into partitions of equal size. The partitions of
secondary memory area units are known as segments. The details
concerning every segment are hold in a table known as segmentation table.
Segment table contains two main data concerning segments, one is Base,
which is the bottom address of the segment and another is Limit, which is the
length of the segment.

In segmentation, the CPU generates a logical address that contains the


Segment number and segment offset. If the segment offset is a smaller
amount than the limit then the address is called valid address otherwise it
throws a miscalculation because the address is invalid.
Paging Segmentation

In paging, the program is In segmentation, the


1. divided into fixed or program is divided into
mounted size pages. variable size sections.

For the paging operating For segmentation compiler is


2.
system is accountable. accountable.

Page size is determined by Here, the section size is given


3.
hardware. by the user.

It is faster in comparison to
4. Segmentation is slow.
segmentation.
Paging could result in Segmentation could result in
5.
internal fragmentation. external fragmentation.

In paging, the logical Here, the logical address is


6. address is split into a page split into section number and
number and page offset. section offset.

While segmentation also


Paging comprises a page
comprises the segment table
7. table that encloses the base
which encloses segment
address of every page.
number and segment offset.

The page table is employed Section Table maintains the


8.
to keep up the page data. section data.

In segmentation, the
In paging, the operating
operating system maintains a
9. system must maintain a free
list of holes in the main
frame list.
memory.
Paging is invisible to the Segmentation is visible to
10.
user. the user.

In paging, the processor In segmentation, the


needs the page number, and processor uses segment
11.
offset to calculate the number, and offset to
absolute address. calculate the full address.

It is hard to allow sharing of Facilitates sharing of


12. procedures between procedures between the
processes. processes.

In paging, a programmer
It can efficiently handle data
13 cannot efficiently handle
structures.
data structure.

This protection is hard to Easy to apply for protection


14.
apply. in segmentation.
The size of the page needs
There is no constraint on the
15. always be equal to the size
size of segments.
of frames.

A page is referred to as a A segment is referred to as a


16.
physical unit of information. logical unit of information.

Paged Segmentation is a hybrid memory management scheme that combines


the benefits of both paging and segmentation techniques. It addresses some of
the limitations of pure segmentation and pure paging by introducing a two-level
hierarchy in memory management. Here's an explanation of how paged
segmentation works and its key concepts:

Key Concepts of Paged Segmentation

1.​ Two-Level Address Translation:


○​ Segmentation: Memory is divided into variable-sized logical
segments (e.g., code segment, data segment).
○​ Paging: Each segment is further divided into fixed-size pages.
2.​ Segment Table:
○​ Each process has its own segment table, which contains entries for
each segment of the process.
○​ Each segment table entry points to a page table for that segment.
3.​ Page Table:
○​ For each segment, there is a corresponding page table that maps
logical pages within the segment to physical frames in memory.
○​ The page table contains entries with mappings for each page of the
segment.
4.​ Address Translation:
○​ When a process generates a logical address, the CPU uses the
segment table to locate the segment base address.
○​ From the segment base address, the CPU then uses the page table
to translate the logical page number to a physical frame number.

Virtual Memory

Virtual memory is a memory management technique used by operating


systems to give the appearance of a large, continuous block of memory to
applications, even if the physical memory (RAM) is limited. It allows the
system to compensate for physical memory shortages, enabling larger
applications to run on systems with less RAM.

A memory hierarchy, consisting of a computer system’s memory and a disk,


enables a process to operate with only some portions of its address space in
memory. A virtual memory is what its name indicates- it is an illusion of a
memory that is larger than the real memory. We refer to the software
component of virtual memory as a virtual memory manager. The basis of
virtual memory is the noncontiguous memory allocation model. The virtual
memory manager removes some components from memory to make room for
other components.

The size of virtual storage is limited by the addressing scheme of the


computer system and the amount of secondary memory available, not by the
actual number of main storage locations.

Key Concepts of Virtual Memory

1.​ Address Spaces:


○​ Logical Address: Generated by the CPU, it refers to a location in
virtual memory.
○​ Physical Address: The actual location in physical memory (RAM)
where data is stored.
2.​ Address Translation:
○​ Virtual memory uses hardware and software mechanisms to
translate logical addresses to physical addresses.
○​ This translation allows programs to operate as if they have
contiguous (linear) memory addresses, even though physical
memory is fragmented or limited.
3.​ Page Faults:
○​ When a program references a page that is not currently in
physical memory (RAM), a page fault occurs.
○​ The operating system handles page faults by fetching the
required page from secondary storage (usually disk) into RAM.
4.​ Demand Paging:
○​ Only the portions of a program or data that are actively used
(demanded) are loaded into physical memory.
○​ Reduces the amount of physical memory required to run
programs, as not all program data needs to be loaded at once.
5.​ Page Replacement Algorithms:
○​ When physical memory is full, and a new page needs to be
brought in, the operating system selects a page to evict from
physical memory.
○​ Common algorithms include Least Recently Used (LRU),
First-In-First-Out (FIFO), and Optimal Page Replacement.
6.​ Backing Store:
○​ Secondary storage (such as a hard disk) serves as the backing
store for virtual memory.
○​ Pages that are not currently in physical memory are stored here
and are swapped in and out as needed.
7.​ Protection:
○​ Virtual memory systems enforce memory protection by assigning
access permissions (read, write, execute) to pages.
○​ Unauthorized accesses result in exceptions (e.g., segmentation
faults) to protect system integrity.

Feature Virtual Memory Physical Memory (RAM)

An abstraction that The actual hardware (RAM)


extends the available that stores data and
Definition
memory by using disk instructions currently being
storage used by the CPU

On the hard drive or On the computer’s


Location
SSD motherboard

Slower (due to disk I/O Faster (accessed directly by


Speed
operations) the CPU)

Larger, limited by disk Smaller, limited by the amount


Capacity
space of RAM installed
Lower (cost of additional
Cost Higher (cost of RAM modules)
disk storage)

Data Indirect (via paging and Direct (CPU can access data
Access swapping) directly)

Non-volatile (data Volatile (data is lost when


Volatility
persists on disk) power is off)

A page replacement algorithm is a technique used by operating systems to


decide which memory pages to swap out (evict) from physical memory
(RAM) to make room for incoming pages. This is necessary when physical
memory becomes full and a new page needs to be brought in from disk. The
goal of page replacement algorithms is to minimize the number of page
faults (instances where a required page is not in memory) and optimize
system performance by efficiently managing memory resources. Here are
some commonly used page replacement algorithms:

Common Page Replacement Algorithms

1.​ Optimal Page Replacement (OPT or MIN):


○​ Principle: Replaces the page that will not be used for the longest
period of time in the future.
○​ Implementation: Ideal but impractical because it requires
knowledge of future page references, which is usually not
feasible.
2.​ Least Recently Used (LRU):
○​ Principle: Replaces the page that has not been used for the
longest period of time.
○​ Implementation: Requires keeping track of the order of page
accesses. This can be done using a hardware counter, stack, or
clock algorithm (used in software).
3.​ First-In-First-Out (FIFO):
○​ Principle: Replaces the page that has been in memory the
longest.
○​ Implementation: Uses a queue structure to track the order in
which pages were brought into memory. The oldest page in the
queue is replaced when a page fault occurs.
4.​ Clock (or Second-Chance):
○​ Principle: Improves upon FIFO by checking if the oldest page in
memory (pointed to by a clock hand) has been referenced
recently (second chance).
○​ Implementation: Uses a circular list or array of page frames. The
clock hand moves sequentially, and when a page fault occurs, the
clock hand searches for the first page that hasn't been referenced
recently (i.e., referenced bit is 0).
5.​ Least Frequently Used (LFU):
○​ Principle: Replaces the page with the smallest count of
references.
○​ Implementation: Requires maintaining a counter for each page to
track the number of times it has been referenced. Pages with the
lowest count are candidates for replacement.
6.​ Most Recently Used (MRU):
○​ Principle: Replaces the page that has been most recently
accessed.
○​ Implementation: Less common than LRU because it may not
always be the best predictor of future behavior.

Factors Affecting Page Replacement Algorithms

●​ Page-Fault Rate: How often page faults occur influences the effectiveness of different
algorithms.
●​ Memory Access Pattern: Algorithms perform differently based on whether programs
exhibit temporal locality (repeated access to the same pages) or spatial locality (access
to nearby pages).
●​ Implementation Complexity: Some algorithms, like LRU, require more complex data
structures and mechanisms to track page access history.

Thrashing

Thrashing is a condition in a computer system where excessive paging operations occur,


causing the system to spend more time swapping pages in and out of physical memory than
executing actual tasks. This leads to a significant decline in system performance and CPU
utilization, as the CPU remains busy handling page faults and swapping pages rather than
executing processes.
Cause: Thrashing typically occurs when a process is allocated insufficient frames of physical
memory, leading to frequent page faults.

Effect: The operating system must constantly swap pages between physical memory and disk,
which consumes significant CPU and I/O resources.

Impact:

CPU Utilization: Drastically reduced as the CPU spends most of its time managing page faults
and swapping.

System Performance: Severely degraded, as processes spend more time waiting for required
pages to be loaded rather than performing useful computation.

Long-Term Scheduler Response: In an attempt to improve CPU utilization, the long-term


scheduler might load more processes into memory, increasing the degree of multiprogramming.
However, this exacerbates the problem, leading to even more page faults and further reducing
CPU utilization.

Key Concepts

Page Fault: An event that occurs when a process tries to access a page not currently in
physical memory.

Swapping: The process of moving pages between physical memory and disk storage.

Frames: Fixed-size blocks of physical memory into which pages are loaded.

Degree of Multiprogramming: The number of processes in memory at a given time.

Cache Memory Organization

Cache memory is a small, high-speed storage layer located close to the CPU that stores
frequently accessed data and instructions. Its primary purpose is to reduce the time it takes to
access data from the main memory (RAM) by exploiting the principle of locality (temporal and
spatial locality). Effective cache memory organization is crucial for improving overall system
performance.

Key Concepts

1.​ Levels of Cache:


○​ L1 Cache: Smallest, fastest, and closest to the CPU cores. Typically divided into
separate instruction and data caches (Harvard architecture).
○​ L2 Cache: Larger than L1 but slower. May be shared among cores in multi-core
processors.
○​ L3 Cache: Even larger and slower than L2. Usually shared among all cores in a
CPU.
2.​ Cache Mapping Techniques:
○​ Direct-Mapped Cache: Each block of main memory maps to exactly one cache
line. Simple and fast but can lead to high conflict misses.
○​ Set-Associative Cache: Memory blocks map to a set of cache lines. Each set
contains multiple lines (associativity). Balances complexity and performance.
○​ Fully Associative Cache: Any memory block can be placed in any cache line.
Offers the lowest miss rate but is complex and slow to search.
3.​ Cache Replacement Policies:
○​ Least Recently Used (LRU): Replaces the least recently accessed cache line.
Tracks usage history to make replacement decisions.
○​ First-In-First-Out (FIFO): Replaces the oldest cache line in a set. Simple to
implement but may not be optimal.
○​ Random Replacement: Replaces a randomly selected cache line. Easy to
implement but may lead to suboptimal performance.
4.​ Cache Write Policies:
○​ Write-Through: Data is written to both the cache and the main memory
simultaneously. Ensures data consistency but may be slower.
○​ Write-Back: Data is written to the cache only, and the write to main memory is
deferred until the cache line is evicted. Reduces write latency but requires
tracking of modified (dirty) lines.

Cache Organization

1.​ Cache Line (Block): The smallest unit of data that can be stored in a cache. Typical
sizes range from 32 to 128 bytes.
2.​ Tag: Part of the cache that stores the metadata (address) of the cached memory block.
Used to identify and validate the data in a cache line.
3.​ Index: Part of the address that determines which cache set a block maps to (in
set-associative and direct-mapped caches).
4.​ Offset: Part of the address that specifies the exact byte within a cache line.

Cache Operations

1.​ Cache Hit: When the data requested by the CPU is found in the cache. Results in fast
data retrieval.
2.​ Cache Miss: When the data is not found in the cache, necessitating retrieval from main
memory. Types of cache misses include:
○​ Compulsory Miss: The first access to a block of data.
○​ Capacity Miss: Cache cannot contain all the data needed by the CPU due to its
limited size.
○​ Conflict Miss: Multiple memory blocks map to the same cache line (in
direct-mapped or set-associative caches).

Examples of Cache Organizations

1.​ Direct-Mapped Cache Example:


○​ A cache with 1024 lines and a block size of 64 bytes.
○​ Memory address: 0x12345678.
○​ Index: bits [11:6] (determines the cache line).
○​ Tag: bits [31:12] (identifies the memory block).
○​ Offset: bits [5:0] (specifies the byte within the block).
2.​ 4-Way Set-Associative Cache Example:
○​ A cache with 256 sets, 4 lines per set, and a block size of 64 bytes.
○​ Memory address: 0x12345678.
○​ Index: bits [13:6] (determines the set).
○​ Tag: bits [31:14] (identifies the memory block).
○​ Offset: bits [5:0] (specifies the byte within the block).

Cache Memory Organization

Cache memory is a small, high-speed storage layer located close to the CPU that stores
frequently accessed data and instructions. Its primary purpose is to reduce the time it takes to
access data from the main memory (RAM) by exploiting the principle of locality (temporal and
spatial locality). Effective cache memory organization is crucial for improving overall system
performance.

Characteristics of Cache Memory


●​ Cache memory is an extremely fast memory type that acts as a

buffer between RAM and the CPU.

●​ Cache Memory holds frequently requested data and instructions so

that they are immediately available to the CPU when needed.

●​ Cache memory is costlier than main memory or disk memory but

more economical than CPU registers.

●​ Cache Memory is used to speed up and synchronize with a

high-speed CPU.

Key Concepts

1.​ Levels of Cache:


○​ L1 Cache: Smallest, fastest, and closest to the CPU cores. Typically divided into
separate instruction and data caches (Harvard architecture).
○​ L2 Cache: Larger than L1 but slower. May be shared among cores in multi-core
processors.
○​ L3 Cache: Even larger and slower than L2. Usually shared among all cores in a
CPU.
2.​ Cache Mapping Techniques:
○​ Direct-Mapped Cache: Each block of main memory maps to exactly one cache
line. Simple and fast but can lead to high conflict misses.
○​ Set-Associative Cache: Memory blocks map to a set of cache lines. Each set
contains multiple lines (associativity). Balances complexity and performance.
○​ Fully Associative Cache: Any memory block can be placed in any cache line.
Offers the lowest miss rate but is complex and slow to search.
3.​ Cache Replacement Policies:
○​ Least Recently Used (LRU): Replaces the least recently accessed cache line.
Tracks usage history to make replacement decisions.
○​ First-In-First-Out (FIFO): Replaces the oldest cache line in a set. Simple to
implement but may not be optimal.
○​ Random Replacement: Replaces a randomly selected cache line. Easy to
implement but may lead to suboptimal performance.
4.​ Cache Write Policies:
○​ Write-Through: Data is written to both the cache and the main memory
simultaneously. Ensures data consistency but may be slower.
○​ Write-Back: Data is written to the cache only, and the write to main memory is
deferred until the cache line is evicted. Reduces write latency but requires
tracking of modified (dirty) lines.

Cache Organization

1.​ Cache Line (Block): The smallest unit of data that can be stored in a cache. Typical
sizes range from 32 to 128 bytes.
2.​ Tag: Part of the cache that stores the metadata (address) of the cached memory block.
Used to identify and validate the data in a cache line.
3.​ Index: Part of the address that determines which cache set a block maps to (in
set-associative and direct-mapped caches).
4.​ Offset: Part of the address that specifies the exact byte within a cache line.

Cache Operations

1.​ Cache Hit: When the data requested by the CPU is found in the cache. Results in fast
data retrieval.
2.​ Cache Miss: When the data is not found in the cache, necessitating retrieval from main
memory. Types of cache misses include:
○​ Compulsory Miss: The first access to a block of data.
○​ Capacity Miss: Cache cannot contain all the data needed by the CPU due to its
limited size.
○​ Conflict Miss: Multiple memory blocks map to the same cache line (in
direct-mapped or set-associative caches).

Examples of Cache Organizations

1.​ Direct-Mapped Cache Example:


○​ A cache with 1024 lines and a block size of 64 bytes.
○​ Memory address: 0x12345678.
○​ Index: bits [11:6] (determines the cache line).
○​ Tag: bits [31:12] (identifies the memory block).
○​ Offset: bits [5:0] (specifies the byte within the block).
2.​ 4-Way Set-Associative Cache Example:
○​ A cache with 256 sets, 4 lines per set, and a block size of 64 bytes.
○​ Memory address: 0x12345678.
○​ Index: bits [13:6] (determines the set).
○​ Tag: bits [31:14] (identifies the memory block).
○​ Offset: bits [5:0] (specifies the byte within the block).

Locality of Reference
The concept of locality of reference refers to the tendency of programs to access a relatively
small portion of their address space at any given time. This principle is fundamental to the
design and effectiveness of cache memory, as it allows the system to predict which data and
instructions will be needed soon and to keep those in faster, smaller storage closer to the CPU.

There are two main types of locality:

1.​ Temporal Locality:


○​ Definition: Temporal locality occurs when recently accessed data or instructions
are likely to be accessed again in the near future.
○​ Example: A loop in a program repeatedly accesses the same set of instructions
and data. Once an instruction or piece of data is brought into the cache, it is likely
to be used repeatedly before it is evicted.
2.​ Spatial Locality:
○​ Definition: Spatial locality occurs when data or instructions located near each
other in memory are likely to be accessed within a close time frame.
○​ Example: Accessing elements of an array sequentially. When one element is
accessed, the elements near it are also likely to be accessed soon. Bringing a
block of memory (a cache line) that includes the desired element and nearby
elements helps exploit this locality.

Importance of Locality of Reference

Locality of reference is crucial for the design of efficient memory hierarchies, including caches,
virtual memory systems, and prefetching mechanisms. Here are some reasons why:

1.​ Cache Efficiency:


○​ Caches are designed to take advantage of locality by storing recently accessed
and nearby data. This reduces the need to access slower main memory.
○​ Temporal locality ensures that once data is loaded into the cache, it will be used
multiple times before being replaced.
○​ Spatial locality ensures that when a block of memory is loaded, multiple
accesses can be satisfied by the same cache line.
2.​ Performance Optimization:
○​ Programs that exhibit strong locality of reference tend to run faster because they
make better use of the cache, reducing the frequency of expensive main memory
accesses.
○​ Compilers and programmers can optimize code to improve locality, such as by
rearranging data structures and instructions to enhance spatial and temporal
locality.
3.​ Memory Management:
○​ Operating systems use locality to make decisions about paging and swapping.
Pages that exhibit high locality are kept in physical memory to minimize page
faults.
○​ Virtual memory systems leverage locality to predict which pages will be accessed
soon, improving the efficiency of paging algorithms.

Examples of Locality of Reference

1.​ Loops in Programs:


○​ Temporal locality: Instructions within a loop are repeatedly executed.
○​ Spatial locality: Data elements in arrays or lists accessed within loops.
2.​ Function Calls:
○​ Temporal locality: Functions called multiple times will have their instructions
fetched into the cache.
○​ Spatial locality: Local variables and instructions within a function are stored close
to each other in memory.
3.​ Data Structures:
○​ Arrays: Sequential access to array elements exhibits spatial locality.
○​ Linked Lists: Access patterns can be optimized for spatial locality by allocating
nodes close to each other.

Improving Locality of Reference

1.​ Code Optimization:


○​ Loop transformations (e.g., loop unrolling, loop fusion) to improve temporal and
spatial locality.
○​ Inlining functions to reduce the overhead of function calls and improve temporal
locality.
2.​ Data Structure Design:
○​ Use contiguous memory allocations (e.g., arrays instead of linked lists) to
enhance spatial locality.
○​ Organize data structures to keep frequently accessed data together.
3.​ Memory Allocation Strategies:
○​ Allocate memory in a way that enhances spatial locality (e.g., using memory
pools for related objects).
○​ Use cache-aware and cache-oblivious algorithms that inherently exploit locality.
Unit 5
I/O Management and Disk Scheduling
I/O Buffering

I/O buffering is a technique used by operating systems to manage data


transfer between different devices, such as the CPU, memory, and
peripherals, ensuring efficient and smooth data handling. It acts as a
temporary storage area that holds data while it is being transferred between
two places. Here are some common I/O buffering techniques:

1. Single Buffering

In single buffering, a single buffer is used for I/O operations. When data is
read from an input device, it is stored in this buffer before being processed by
the CPU. Similarly, data to be written to an output device is first placed in this
buffer.

●​ Advantages:
○​ Simple to implement.
○​ Provides some level of decoupling between I/O device speed and
CPU processing speed.
●​ Disadvantages:
○​ Inefficient if the I/O device and CPU have significantly different
speeds.
○​ The CPU might have to wait if the buffer is full.

2. Double Buffering

Double buffering uses two buffers instead of one. While one buffer is being
used for data transfer, the other can be filled or emptied simultaneously. This
technique helps overlap the computation and I/O operations, improving
performance.

●​ Advantages:
○​ Reduces the waiting time for the CPU and I/O devices.
○​ Increases the overall efficiency of I/O operations.
●​ Disadvantages:
○​ Requires more memory for the additional buffer.
○​ Slightly more complex to implement than single buffering.

3. Circular Buffering (Ring Buffer)

A circular buffer, or ring buffer, uses a fixed-size buffer that is treated as if it


were connected end-to-end. This allows the buffer to be used efficiently
without needing to move data around.

●​ Advantages:
○​ Efficient use of buffer space.
○​ Suitable for continuous data streams, like audio or video.
●​ Disadvantages:
○​ More complex to manage compared to single or double buffering.
○​ Potential for buffer overflow if the producer is much faster than the
consumer.

4. Buffer Pooling

Buffer pooling involves maintaining a pool of buffers that can be dynamically


allocated to different I/O operations as needed. This allows for flexible and
efficient use of memory resources.
●​ Advantages:
○​ Reduces memory fragmentation.
○​ Improves system responsiveness under varying loads.
●​ Disadvantages:
○​ Increased complexity in buffer management.
○​ Potential for memory waste if buffers are not efficiently recycled.

5. Spooling (Simultaneous Peripheral Operations On-Line)

Spooling is used for managing output devices like printers. Instead of sending
data directly to the printer, it is first written to a spool file on the disk. The
printer can then read from this spool file at its own pace.

●​ Advantages:
○​ Allows multiple print jobs to be queued and processed
sequentially.
○​ Decouples the printing process from the application's execution.
●​ Disadvantages:
○​ Requires disk space for the spool files.
○​ Additional overhead in managing the spool files.

Importance of I/O Buffering

●​ Performance Improvement: Buffering helps to balance the speed


difference between the CPU and I/O devices, preventing the CPU from
being idle while waiting for I/O operations to complete.
●​ Efficient Resource Utilization: By overlapping computation and I/O,
buffering techniques make better use of system resources.
●​ Data Integrity and Reliability: Buffers can help in error detection and
correction, ensuring that data is transferred correctly.
●​ Smooth Data Flow: Buffers can accommodate bursts of data and
smooth out irregularities in data flow, providing a more consistent and
reliable system performance.

Disk scheduling- Disk scheduling is a technique used by operating systems


to decide the order in which disk I/O (input/output) requests are processed.
This process is also called I/O Scheduling. The main goals of disk scheduling
are to improve the performance of disk operations, reduce the time to access
data, and enhance overall system efficiency.

What are Disk Scheduling Algorithms?

Disk scheduling algorithms manage how data is read from and written to a
computer’s hard disk. They determine the order of disk read and write
requests, impacting data access speed and efficiency. Common methods
include First-Come, First-Served (FCFS), Shortest Seek Time First (SSTF),
SCAN, C-SCAN, LOOK, and C-LOOK.

Importance of Disk Scheduling in Operating System

●​ Multiple I/O requests may arrive by different processes and only one
I/O request can be served at a time by the disk controller. Thus other
I/O requests need to wait in the waiting queue and need to be
scheduled.
●​ Two or more requests may be far from each other so this can result
in greater disk arm movement.
●​ Hard drives are one of the slowest parts of the computer system and
thus need to be accessed in an efficient manner.

Key Terms Associated with Disk Scheduling

●​ Seek Time: Seek time is the time taken to locate the disk arm to a
specified track where the data is to be read or written. So the disk
scheduling algorithm that gives a minimum average seek time is
better.
●​ Rotational Latency: Rotational Latency is the time taken by the
desired sector of the disk to rotate into a position so that it can
access the read/write heads. So the disk scheduling algorithm that
gives minimum rotational latency is better.
●​ Transfer Time: Transfer time is the time to transfer the data. It
depends on the rotating speed of the disk and the number of bytes to
be transferred.
●​ Disk Access Time:

Disk Access Time = Seek Time + Rotational Latency + Transfer Time

Total Seek Time = Total head Movement * Seek Time


Disk Access Time and Disk Response Time

●​ Disk Response Time: Response Time is the average time spent by


a request waiting to perform its I/O operation. The average
Response time is the response time of all requests. Variance
Response Time is the measure of how individual requests are
serviced with respect to average response time. So the disk
scheduling algorithm that gives minimum variance response time is
better.

Goal of Disk Scheduling Algorithms

●​ Minimize Seek Time


●​ Maximize Throughput
●​ Minimize Latency
●​ Fairness
●​ Efficiency in Resource Utilization

Disk Scheduling Algorithms

1. FCFS (First Come First Serve)


FCFS is the simplest of all Disk Scheduling Algorithms. In FCFS, the requests
are addressed in the order they arrive in the disk queue. Let us understand
this with the help of an example.

First Come First Serve

Example:

Suppose the order of request is- (82,170,43,140,24,16,190)​


And current position of Read/Write head is: 50

So, total overhead movement (total distance covered by the disk arm) =

(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16) =642

Advantages of FCFS
Here are some of the advantages of First Come First Serve.

●​ Every request gets a fair chance


●​ No indefinite postponement

Disadvantages of FCFS

Here are some of the disadvantages of First Come First Serve.

●​ Does not try to optimize seek time


●​ May not provide the best possible service

2. SSTF (Shortest Seek Time First)

In SSTF (Shortest Seek Time First), requests having the shortest seek time
are executed first. So, the seek time of every request is calculated in advance
in the queue and then they are scheduled according to their calculated seek
time. As a result, the request near the disk arm will get executed first. SSTF is
certainly an improvement over FCFS as it decreases the average response
time and increases the throughput of the system. Let us understand this with
the help of an example.

Example:
Shortest Seek Time First

Suppose the order of request is- (82,170,43,140,24,16,190)​


And current position of Read/Write head is: 50

So,

total overhead movement (total distance covered by the disk


arm) =

(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170)
=208

Advantages of Shortest Seek Time First

Here are some of the advantages of Shortest Seek Time First.

●​ The average Response Time decreases


●​ Throughput increases

Disadvantages of Shortest Seek Time First

Here are some of the disadvantages of Shortest Seek Time First.


●​ Overhead to calculate seek time in advance
●​ Can cause Starvation for a request if it has a higher seek time as
compared to incoming requests
●​ The high variance of response time as SSTF favors only some
requests

3. SCAN

In the SCAN algorithm the disk arm moves in a particular direction and
services the requests coming in its path and after reaching the end of the disk,
it reverses its direction and again services the request arriving in its path. So,
this algorithm works as an elevator and is hence also known as an elevator
algorithm. As a result, the requests at the midrange are serviced more and
those arriving behind the disk arm will have to wait.

Example:
SCAN Algorithm

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the


Read/Write arm is at 50, and it is also given that the disk arm should move
“towards the larger value”.

Therefore, the total overhead movement (total distance covered by the disk
arm) is calculated as

= (199-50) + (199-16) = 332

Advantages of SCAN Algorithm

Here are some of the advantages of the SCAN Algorithm.

●​ High throughput
●​ Low variance of response time
●​ Average response time

Disadvantages of SCAN Algorithm

Here are some of the disadvantages of the SCAN Algorithm.

●​ Long waiting time for requests for locations just visited by disk arm

4. C-SCAN

In the SCAN algorithm, the disk arm again scans the path that has been
scanned, after reversing its direction. So, it may be possible that too many
requests are waiting at the other end or there may be zero or few requests
pending at the scanned area.
These situations are avoided in the CSCAN algorithm in which the disk arm
instead of reversing its direction goes to the other end of the disk and starts
servicing the requests from there. So, the disk arm moves in a circular fashion
and this algorithm is also similar to the SCAN algorithm hence it is known as
C-SCAN (Circular SCAN).

Example:

Circular SCAN

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the


Read/Write arm is at 50, and it is also given that the disk arm should move
“towards the larger value”.

So, the total overhead movement (total distance covered by the disk arm) is
calculated as:
=(199-50) + (199-0) + (43-0) = 391

Advantages of C-SCAN Algorithm

Here are some of the advantages of C-SCAN.

●​ Provides more uniform wait time compared to SCAN.

5. LOOK

LOOK Algorithm is similar to the SCAN disk scheduling algorithm except for
the difference that the disk arm in spite of going to the end of the disk goes
only to the last request to be serviced in front of the head and then reverses
its direction from there only. Thus it prevents the extra delay which occurred
due to unnecessary traversal to the end of the disk.

Example:
LOOK Algorithm

Suppose the requests to be addressed are- 82,170,43,140,24,16,190. And the


Read/Write arm is at 50, and it is also given that the disk arm should move
“towards the larger value”.

So, the total overhead movement (total distance covered by the disk arm) is
calculated as:

= (190-50) + (190-16) = 314

6. C-LOOK

As LOOK is similar to the SCAN algorithm, in a similar way, C-LOOK is similar


to the CSCAN disk scheduling algorithm. In C-LOOK, the disk arm in spite of
going to the end goes only to the last request to be serviced in front of the
head and then from there goes to the other end’s last request. Thus, it also
prevents the extra delay which occurred due to unnecessary traversal to the
end of the disk.

Example:

1.​ Suppose the requests to be addressed are-82,170,43,140,24,16,190.


And the Read/Write arm is at 50, and it is also given that the disk arm
should move “towards the larger value”
C-LOOK

So, the total overhead movement (total distance covered by the disk arm) is
calculated as

= (190-50) + (190-16) + (43-16) = 341.

RAID (Redundant Arrays of Independent Disks)

RAID (Redundant Arrays of Independent Disks) is a technique that makes use


of a combination of multiple disks for storing the data instead of using a single
disk for increased performance, data redundancy, or to protect data in the
case of a drive failure.
What is RAID?

RAID (Redundant Array of Independent Disks) is like having backup copies of


your important files stored in different places on several hard drives or
solid-state drives (SSDs). If one drive stops working, your data is still safe
because you have other copies stored on the other drives. It’s like having a
safety net to protect your files from being lost if one of your drives breaks
down.

RAID (Redundant Array of Independent Disks) in a Database Management


System (DBMS) is a technology that combines multiple physical disk drives
into a single logical unit for data storage. The main purpose of RAID is to
improve data reliability, availability, and performance. There are different
levels of RAID, each offering a balance of these benefits.

RAID Working

Consider a scenario where you want to keep a favorite book safe among a
group of friends. Instead of giving the book to one friend, you make copies
and distribute pieces to each friend. If one friend loses their piece, you can still
reassemble the book from the other pieces. RAID works similarly by
distributing data across multiple drives, ensuring data safety even if one drive
fails.

What is a RAID Controller?

A RAID controller is like a boss for your hard drives in a big storage system. It
works between your computer’s operating system and the actual hard drives,
organizing them into groups to make them easier to manage. This helps
speed up how fast your computer can read and write data, and it also adds a
layer of protection in case one of your hard drives breaks down. So, it’s like
having a smart helper that makes your hard drives work better and keeps your
important data safer.

Data Redundancy

Data redundancy, while requiring additional space, enhances disk reliability. In


the event of a disk failure, having data backed up on another disk allows for
data recovery, preventing operation disruption. Without RAID, a single disk
failure could compromise the entire data set.

Key Evaluation Points for a RAID System

●​ Reliability: How many disk faults can the system tolerate?


●​ Availability: What fraction of the total session time is a system in
uptime mode, i.e. how available is the system for actual use?
●​ Performance: How good is the response time? How high is the
throughput (rate of processing work)? Note that performance
contains a lot of parameters, not just the two.

Different RAID Levels

RAID-0 (Striping)

Data is split across multiple disks, enhancing performance but offering no data
redundancy. If one drive fails, data is lost.
Advantages:

●​ Easy to implement
●​ Efficient use of storage capacity

Disadvantages:

●​ Complete data loss if a single drive fails


●​ Not suitable for critical systems

RAID-1 (Mirroring)

Each block of data is stored on two or more disks, providing redundancy.


Advantages:

●​ High data security and speed


●​ Complete redundancy

Disadvantages:

●​ Expensive
●​ Reduced storage capacity

RAID-2 (Bit-Level Striping with Dedicated Parity)

Uses Hamming Code for error checking at the bit level, requiring a complex
structure and extra drive.

Advantages:

●​ Error correction with Hamming Code


●​ Designated parity drive
Disadvantages:

●​ Complex and costly


●​ Requires an extra drive

RAID-3 (Byte-Level Striping with Dedicated Parity)

Byte-level striping with dedicated parity, allowing data recovery using the parity
drive.

Advantages:

●​ Bulk data transfer


●​ Parallel data access

Disadvantages:

●​ Requires an additional parity drive


●​ Slow performance with small files

RAID-4 (Block-Level Striping with Dedicated Parity)

Adopts a parity-based approach, storing parity in one disk for data recovery.

Advantages:

●​ Data recovery for a single disk failure

Disadvantages:

●​ Cannot recover if more than one disk fails

RAID-5 (Block-Level Striping with Distributed Parity)

RAID 5 requires the use of at least three drives. It combines these disks to protect data
against loss of any one disk; the array's storage capacity is reduced by one disk. It strips
data across multiple drives to increase performance. But, it also adds the aspect of
redundancy by distributing parity information across the disks.
Advantages:

●​ Better performance
●​ Data recovery using parity bits

Disadvantages:

●​ Complex and requires extra space


●​ Data loss if multiple disks fail

RAID-6 (Block-Level Striping with Two Parity Bits)

RAID 6 is similar to RAID 5, but the parity data are written to two drives. The use of
additional parity enables the array to continue to function even if two disks fail
simultaneously. However, this extra protection comes at a cost. RAID 6 has a slower
write performance than RAID 5.
Advantages:

●​ High data accessibility


●​ Fast read transactions

Disadvantages:

●​ Slow write transactions due to double parity


●​ Requires extra space

Advantages of RAID

●​ Data Redundancy: Protects data from disk failures.


●​ Performance Enhancement: Allows simultaneous read/write operations.
●​ Scalability: Easily expand storage capacity.
●​ Versatility: Applicable to various devices.

Disadvantages of RAID

●​ Cost: Expensive, especially for large arrays.


●​ Complexity: Challenging to set up and manage.
●​ Decreased Performance: Parity calculations can slow down operations.
●​ Single Point of Failure: Not a complete backup solution; controller failure
can lead to data loss.

File Systems
A computer file is a digital medium used for storing and managing data in a
computer system. Files come in various types, allowing for efficient data
storage and management. A file system is a fundamental component of any
operating system, providing a structured way to store, organize, and manage
data on storage devices like hard drives, SSDs, and USB drives. It acts as a
bridge between the operating system and physical storage hardware, enabling
users and applications to create, read, update, and delete files in an
organized and efficient manner.

What is a File System?

A file system is a method used by an operating system to store, organize, and


manage files and directories on a storage device. Some common types of file
systems include:

FAT (File Allocation Table): An older file system used by early versions
of Windows and other operating systems.

NTFS (New Technology File System): A modern file system used by


Windows, supporting features like file and folder permissions,
compression, and encryption.
ext (Extended File System): A file system commonly used on Linux
and Unix-based operating systems.

HFS (Hierarchical File System): A file system used by macOS.

APFS (Apple File System): A newer file system introduced by Apple


for their Macs and iOS devices.

A file is a collection of related information that is recorded on secondary


storage. Or file is a collection of logically related entities. From the user’s
perspective, a file is the smallest allotment of logical secondary storage.

File Naming

A file name is divided into two parts:

●​ Name: The primary identifier of the file.


●​ Extension: Indicates the file type, separated by a period (e.g., .txt, .jpg).

Issues Handled by File Systems

File systems are responsible for keeping files organized efficiently. They handle
issues such as:

●​ Free Space Management: Reclaiming space from deleted files to allocate it


to new files.
●​ File Storage Location: Determining where to store files on the disk, which
can involve non-contiguous block allocation.
●​ Tracking File Locations: Keeping track of all the blocks where parts of a file
are stored.

File Directories

The collection of files is a file directory. The directory contains information


about the files, including attributes, location, and ownership. Much of this
information, especially that is concerned with storage, is managed by the
operating system. The directory is itself a file, accessible by various file
management routines.

Below is information contained in a device directory.

●​ Name
●​ Type
●​ Address
●​ Current length
●​ Maximum length
●​ Date last accessed
●​ Date last updated
●​ Owner id
●​ Protection information

The operation performed on the directory are:

●​ Search for a file


●​ Create a file
●​ Delete a file
●​ List a directory
●​ Rename a file
●​ Traverse the file system

Advantages of Maintaining Directories

●​ Efficiency: A file can be located more quickly.


●​ Naming: It becomes convenient for users as two users can have
same name for different files or may have different name for same
file.
●​ Grouping: Logical grouping of files can be done by properties e.g.
all java programs, all games etc.

Single-Level Directory

In this, a single directory is maintained for all the users.

●​ Naming Problem: Users cannot have the same name for two files.
●​ Grouping Problem: Users cannot group files according to their
needs.

Two-Level Directory

In this separate directories for each user is maintained.

●​ Path Name: Due to two levels there is a path name for every file to
locate that file.
●​ Now, we can have the same file name for different users.
●​ Searching is efficient in this method.

Tree-Structured Directory
The directory is maintained in the form of a tree. Searching is efficient and
also there is grouping capability. We have an absolute or relative path name
for a file.

File Allocation Methods

1.​ Contiguous Allocation: Allocates a single continuous block of space to a


file.
○​ Disadvantages: External fragmentation and need to pre-declare
file size.
2.​ Linked Allocation: Allocates blocks as needed, with each block
pointing to the next.
○​ Disadvantages: Internal fragmentation, overhead of maintaining
pointers, risk of pointer loss, and support only for sequential
access.
3.​ Indexed Allocation: Uses a separate index for each file, supporting
both sequential and direct access.
○​ Advantages: Eliminates external fragmentation and supports
efficient access.

Disk Free Space Management

Managing unallocated disk space is crucial for effective file allocation.


Methods include:

●​ Bit Tables: Uses a vector with bits representing free or occupied blocks.
●​ Free Block List: Maintains a list of free block numbers.

Advantages of File Systems


●​ Organization: Facilitates file organization into directories and
subdirectories.
●​ Data Protection: Includes features like permissions, backups, and error
correction.
●​ Improved Performance: Efficient data read/write operations.

Disadvantages of File Systems

●​ Compatibility Issues: Different file systems may not be compatible,


hindering data transfer.
●​ Disk Space Overhead: Uses disk space for metadata and overhead
information.
●​ Vulnerability: Susceptible to data corruption, malware, and security
threats.

Advantages of File System

●​ Organization: A file system allows files to be organized into


directories and subdirectories, making it easier to manage and locate
files.
●​ Data Protection: File systems often include features such as file and
folder permissions, backup and restore, and error detection and
correction, to protect data from loss or corruption.
●​ Improved Performance: A well-designed file system can improve
the performance of reading and writing data by organizing it
efficiently on disk.

Disadvantages of File System


●​ Compatibility Issues: Different file systems may not be compatible
with each other, making it difficult to transfer data between different
operating systems.
●​ Disk Space Overhead: File systems may use some disk space to
store metadata and other overhead information, reducing the amount
of space available for user data.
●​ Vulnerability: File systems can be vulnerable to data corruption,
malware, and other security threats, which can compromise the
stability and security of the system.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy