DMA Controller (Direct Memory Access Controller)
A DMA (Direct Memory Access) controller is a hardware component that allows certain devices
(like hard drives, sound cards, or network cards) to transfer data directly to or from memory
without involving the CPU for every byte of data.
Needs of DMA
Without DMA:
The CPU must manage every data transfer between I/O devices and memory.
This leads to CPU overhead and inefficient performance, especially for large data transfers.
With DMA:
Devices can bypass the CPU and communicate directly with memory.
The CPU is freed up to perform other tasks during the transfer.
How DMA Works
1. CPU initializes the DMA controller:
o Provides source address
o Destination address
o Number of bytes to transfer
2. DMA controller takes control of the bus:
o It pauses the CPU's access to memory during the transfer.
3. DMA transfers data between I/O device and memory.
4. DMA sends an interrupt to CPU when the transfer is complete.
5.
Modes of DMA Transfer
Mode Description
Burst Mode Transfers a large block of data in one go; CPU is locked out during the burst.
DMA takes control of the bus for one cycle at a time, allowing CPU and DMA to
Cycle Stealing
share the bus.
Mode Description
Transparent DMA only transfers data when the CPU is not using the system bus (least
Mode interference).
Key Components of DMA Controller
Component Function
Address Register Holds the source/destination memory address.
Counter Register Holds the number of bytes to transfer.
Control Register Specifies direction (read/write), mode, etc.
Data Bus Access Logic Controls access to system bus during transfer.
Popular DMA Controller Example
Intel 8237: Common DMA controller used in older PC systems.
Benefits of DMA
Reduces CPU load
Speeds up data transfer
Improves overall system performance
Disadvantages / Challenges
Adds hardware complexity
Needs careful synchronization (may cause bus conflicts)
Improper use can lead to data corruption
Concurrent Process
In operating systems, concurrent processes are processes that exist and execute during the same
time period. They may or may not actually run at the exact same moment, depending on the
system (single-core or multi-core), but they are conceptually progressing together.
Concurrency vs. Parallelism
Concurrency: Multiple processes are in progress simultaneously but not necessarily
executing at the same instant.
Parallelism: Multiple processes or threads execute at the same time on multiple processors or
cores.
Why Use Concurrent Processes?
To increase efficiency by overlapping I/O and CPU operations.
To handle multiple tasks (e.g., web servers handling many requests).
To improve responsiveness in interactive systems.
Key Concepts in Concurrency
1. Process: An independent program in execution with its own memory space.
2. Thread: A lightweight subprocess, shares memory space with other threads of the same
process.
3. Context Switching: The OS switches the CPU from one process to another, giving the
illusion of parallelism.
4. Synchronization: Mechanisms (like mutexes, semaphores) to manage access to shared
resources.
5. Critical Section: Code section where shared resources are accessed — needs protection to
avoid data inconsistency.
6. Race Condition: A situation where the output depends on the sequence/timing of
uncontrollable events.
Techniques to Handle Concurrency
Mutual Exclusion: Ensures only one process accesses a critical section at a time.
Semaphores and Monitors: Used to control process synchronization.
Inter-process Communication (IPC): Mechanisms like message passing, shared memory.
Deadlock Handling: Techniques to avoid or recover from situations where processes wait
indefinitely for resources.
Examples of Concurrent Systems
Multi-user operating systems like Linux or Windows.
Web servers handling multiple client connections.
Real-time systems like flight control software.
Real vs. Virtual Concurrency
Real Concurrency (True Concurrency)
Definition:
Real concurrency occurs when multiple processes or threads execute simultaneously on
different CPU cores.
Where it Happens:
In multi-core or multiprocessor systems.
Key Characteristics:
Each process/thread runs at the same time on separate cores.
Offers true parallelism.
More efficient for CPU-bound operations.
Requires hardware support (multiple processors or cores).
Example:
On a quad-core CPU, four threads of a video rendering program running on all four cores
simultaneously.
Virtual Concurrency (Pseudo-Concurrency)
Definition:
Virtual concurrency occurs when a single CPU switches between processes so quickly that it
gives the illusion of simultaneous execution.
Where it Happens:
In single-core systems (or in multi-core systems with more processes than cores).
Key Characteristics:
Achieved via context switching by the operating system.
Only one process executes at any given time, but switching is so fast that users perceive
them as running together.
Heavily dependent on OS scheduling algorithms.
Example:
On a single-core machine, a web browser, music player, and text editor appear to run at the
same time, but the CPU is rapidly switching between them.
synchronization
In an operating system, process synchronization ensures the coordinated and orderly execution of
multiple concurrent processes or threads, preventing data inconsistencies, race conditions, and
deadlocks. It's crucial for managing shared resources safely and efficiently in multi-process and multi-
threaded environments.
Synchronization in an operating system ensures that multiple processes or threads can execute
concurrently without interfering with each other, especially when they share resources like memory,
files, or devices.
Condition/rules for creating synchronization solution
Mutual Exclusion – No two processes are in the critical section at the same time.
Progress – If no one is in the critical section, one of the waiting processes should be allowed
to enter.
Bounded Waiting – A limit on how long a process waits before entering its critical section.
No Assumptions on Speed/Number of CPUs – It must work regardless of execution speed
or number of processors.
Why is synchronization needed?
Shared Resources:
Processes often need to access and modify shared data or resources like files, memory, and databases.
Concurrency:
Multiple processes might try to access these shared resources concurrently, leading to potential
conflicts and issues.
Data Inconsistency:
Without synchronization, one process might modify data while another is reading or using it, resulting
in incorrect or corrupted data.
Race Conditions:
The outcome of concurrent execution can depend on the order in which instructions are executed,
leading to unpredictable results.
Deadlocks:
Processes can get stuck waiting for resources held by other processes, creating a standstill.
Mutual Exclusion
Mutual Exclusion in an operating system is a fundamental concept used to prevent concurrent
processes from accessing shared resources (like variables, files, or printers) simultaneously, which
could lead to data inconsistency or corruption.
Mutual exclusion ensures that only one process or thread can access a critical section (a part of the
code where the shared resource is accessed) at a time.
Why Is It Needed?
Consider two processes trying to update a shared variable at the same time. Without mutual exclusion,
the result could be incorrect due to race conditions.
Critical section
A critical section is a part of a program where the process accesses shared resources. To avoid
inconsistencies (like data corruption), only one process should be allowed to execute in the critical
section at a time.
The critical section includes operations on shared variables or resources that must be executed
atomically to maintain data consistency. For example, reading from or writing to a shared file or
modifying a global variable requires exclusive access.
Structure of a Critical Section
Entry Section
The process requests permission to enter the critical section.
Synchronization tools (e.g., mutex, semaphore) are used to control access.
Critical Section
The actual code where shared resources are accessed or modified.
Exit Section
The process releases the lock or semaphore, allowing other processes to enter the critical
section.
Remainder Section
The rest of the program that does not involve shared resource access.
Critical Section Structure
Characteristics Of Critical Section
There are some properties that should be followed by any code in the critical section :
1. Mutual Exclusion
Only one process or thread can execute in the critical section at a given time. If two or more processes
access shared resources (like variables or files) at the same time without control, data inconsistency or
corruption may occur. If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
Example:
If two threads update the same bank account balance at the same time, the final result may be
incorrect due to race conditions.
We can achieve it by : Using synchronization tools like mutexes, locks, or semaphores to ensure
exclusive access.
2. Progress
If no process is executing in its critical section and some processes wish to enter their critical sections,
then only those processes that are not executing in their remainder sections can participate in deciding
which will enter its critical section next, and this selection cannot be postponed indefinitely.
It's important to avoid idle CPU cycles or deadlock situations where all processes are waiting, even
when they don’t have to enter.
Example:
If process A finishes its work and leaves the critical section, process B (waiting to enter) should not be
delayed unnecessarily due to faulty design or logic in the algorithm.
Goal: To ensure that the system continues to make progress and doesn't freeze or hang.
3. Bounded Waiting
There must be a limit on how many times other processes are allowed to enter the critical section
before a waiting process gets its turn. It is important to prevent starvation, where one process waits
indefinitely while others repeatedly enter the critical section.
Example:
If process A is always skipped in favor of processes B and C, then A might never enter, even if it's
ready.
Solution: Implement fair scheduling policies like FIFO queues or ticket-based systems.
Handling Critical Section
Two general approaches are used to handle critical sections :
1. Preemptive Kernels : A preemptive kernel allows the operating system to interrupt or preempt a
process even when it is running in kernel mode.
The OS can forcibly switch from one running process to another.
Even if a process is performing a system call or executing kernel code, it can be paused, and
another process can be scheduled.
Advantages:
Better responsiveness, especially for real-time systems.
Higher CPU utilization and fairness among processes.
Disadvantages:
Increased complexity due to the need to manage race conditions and data consistency when
kernel data structures are accessed by multiple processes.
Example Use Case: Modern desktop and server operating systems like Linux, Windows, and macOS
use preemptive kernels for better multitasking.
2. Non-Preemptive Kernels : A non-preemptive kernel does not allow interruption of a process that
is running in kernel mode. The CPU control is explicitly released by the process. The kernel ensures
that only one process is active in the kernel at any given time. The process continues until it:
Exits the kernel,
Blocks (e.g., waits for I/O), or
Voluntarily yields the CPU.
Advantages:
Simplicity: Easier to program and maintain.
No race conditions on kernel data since access is automatically serialized.
Disadvantages:
Poor responsiveness, especially if a long-running kernel operation delays other processes.
Not suitable for real-time or interactive systems.
Example Use Case: Older operating systems or embedded systems where simplicity and reliability
outweigh responsiveness.