notes (2)
notes (2)
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.
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);
}
}
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.
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.
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
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 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.
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.
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.
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.
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.
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.
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 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.
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.
Multilevel Queue Scheduling: Divides processes into different queues with varying priorities.
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:
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.
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).
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.
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.
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:
Process Scheduling
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-
Context Switching
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.
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.
● 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.
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.
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.
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.
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
Disadvantages of Threads
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.
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. 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
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
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.
Given Data
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:
| P1 | P3 | P2 |P5 | P4 | P6 |
0 9 11 14 17 21 23
Detailed Breakdown
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
Advantages:
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) 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
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
Gantt Chart
| P1 | | P3 | | P6 | | P2 | | P5 | | P4 | | P1 |
0 1 3 5 8 11 14 18 23
Detailed Breakdown
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
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.
Given Data
Let's assume we have the following processes with their respective priorities
(lower value means higher priority):
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
Detailed Breakdown
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
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.
Given Data
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
Execution Order
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
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
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:
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?
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.
Deadlock detection involves checking if any processes are stuck waiting for each
other indefinitely. Several algorithms can help detect deadlocks, such as:
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:
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.
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.
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
2. Loader
3. Device Driver
4. Interrupt Processing
Types of Addresses
Dynamic Loading
Dynamic Linking
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.
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.
Advantages:
Disadvantages:
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.
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.
Example:
● The Logical Address Space is also split into fixed-size blocks, called
pages.
words
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
● When this memory is used, then an item is compared with all tags
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.
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 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, a programmer
It can efficiently handle data
13 cannot efficiently handle
structures.
data structure.
Virtual Memory
Data Indirect (via paging and Direct (CPU can access data
Access swapping) directly)
● 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
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.
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.
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
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).
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.
high-speed CPU.
Key Concepts
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).
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.
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. 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.
● 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
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.
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.
● 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.
● 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:
Example:
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.
Disadvantages of FCFS
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
So,
(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170)
=208
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
Therefore, the total overhead movement (total distance covered by the disk
arm) is calculated as
● High throughput
● Low variance of response time
● Average response time
● 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
So, the total overhead movement (total distance covered by the disk arm) is
calculated as:
=(199-50) + (199-0) + (43-0) = 391
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
So, the total overhead movement (total distance covered by the disk arm) is
calculated as:
6. C-LOOK
Example:
So, the total overhead movement (total distance covered by the disk arm) is
calculated as
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.
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
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:
RAID-1 (Mirroring)
Disadvantages:
● Expensive
● Reduced storage capacity
Uses Hamming Code for error checking at the bit level, requiring a complex
structure and extra drive.
Advantages:
Byte-level striping with dedicated parity, allowing data recovery using the parity
drive.
Advantages:
Disadvantages:
Adopts a parity-based approach, storing parity in one disk for data recovery.
Advantages:
Disadvantages:
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:
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:
Disadvantages:
Advantages of RAID
Disadvantages of RAID
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.
FAT (File Allocation Table): An older file system used by early versions
of Windows and other operating systems.
File Naming
File systems are responsible for keeping files organized efficiently. They handle
issues such as:
File Directories
● Name
● Type
● Address
● Current length
● Maximum length
● Date last accessed
● Date last updated
● Owner id
● Protection information
Single-Level Directory
● Naming Problem: Users cannot have the same name for two files.
● Grouping Problem: Users cannot group files according to their
needs.
Two-Level Directory
● 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.
● Bit Tables: Uses a vector with bits representing free or occupied blocks.
● Free Block List: Maintains a list of free block numbers.