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

OS3 RD Sem

Operational settings system

Uploaded by

ibrahimiliyas.ki
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)
17 views

OS3 RD Sem

Operational settings system

Uploaded by

ibrahimiliyas.ki
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/ 27

System Call Interface

System Call Interface:


The system-call interface connects user-level applications to the operating system's kernel, allowing
controlled access to hardware and resources. When a program uses an API function that requires OS
resources (e.g., open()), the interface intercepts this call, assigns it a unique ID, and invokes the
corresponding system call in the kernel. The OS then executes the request in kernel mode, returns the
outcome, and any data back to the application.
Types of System Calls
System calls are generally grouped into several main categories based on their functionality:
Process Control: Create, terminate, and manage processes. Examples include fork(), exec(), and wait().
File Manipulation: Handle file operations, such as opening, reading, writing, and closing files. Examples
include open(), read(), write(), and close().
Device Manipulation: Interact with devices by requesting and releasing devices or performing I/O
operations. Examples include ioctl() for control operations.
Information Maintenance: Get and set system data such as time, date, and system parameters. Examples
include getpid() for process IDs and gettimeofday() for time.
Communication: Manage inter-process communication, either between processes on the same machine or
over a network. Examples include pipe() for IPC and socket() for network communication.
Protection: Control access to resources and manage permissions. Examples include chmod() to change file
permissions and setuid() for setting user IDs.

Functions of Operation System An Operating System acts as a communication bridge (interface) between the
user and computer hardware. The purpose of an operating system is to provide a platform on which a user
can execute programs in a convenient and efficient manner

Processor Management: Manages CPU resources for processes (programs in execution). The OS schedules
processes, creates and deletes processes, suspends/resumes processes, and provides mechanisms for
process synchronization and communication.
Memory Management: Manages main memory by tracking which parts are in use, deciding which processes
to load, and allocating/deallocating memory as needed to improve CPU utilization and response time.
Storage Management:
File System Management: Manages file storage by creating/deleting files, organizing files in directories, and
controlling file access permissions.
Mass-Storage Management: Manages secondary storage (disk), including free-space management, storage
allocation, and disk scheduling.
Caching: Uses high-speed cache memory to temporarily store frequently accessed data to speed up
processing.
Protection and Security: Ensures that only authorized users and processes access resources, controls access
to files and memory, and protects against external and internal threats like malware and unauthorized
access.
Device Management: Manages device communication through drivers, tracks connected devices, allocates
devices to processes, and deallocates them as needed.
Performance Monitoring: Monitors system performance, tracks response times, and provides data to
troubleshoot and improve performance.
Software and User Coordination: Coordinates various programs (interpreters, compilers, assemblers) and
assigns them to users, ensuring efficient use of system resources.

Scheduling Algorithms
CPU scheduling determines the order in which processes in the ready queue are allocated the CPU. Below
are various scheduling algorithms with their key characteristics:

a) First-Come, First-Served (FCFS)


Description: Processes are executed in the order they arrive using a FIFO queue.
Advantages: Simple and easy to implement.
Disadvantages:
Long average waiting time.
Non-preemptive: unsuitable for time-sharing systems.
Can lead to convoy effect (long processes block shorter ones).

b) Shortest-Job-First (SJF)
Description: Allocates the CPU to the process with the shortest burst time.
Variants:
Non-preemptive: Current process completes before switching.
Preemptive (Shortest Remaining Time First): Switches to new processes with shorter bursts.
Advantages:
Minimizes average waiting time (optimal).
Disadvantages:
Requires knowledge of future CPU burst lengths.
Potential for starvation of long processes.

c) Priority Scheduling
Description: Allocates CPU to the process with the highest priority.
Preemptive: Preempts running processes for higher-priority arrivals.
Non-preemptive: Higher-priority processes wait for the current process to finish.
Advantages: Handles processes based on importance.
Disadvantages:
Risk of starvation for lower-priority processes.
Solution: Aging (gradually increasing priority of waiting processes).

d) Round-Robin (RR)
Description: Designed for time-sharing systems. Processes are executed in circular order with a fixed time
quantum.
Advantages:
Fair distribution of CPU time.
Preemptive: improves responsiveness.
Disadvantages:
Long average waiting time for short bursts if the quantum is large.
High overhead if the quantum is too small.

e) Multilevel Queue Scheduling


Description: Divides the ready queue into multiple queues based on process characteristics (e.g., priority,
memory). Each queue has its own scheduling algorithm.
Advantages:
Tailored scheduling for different process types.
Disadvantages:
Fixed-priority queues can lead to starvation of lower-priority processes.

f) Multilevel Feedback Queue Scheduling


Description: Similar to multilevel queue scheduling but allows processes to move between queues based on
behavior (e.g., CPU-bound vs. I/O-bound).
Advantages:
Prevents starvation using aging.
Adapts to varying process requirements.
Disadvantages:
Complex to implement.
Parameters:
Number of queues.
Scheduling algorithm for each queue.
Rules for upgrading or demoting processes.
Process Termination
Definition:
A process terminates when it has completed execution or is explicitly stopped. Upon termination:
The operating system deallocates its resources (e.g., memory, files, and I/O buffers).
A termination status may be returned to the parent process using the exit() system call.
Normal Termination
The process finishes executing its final statement and invokes exit().
The process can pass a status value to its parent via the wait() system call.

Abnormal Termination
A process can also be terminated in other scenarios, such as:
By a System Call:
A process can terminate another process using specific system calls (e.g., TerminateProcess() in Win32).
Typically restricted to the parent process for security reasons.
By the Parent Process:
A parent can terminate its child process under these conditions:
Resource Overuse: The child exceeds allocated resources.
Task Redundancy: The child’s task is no longer needed.
Parent Exit: Some operating systems disallow child processes to continue if their parent terminates.

Cascading Termination
In systems like VMS, when a parent process terminates (either normally or abnormally), all its child
processes are also terminated automatically.
This behavior, known as cascading termination, is typically managed by the operating system.

Key Points:
Process Identity: The parent process is always aware of its child processes’ identities, which are established
when the child is created.
Resource Cleanup: The operating system ensures all resources held by a terminated process are freed to
avoid resource leaks.
Controlled Termination: Restrictions on termination rights help prevent unauthorized interference among
user processes.

Process Creation
Definition:
A process can create new processes during its execution using a create-process system call.
Parent Process: The creating process.
Child Process: The newly created process.

Hierarchy and Identification


Processes can form a tree-like structure where the parent spawns multiple children, which in turn can spawn
their own children.
Most operating systems assign a unique process identifier (PID) to each process (e.g., UNIX, Windows).

Resource Allocation
Child processes need resources such as CPU time, memory, files, or I/O devices to execute.
Resource management options:
The operating system directly allocates resources to the child.
The child receives a subset of the parent's resources.
Resources may be shared between parent and child (e.g., shared memory or files).
Purpose of constraints: To prevent system overload caused by excessive subprocess creation.

Execution Possibilities
When a process creates a child, execution behavior can differ:
Concurrent Execution: Parent and child execute simultaneously.
Sequential Execution: Parent waits until the child process finishes.

Address Space
The child's address space can be configured in two ways:
Duplicate of the Parent: Child inherits the parent’s program and data.
New Program: A completely new program is loaded into the child’s memory space.
UNIX Process Creation Example
In UNIX, process creation occurs via the fork() system call:
What fork() does:
Creates a copy of the parent process’s address space for the child process.
Both parent and child execute from the instruction immediately after the fork() call.
Return Codes:
Child process: fork() returns 0.
Parent process: fork() returns the PID of the child.

Typical Workflow in UNIX


fork(): Creates the child process.
exec(): Replaces the child’s memory space with a new program.
This allows the child to execute a completely different program.
wait():
The parent process may call this to pause until the child process finishes execution.
exit():
Both parent and child processes terminate using this call, deallocating resources.

Parent-Child Synchronization
Communication:
Since the child starts as a copy of the parent, they can easily share data initially.
Lifecycle Management:
The parent may create additional children or remain idle until its children finish execution.

Process Scheduling
Objectives:
Multiprogramming: Maximize CPU utilization by ensuring a process is always running.
Time Sharing: Rapidly switch processes to allow interactive user experiences.

Scheduling Queues:
Job Queue: All processes in the system.
Ready Queue: Processes in memory, ready to execute (linked list representation).
Device Queues: Processes waiting for specific I/O devices.
Process Flow:
A new process enters the ready queue.
During execution, the process may:
Request I/O (moves to I/O queue).
Create a subprocess (waits for completion).
Be interrupted (returns to the ready queue).
The process continues this cycle until termination, after which it is removed and resources are freed.

Types of Schedulers:
Long-Term Scheduler (Job Scheduler):
Selects processes from the job queue for loading into memory.
Runs infrequently, balancing CPU-bound and I/O-bound processes.
Short-Term Scheduler (CPU Scheduler):
Selects processes from the ready queue for CPU execution.
Runs frequently (every 100 ms); speed is critical.
Medium-Term Scheduler:
Temporarily removes processes (swapping) to adjust multiprogramming or free memory.
Swapped processes can be resumed later.

Context Switch:
Definition: Transitioning CPU control between processes.
Components of Context: CPU registers, process state, memory info.
Procedure:
Save the current process's context in its PCB (Process Control Block).
Load the next process's context from its PCB.
Overhead: Involves a few milliseconds with no productive output.

Key Concepts:
I/O-Bound Processes: More time on I/O; less on computation.
CPU-Bound Processes: More time on computation; less on I/O.
Swapping: Temporary removal of processes to optimize resource allocation.
Inter-Process Communication (IPC)
Independent vs. Cooperating Processes
Independent Process: Cannot affect or be affected by others; no shared data.
Cooperating Process: Can affect or be affected by others; shares data.
Reasons for Process Cooperation
Information Sharing: Access shared resources like files.
Computation Speedup: Parallel processing for faster execution (requires multiple CPUs/I/O channels).
Modularity: Structure systems into modular processes or threads.
Convenience: Enable multitasking (e.g., editing, printing, compiling simultaneously).

IPC Mechanisms
1. Shared Memory
Concept: Processes exchange data via a shared memory region.
Advantages: Faster communication (treated as regular memory access).
Challenges: Requires synchronization to prevent conflicts (e.g., simultaneous writes).
Example: Producer-Consumer Problem
Buffer Types:
Unbounded: No size limit; consumer may wait.
Bounded: Fixed size; producer/consumer may wait if full/empty.
Implementation: Circular array with in (next free position) and out (first full position).
2. Message Passing
Concept: Communication via message sending and receiving (no shared memory).
Advantages: Easier to implement in distributed systems.
Challenges: Slower (requires kernel intervention).
Primitives:
Send(message): Transfers a message.
Receive(message): Retrieves a message.
Message Sizes:
Fixed: Simplifies implementation but harder to program.
Variable: Easier to program but harder to implement.
Communication Methods:
Direct: Processes explicitly name each other.
One link per process pair; automatically established.
Indirect: Messages sent to shared mailboxes (ports).
Multiple processes can use a mailbox.

Synchronization in Message Passing


Blocking (Synchronous): Sender/receiver waits until the operation completes.
Non-blocking (Asynchronous): Sender/receiver proceeds without waiting.

Buffering Options
Zero Capacity: No queued messages; sender blocks until the recipient receives the message.
Bounded Capacity: Fixed-length queue; sender blocks if the queue is full.
Unbounded Capacity: Infinite queue; sender never blocks.
Deadlock Prevention
Deadlocks occur when processes are blocked indefinitely because each process is waiting for a resource held
by another in the set. To prevent deadlocks, address the four necessary conditions: Mutual Exclusion, Hold
and Wait, No Preemption, and Circular Wait.
1. Mutual Exclusion
Definition: Some resources (e.g., printers) are non-sharable, meaning only one process can use them at a
time.
Prevention:
Deadlocks cannot be prevented by eliminating mutual exclusion because certain resources are inherently
non-sharable.
Sharable resources (e.g., read-only files) do not cause deadlocks.

2. Hold and Wait


Definition: A process holding resources and waiting for others held by another process can cause deadlock.
Prevention Protocols:
Request All Resources at Once: Processes request and are allocated all necessary resources at the start of
execution.
Release and Re-request: A process releases held resources before requesting new ones.
Example:
A process needs a DVD drive, disk file, and printer.
First Protocol: Requests all three upfront, even if some are used later.
Second Protocol: Requests and uses the DVD drive and disk file, releases them, and then requests the printer.

3. No Preemption
Definition: Allocated resources cannot be preempted until the holding process finishes using them.
Prevention Protocol:
Preemption on Request Failure: If a process requests a resource that is unavailable, it releases all held
resources, which are reallocated later.
Resource Reallocation: The system may preempt resources held by other processes to satisfy a new request,
ensuring progress.

4. Circular Wait
Definition: Processes form a cycle, each waiting for a resource held by the next.
Prevention Protocol:
Resource Ordering: Assign unique numbers to resource types and require processes to request them in
increasing order.
Example: Resources {Tape drive, Disk drive, Printer} have numbers {1, 5, 12}.
A process must request the Tape drive before the Disk drive, and the Printer last.
Proof: A circular wait cannot occur since resource requests must follow the increasing order, avoiding cycles.
Deadlock Avoidance
Deadlock avoidance is a strategy where the system dynamically decides whether to grant resource requests
to ensure that the system does not enter a potentially deadlocked state. Unlike deadlock prevention, it does
not eliminate the conditions for deadlock but ensures they are never all true simultaneously.

Key Concepts of Deadlock Avoidance


1. Resource Request Information
The system requires advance knowledge of the order in which processes will request resources.
Example:
Process P: Requests the tape drive first, then the printer.
Process Q: Requests the printer first, then the tape drive.
This information helps the system predict potential resource conflicts.
2. Dynamic Decision Making
When a process requests a resource, the system decides whether granting the request will keep the system
in a safe state.
Decisions are based on:
Currently available resources.
Resources already allocated to processes.
Future requests and releases by processes.
3. Resource-Allocation State
The state is defined by:
Available Resources: Resources currently free.
Allocated Resources: Resources currently held by processes.
Maximum Demands: Total resources each process may require during execution.
The system uses this information to assess whether granting a resource request might lead to circular wait.

Deadlock-Avoidance Algorithm
The system analyzes the current allocation state to decide whether to grant a request:
Checks if the system remains in a safe state (no potential circular wait).
If granting the request might lead to deadlock, the system denies the request, forcing the process to wait.
Deadlock Detection
Deadlock detection is essential in systems that do not implement deadlock-prevention or
deadlock-avoidance mechanisms. It involves identifying deadlock situations and employing strategies to
recover from them. This requires both a detection algorithm and a recovery mechanism.

Key Concepts of Deadlock Detection


1. Deadlock Detection Algorithm
The system analyzes the resource allocation state to detect deadlocks.
Examines:
Resources allocated to processes.
Resources requested by processes.
Waiting conditions where processes are blocked by others.
2. Detection and Recovery Overhead
Tracking Information: Systems maintain data structures, such as a resource-allocation graph or wait-for
graph, to monitor the system's state.
Run-Time Costs: Executing the detection algorithm and recovering from deadlocks incurs overhead.

Types of Resource Allocation and Detection Algorithms


1. Single Instance of Each Resource Type
Wait-For Graph:
Derived from the resource-allocation graph by collapsing resource nodes and showing direct dependencies
between processes.
Edge: Pi→PjP_i \rightarrow P_jPi​→Pj​: Process PiP_iPi​is waiting for PjP_jPj​to release a resource.
Detection: A cycle in the wait-for graph indicates deadlock.
2. Multiple Instances of Each Resource Type
Requires a more complex detection algorithm.
Key Steps:
Use data structures to track allocated, requested, and available resources (similar to the Banker's algorithm).
Attempt to fulfill processes’ resource requests iteratively.
Deadlock Detection: Deadlock occurs if a cycle is formed, where no process can proceed due to insufficient
resources.
Example:
For processes P0P_0P0​to P4P_4P4​and resources A,B,A, B,A,B, and CCC, deadlock exists if resource
availability cannot satisfy process requests, leading to circular waiting.

Algorithm Usage Considerations


1. Frequency of Invocation
Frequent Detection: Necessary in systems prone to frequent deadlocks to minimize idle resources.
Infrequent Detection: Suitable if deadlocks are rare, reducing overhead.
2. Triggering Condition
The algorithm is activated when:
A resource request cannot be immediately granted.
The request may potentially complete a cycle in the resource-allocation graph.
Deadlock Detection
Deadlock detection is crucial when a system does not use deadlock-prevention or deadlock-avoidance
mechanisms. It involves both detecting deadlocks and recovering from them using dedicated algorithms.
Key Concepts of Deadlock Detection:
Deadlock Detection Algorithm:
This algorithm examines the current resource allocation state to detect deadlocks.
It analyzes the resources allocated to processes, their requests, and if processes are waiting for resources
held by others.
Detection and Recovery Overhead:
Deadlock detection requires additional data, such as a resource-allocation graph (or wait-for graph), to track
the system's state.
Running the detection algorithm incurs runtime costs, and recovering from deadlocks adds further overhead.
Types of Resource Allocation and Deadlock Detection:
Single Instance of Each Resource Type:
When each resource type has a single instance, a wait-for graph is used for detection.
This graph removes resource nodes and links processes that are waiting on each other. A cycle in the wait-for
graph indicates a deadlock.
Multiple Instances of Each Resource Type:
For systems with multiple instances of each resource type, a more complex detection algorithm is used,
similar to Banker's algorithm.
The system tracks allocations, requests, and available resources, attempting to fulfill resource requests. A
deadlock is detected if requests cannot be met and a cycle forms in the resource allocation.
Deadlock Detection Algorithm Usage:
Frequency of Invocation:
The algorithm should run more frequently if deadlocks are likely to occur. For rare deadlocks, detection can
be less frequent.
Impact of Deadlock:
Frequent detection is essential when many processes are affected by deadlocks, as this helps minimize idle
resources and resolve resource allocation issues quicker.
The detection algorithm triggers when a process’s request cannot be granted immediately, potentially
completing a cycle in the resource allocation graph.

Recovery from Deadlock


When a deadlock is detected, recovery can be performed manually by an operator or automatically by the
system. Automatic recovery involves either process termination or resource preemption.

Recovery Methods
1. Process Termination
Abort All Deadlocked Processes:
Terminates all processes involved in the deadlock.
Drawback: Costly as all progress is lost and must be recomputed.
Abort One Process at a Time:
Gradually resolves deadlock by terminating one process at a time.
Overhead: Requires running the deadlock detection algorithm after each termination.
Risks: May leave systems in inconsistent states (e.g., unfinished file updates).
Process Selection: Based on minimal termination cost, such as the least computation lost.

2. Resource Preemption
Preempt Resources:
Breaks deadlock by reallocating resources held by processes.
Key Considerations:
Selecting a Victim:
Choose processes/resources to preempt based on factors like the number of resources held or execution
time, minimizing disruption.
Rollback:
Total Rollback: Abort and restart the process.
Partial Rollback: Roll back to the last safe state, requiring detailed state tracking.
Starvation Prevention:
Ensure fairness so no process is perpetually denied resources.
Policies are needed to prevent continuous preemption of the same processes.
Synchronization Problems
1. The Bounded-Buffer Problem
Overview: Manage a pool of n buffers, each holding one item.
Semaphores Used:
Mutex: Ensures mutual exclusion during buffer access (initialized to 1).
Empty: Tracks the number of empty buffers (initialized to n).
Full: Tracks the number of full buffers (initialized to 0).
Processes:
Producer:
Waits for an empty buffer, produces an item, and updates the buffer.
Consumer:
Waits for a full buffer, consumes an item, and updates the buffer.
Goal: Prevent overproduction or under-consumption by synchronizing producer and consumer actions.

2. The Readers-Writers Problem


Overview: Controls access to a shared database where:
Multiple readers can read simultaneously.
Writers require exclusive access.
Variants:
First Readers-Writers Problem: Readers do not wait unless a writer is already writing.
Second Readers-Writers Problem: Writers have priority once they arrive.
Synchronization:
Mutex: Protects access to the readcount variable (tracks reader count).
wrt Semaphore: Ensures mutual exclusion for writers.
Processes:
Reader: Updates readcount and synchronizes entry/exit to/from the database.
Writer: Waits on wrt to gain exclusive access for writing.

3. The Dining-Philosophers Problem


Overview: Models five philosophers alternately thinking and eating using five chopsticks.
Each philosopher needs two chopsticks to eat.
Problem: Deadlock occurs if all philosophers pick up one chopstick simultaneously.
Solution Approaches:
Limit Philosophers: Reduce the number of philosophers at the table to four.
Critical Section: Ensure philosophers pick up both chopsticks within a single critical section.
Asymmetric Strategy:
Odd-numbered philosophers pick up the left chopstick first.
Even-numbered philosophers pick up the right chopstick first.
Semaphores: Overview
A semaphore is an abstract data type used for process synchronization in multitasking operating systems. It
helps control access to shared resources and ensures that processes are synchronized in concurrent
environments. A semaphore is represented by an integer variable and supports two key atomic operations:
wait() and signal().

Semaphore Operations
wait(S):
The process waits if the semaphore value is 0 or negative.
If the value is greater than 0, it decrements the semaphore and proceeds.
Ensures mutual exclusion by preventing other processes from entering their critical section.
signal(S):
Increments the semaphore value.
Wakes up a waiting process if any are waiting.
Both operations are atomic to prevent interference from other processes.

Types of Semaphores
Binary Semaphores (Mutex):
Values can only be 0 or 1.
Used to implement mutual exclusion (only one process can enter the critical section at a time).
Counting Semaphores:
Can hold any non-negative integer value.
Used to manage a pool of resources (e.g., printers or buffers).

Use Cases of Semaphores


Mutual Exclusion:
Used to solve the critical section problem. A binary semaphore ensures that only one process can access its
critical section at a time:
c
Copy code
wait(mutex); // Enter critical section
signal(mutex); // Exit critical section
Resource Allocation:
Counting semaphores control access to a fixed number of resources. A process requests a resource by calling
wait(), and releases it with signal(). If the semaphore is 0, processes are blocked until a resource is available.
Process Synchronization:
Semaphores can synchronize processes. For example, if Process 1 (P1) needs to execute before Process 2
(P2), they can use a shared semaphore:
c
Copy code
signal(synch); // In P1
wait(synch); // In P2

Busy Waiting vs. Blocking


Busy Waiting (Spin Locks):
A process in wait() might engage in busy waiting if the semaphore value is negative, wasting CPU cycles.
Blocking (No Busy Waiting):
To avoid busy waiting, processes block themselves by entering a waiting queue until another process signals
the semaphore. When a process signals, a waiting process is awakened.

Implementation with Blocking


Semaphore Structure:
c
Copy code
typedef struct {
int value;
struct process *list;
} semaphore;
Operations:
wait(semaphore *S): Decrements the semaphore. If the value is non-positive, the process is added to the
waiting list and blocked.
signal(semaphore *S): Increments the semaphore. If any process is waiting, it is removed from the list and
resumed.

Basic System Calls:


block(): Suspends the current process.
wakeup(P): Resumes a blocked process P.

Monitors are synchronization constructs used to manage access to shared resources, preventing errors such
as deadlocks and race conditions that can occur with semaphores. A monitor encapsulates both data and
methods to operate on that data, ensuring mutual exclusion by allowing only one process to execute any
procedure within the monitor at a time.
Monitor Overview:
Definition: A monitor is an abstract data type (ADT) that includes both data and procedures. It enforces
mutual exclusion by ensuring only one process executes any procedure at a time.
Syntax:
text
Copy code
monitor MonitorName {
// Shared variables
procedure P1 (...) { ... }
procedure P2 (...) { ... }
...
}
Condition Variables: Monitors use condition variables to handle complex synchronization needs:
wait(): Suspends the current process until another process signals it.
signal(): Resumes one waiting process, or does nothing if no process is waiting.
Dining Philosophers Problem (Example):
Monitors help solve problems like the Dining Philosophers by controlling access to shared resources (e.g.,
chopsticks):
text
Copy code
monitor DiningPhilosophers {
enum {THINKING, HUNGRY, EATING} state[5];
condition self[5];

void pickup(int i) { ... }


void putdown(int i) { ... }
void test(int i) { ... }
}
This implementation avoids conflicts and deadlocks by ensuring that no two philosophers eat at the same
time.
Semaphore-Based Implementation:
Monitors can be implemented with semaphores:
Mutex: A semaphore ensures mutual exclusion.
Condition Variables: Semaphores (x_sem) handle the suspension and resumption of processes.
Process Resumption: Resumption follows a First-Come-First-Served (FCFS) or priority-based scheme.
Challenges:
Common issues with monitors include:
Unauthorized Access: Processes accessing resources without permission.
Non-release of Resources: Processes failing to release resources.
Multiple Requests: Processes requesting resources multiple times without releasing them.
Careful synchronization within the monitor is required to handle these challenges properly.
Segmentation is a memory management scheme that divides a program’s logical address space into
variable-sized segments, providing a more flexible and modular view of memory. This scheme helps the user
see memory as a collection of logically distinct segments rather than a continuous block of physical memory.
Key Concepts:
User’s View of Memory: Memory is divided into segments, such as code, global variables, stack, and heap.
The logical addresses used by the program are mapped to physical memory through the segmentation
hardware.
Logical vs Physical Memory:
Logical memory: The address space seen by the user, consisting of multiple segments.
Physical memory: The actual memory locations in the system. The logical addresses are translated to
physical memory through a segment table.
Structure of a Logical Address:
A logical address consists of:
Segment number: Refers to the specific segment (e.g., code, data).
Offset: Specifies the position within the segment.
The segment table stores the physical memory location and size of each segment, which is used for address
translation.
Differences from Paging:
Paging: Uses fixed-size blocks (pages) and divides memory into a page number and offset.
Segmentation: Each segment can have a different size, and the user specifies a segment and an offset.
Common Types of Segments:
Code segment: Contains executable code.
Data segment: Holds global variables.
Heap segment: Used for dynamic memory allocation.
Stack segment: Used for local variables and function calls.
Library segments: Shared libraries used by programs.
Implementation:
The loader assigns segment numbers to different program parts. Logical addresses are mapped to physical
memory using the segment number and offset.
Key Benefits of Segmentation:
Modularity: Segments are managed independently, allowing flexibility.
Protection: Segmentation provides protection between segments (e.g., preventing stack and heap from
overwriting each other).
Dynamic Growth: Segments like the heap and stack can grow or shrink dynamically.
Segmentation allows more flexibility and efficiency in managing memory compared to paging, especially
when dealing with different program components.
Fragmentation in memory management refers to inefficiencies in memory allocation caused by free memory
being split into smaller, non-contiguous blocks. It occurs in two types:
1. External Fragmentation:
Definition: Occurs when free memory is scattered into small, non-contiguous blocks, making it difficult to
allocate large memory blocks, even if the total free memory is sufficient.
Impact: Small gaps between processes lead to wasted space, and even if enough memory exists, it may be
unusable due to fragmentation.
Example: In first-fit allocation, memory can be wasted up to 50%, and a significant portion may remain
unusable.
2. Internal Fragmentation:
Definition: Happens when memory is allocated in fixed-sized blocks, and the block size exceeds the
requested size, leaving unused space within the block.
Example: If a process requests 18,462 bytes but the partition is 18,464 bytes, the remaining 2 bytes are
wasted.
Solutions to Fragmentation:
Compaction:
Goal: To reduce external fragmentation by moving processes to consolidate free space into one large block.
Challenges: Compaction requires dynamic relocation, which is not feasible with static relocation.
Compaction Algorithm: Move processes towards one end of memory, creating a single large free space at the
other end. This is resource-intensive and time-consuming.
Paging:
Approach: Divides both logical address space and physical memory into fixed-size blocks (pages and frames).
A page can be stored anywhere in memory, eliminating external fragmentation.
Advantage: No external fragmentation, but potential internal fragmentation occurs when the last page is
partially used.
Segmentation:
Approach: Divides a process into variable-sized segments (e.g., code, data, stack), which are allocated
non-contiguously in physical memory.
Advantage: More flexible and natural memory allocation, reducing internal fragmentation. However, it still
faces external fragmentation due to variable segment sizes.
Key Takeaways:
External fragmentation occurs due to scattered free memory, while internal fragmentation happens when
fixed-sized blocks are allocated, leaving unused space.
Compaction helps reduce external fragmentation by consolidating free memory but requires dynamic
relocation.
Paging eliminates external fragmentation by using fixed-size pages but may introduce internal
fragmentation.
Segmentation allows for flexible allocation and reduces internal fragmentation but can still suffer from
external fragmentation.
Virtual memory is a technique that allows processes to run even if they don't fully fit in physical memory,
enabling programs to exceed the size of available RAM. It provides several key benefits for both the system
and the programmer.
Key Advantages:
Larger Programs: Programs can be larger than physical memory, allowing larger applications to run.
Abstraction: Virtual memory separates the user’s logical view of memory from the physical memory,
simplifying memory management.
Memory Flexibility: Programmers can focus on their application without worrying about memory limitations.
Process Sharing: It facilitates resource sharing between processes, including shared files and memory.
How Virtual Memory Works:
Memory Management Unit (MMU): The MMU maps logical addresses (used by processes) to physical
addresses (actual locations in memory).
Virtual Address Space: A process's memory is divided into segments (e.g., heap, stack, and data). The heap
grows upward, the stack grows downward, and gaps between them can be filled as needed.
Benefits of Virtual Memory:
Shared Libraries: Libraries can be shared across multiple processes, reducing memory usage. These libraries
are mapped as read-only in each process's virtual address space.
Shared Memory: Processes can share memory regions directly, facilitating inter-process communication (IPC)
without expensive I/O operations.
Process Creation Efficiency: Using the fork() system call, a child process can share pages with the parent
process, reducing the time and memory required for process creation.
Sparse Address Spaces:
Virtual memory uses sparse address spaces, leaving large gaps between segments (e.g., between the stack
and heap). These gaps are filled dynamically when needed, improving memory utilization.
Page Sharing:
Shared Libraries: Pages of libraries are mapped into each process’s virtual address space but are physically
shared between processes.
Shared Memory Regions: Processes can create shared memory regions for direct communication, which is
more efficient than using I/O.
Hardware Support for Paging
Paging is supported by hardware through a page table that maps virtual addresses to physical addresses.
There are different ways to implement the page table:
1. Page Table in Registers:
For small page tables (e.g., 256 entries), the page table can be stored in CPU registers for fast access.
However, this is not scalable for large tables.
2. Page Table in Main Memory:
For larger page tables (e.g., 1 million entries), the page table is stored in main memory, and the CPU uses the
Page Table Base Register (PTBR) to locate it. This requires two memory accesses:
One to access the page table entry.
One to access the actual data.
3. TLB (Translation Lookaside Buffer):
The TLB is a fast, small cache that stores the most recently used page table entries, reducing address
translation time.
TLB Hit: If the page number is found in the TLB, the corresponding frame number is retrieved quickly.
TLB Miss: If the page number is not in the TLB, a memory reference must be made to the page table, causing
additional delay.
If the TLB is full, the OS replaces an entry, often using policies like Least Recently Used (LRU).
4. TLB Hit Ratio:
The hit ratio indicates the percentage of times a page is found in the TLB. For example:
With an 80% hit ratio, a memory access takes 140 nanoseconds on average.
With a 98% hit ratio, the access time improves to 122 nanoseconds.
A higher hit ratio reduces memory access time.
5. Effective Memory Access Time (EAT):
EAT is the average memory access time, factoring in both TLB hits and misses. For example, with a 80% hit
ratio: EAT=(0.80×120 ns)+(0.20×220 ns)=140 ns\text{EAT} = (0.80 \times 120 \, \text{ns}) + (0.20 \times 220 \,
\text{ns}) = 140 \, \text{ns}EAT=(0.80×120ns)+(0.20×220ns)=140ns
A high hit ratio significantly improves memory access time.
Key Advantages of Paging:
Avoids External Fragmentation: By dividing memory into fixed-size pages, paging eliminates issues related to
non-contiguous memory.
Simplifies Memory Allocation: Paging makes memory allocation easier, as pages and frames are of equal size.
Evolution of OS
• In the early computers there were no Operating Systems.
• With the advancement of the commercial computer services we have come across a number of Operating
Systems software in 1960’s .
• Starting from the DOS, a lot much Operating Systems has got developed through out the stages like the
UNIX, Windows etc.
• The most commonly-used Operating Systems for laptops and modern Desktops Operating Systems are the
Microsoft Windows.

Early Evolution (1945–1957)


1945: ENIAC developed at the University of Pennsylvania.
1949: EDSAC and EDVAC introduced; BINAC succeeded ENIAC.
1951: UNIVAC by Remington launched.
1952: IBM 701 introduced.
1956: Interrupt mechanism developed.
1954–1957: Development of FORTRAN programming language.

Operating Systems in the Late 1950s


Operating systems advanced to support:
Batch Processing: Single-stream job execution.
Standardized I/O Routines: Simplified device access.
Program Transitioning: Reduced overhead for new jobs.
Error Recovery: Automated cleanup after job failures.
Job Control Languages: User-specified job definitions and resource allocation.

Operating Systems in the 1960s


1961: Introduction of minicomputers.
1962: CTSS (Compatible Time-Sharing System) by MIT.
1963: MCP for Burroughs B5000.
1964: IBM System/360 introduced; disks became mainstream.
1966: Affordable, powerful minicomputers gained popularity.
1967–1968: Mouse invented.
1964+: Development of Multics.
1969: UNIX Time-Sharing System by Bell Labs.

Key OS Features by the 1970s


Multi-user and multitasking capabilities.
Dynamic address translation and virtual machines.
Modular system architectures.
Emergence of personal, interactive computing systems.

Notable Developments Post-1970


1971: Microprocessor launched by Intel.
1972: IBM VM Operating System introduced.
1973: UNIX 4th Edition and Ethernet created.
1974: Personal Computer era begins; BASIC for Altair developed.
1976: Apple II launched.
1981: IBM PC introduced.
1983: Microsoft starts MS-Windows development.
1984: Apple Macintosh released.
1990: Windows 3.0 launched.
1991: GNU/Linux introduced.
1992: First Windows virus appeared.
1993: Windows NT launched.
2007: iOS debuted.
2008: Android OS released.

Operating System Structure


The structure of an OS affects its efficiency, reliability, and ease of maintenance. As systems have evolved,
more hybrid and modular approaches are adopted to balance performance with flexibility, leading to stable
and scalable operating systems suitable for a wide range of hardware and user demands.

Monolithic Structure:
All OS components run in a single layer, offering high performance but lacking modularity, which makes it
harder to isolate faults.
Example: Early Unix versions.
Layered Structure:
OS is divided into distinct layers, each with specific functions. Simplifies debugging and maintenance but can
add overhead.
Example: THE operating system.
Microkernel Structure:
Core functions are in the kernel, while other services run in user mode, increasing stability and security but
adding performance overhead.
Example: Minix, QNX.
Modules:
OS components are modular, allowing dynamic loading and unloading. This improves flexibility but can
create complexity in managing interactions.
Example: Linux kernel with loadable modules.
Hybrid Structure:
Combines elements of different models (e.g., microkernel and monolithic) to balance performance with
modularity and reliability.
Example: Windows NT.

The stages of operating system (OS) structure development represent the evolution of how operating
systems have been designed and organized over time. The key stages include:
Serial Processing:
Early systems: The computer would execute one job at a time, sequentially, without any multitasking.
Characteristics: The system would wait for one program to finish before starting another. This method was
inefficient and slow, especially as computer usage grew.
Example: Early computers like the ENIAC.
Simple Batch Systems:
Batch Processing: Jobs or tasks were grouped into batches and processed in sequence without interaction
during execution.
Characteristics: Programs were loaded onto the system and executed without user interaction. The OS would
manage job scheduling, and there was some level of automation in job execution.
Example: IBM 1401 and UNIVAC.
Multiprogrammed Batch Systems:
Multitasking: The system could run multiple programs (jobs) simultaneously by switching between them,
making better use of CPU time.
Characteristics: When one program was waiting for I/O operations, the CPU could switch to another
program. This reduced idle time and improved overall system efficiency.
Example: IBM System/360 and early versions of UNIX.
Time-Sharing Systems:
Interactive Systems: Time-sharing allows multiple users to interact with the computer at the same time,
giving the illusion that each user has their own dedicated machine.
Characteristics: The OS allocates a small time slice (or quantum) to each user or process, switching rapidly
between tasks to provide the appearance of simultaneous execution.
Example: CTSS (Compatible Time-Sharing System), early UNIX systems.

Operating System Operations


Operating System Operations
Modern operating systems are interrupt-driven, relying on interrupts and traps (software-generated
interrupts caused by errors or user requests) to respond to events. These interrupts allow the OS to handle
tasks dynamically instead of operating continuously.
1. Dual-Mode Operation
OS operations must distinguish between executing system-level code (kernel mode) and user-level code
(user mode):
Kernel Mode (mode bit = 0): Executes OS and privileged instructions, offering full system access.
User Mode (mode bit = 1): Restricts system access, allowing users to execute safe, non-privileged
instructions.
Mode Switching: Occurs when a user requests an OS service, triggering a system call. The mode switches to
kernel to perform the request and back to user mode after.
Privileged Instructions: Specific instructions (like I/O control and interrupt management) can only execute in
kernel mode for security.
2. Timer Management
A timer helps the OS maintain control by limiting user program execution times, preventing infinite loops,
and enforcing system resource management:
Timer Functionality: The timer counts down based on a clock tick, triggering an interrupt when it reaches
zero. This alerts the OS to resume control.
Privileged Timer Instruction: Only the OS can set or alter the timer, using it to monitor program execution
and prevent misuse of CPU time.

User Operating System Interface


User Operating System Interface
Users can interact with an operating system primarily through two interfaces: the Command-Line Interface
(CLI) and the Graphical User Interface (GUI).
1. Command-Line Interface (CLI)
The CLI allows users to type commands directly into the system. Key aspects include:
Command Interpreter: The command interpreter fetches and executes user commands, which often manage
files (create, delete, copy, etc.).
Implementation Approaches:
Internal Commands: Some CLIs, like MS-DOS, implement commands within the interpreter itself.
External Programs: In systems like UNIX, most commands are system programs executed by loading files
(e.g., rm file.txt executes the rm program to delete file.txt).
Examples: Commonly found in MS-DOS and UNIX systems, enabling flexibility by allowing users to create and
add new command programs.
2. Graphical User Interface (GUI)
The GUI provides a more visual and user-friendly way to interact with the OS, using windows, icons, and
menus. Key characteristics include:
Desktop Metaphor: Users navigate using a mouse, clicking icons that represent files, programs, and system
functions on a virtual desktop.
Action-Based: Commands are executed through intuitive actions (clicking, dragging) rather than typed
instructions.
Examples: Widely used in Windows and Linux, GUIs make the OS accessible to non-technical users and
provide a streamlined user experience.
computer system components
A computer system indeed comprises four main components that interact to deliver a cohesive and
functional environment. Here’s a detailed breakdown of each component:

1. Hardware
Definition: Hardware refers to the physical components of a computer that perform the core functions. It
includes all the tangible elements that make up a computer system.
Components:
Central Processing Unit (CPU): The "brain" of the computer that processes instructions and executes
commands.
Memory (RAM): Temporary storage for active data and instructions.
Storage: Persistent storage devices like hard drives (HDD), solid-state drives (SSD), and optical drives.
Input/Output Devices: Components such as the keyboard, mouse, monitor, and printer that allow interaction
with the computer.
Function: Hardware provides the foundational resources and capabilities that allow the OS, software, and
users to operate and perform tasks.

2. Operating System (OS)


Definition: The OS is the system software that manages hardware and software resources, providing services
to applications and acting as an intermediary between the user and hardware.
Key Responsibilities:
Resource Management: Manages CPU, memory, and I/O devices to ensure efficient performance.
File Management: Manages data storage, retrieval, and organization.
Process Management: Schedules tasks and allocates CPU time to running applications.
Security: Controls access to system resources and enforces user permissions.
Function: The OS is essential for creating a stable environment in which applications can operate, handling
low-level tasks so that programs and users don’t need to manage hardware directly.

3. System & Application Programs


System Programs:
Definition: These are essential programs provided by the OS to perform tasks related to system maintenance
and operations.
Examples: File managers, disk management tools, system utilities for performance monitoring, and backup
tools.
Function: System programs are designed to help maintain and optimize the system, allowing users and
administrators to manage resources effectively.
Application Programs:
Definition: Software that users run to perform specific tasks or solve particular problems.
Examples: Web browsers, word processors, games, and media players.
Function: Application programs allow users to perform productive tasks, access information, create content,
and interact with digital data in meaningful ways.

4. Users
Definition: Users are the individuals who interact with the computer system, whether directly or through
other systems or networks.
Types of Users:
End Users: Individuals using applications for personal or professional tasks (e.g., students, employees,
gamers).
System Administrators: Specialists responsible for maintaining, configuring, and managing computer systems
and networks.
Developers: Individuals who create software applications and system programs.
Function: Users initiate tasks, create data, and direct the purpose of computer systems. Their interaction
provides input that drives the use and advancement of both hardware and software components.

Operating System Services


An operating system provides essential services for users and programs, facilitating execution, resource
management, and security. These services can be divided into user-oriented services and system-oriented
functions:

User-Oriented Services
User Interface (UI):
Allows users to interact with the system.
Types:
Command-line Interface (CLI): Text commands.
Batch Interface: Pre-written commands in files.
Graphical User Interface (GUI): Visual interaction via windows, menus, and pointing devices.
Some systems support multiple UI types.
Program Execution:
Loading, running, and terminating programs.
Supports normal and abnormal program termination.
I/O Operations:
Manages input and output devices for programs (e.g., file access, CD recording).
Provides safe and efficient I/O control, isolating users from hardware complexities.
File-System Manipulation:
Handles file operations such as creation, deletion, reading, writing, and searching.
Supports permissions management to control file and directory access.
Offers diverse file systems for performance and feature optimization.
Communication:
Facilitates information exchange between processes, either on the same machine (via shared memory) or
across networks (via message passing).
Error Detection:
Constantly monitors for errors in hardware, I/O devices, and programs.
Takes corrective actions to ensure system stability.
Debugging tools enhance system usability and efficiency.

System-Oriented Functions
Resource Allocation:
Allocates resources (CPU, memory, storage, I/O devices) efficiently for multiple users and jobs.
Includes specialized routines like CPU scheduling and device management.
Accounting:
Tracks resource usage by users and programs.
Data can be used for billing or optimizing system performance.
Protection and Security:
Ensures controlled access to system resources.
Requires user authentication (e.g., passwords).
Protects against unauthorized access and logs connection attempts for security monitoring.
Process State As a process executes, it changes state. The state of a process is defined in part by the current
activity of that process. Each process may be in one of the following states:
♣ New. ►The process is being created
. ♣ Running. ► Instructions are being executed.
♣ Waiting. ►The process is waiting for some event to occur (such as an I/0 completion or reception of a
signal).

♣ Ready. ►The process is waiting to be assigned to a processor.


♣ Terminated. ►The process has finished execution. It is important to realize that only one process can be
running on any processor at any instant. Many processes may be ready and waiting
Job Scheduling vs. CPU Scheduling
Job Scheduling and CPU Scheduling are crucial components of a multitasking operating system that ensures
optimal utilization of system resources.

Job Scheduling (Long-Term Scheduling)


Purpose:
Determines which processes are loaded into memory for execution from a pool of waiting jobs.
Key Characteristics:
Frequency:
Infrequent compared to CPU scheduling; invoked only when a process leaves the system or new jobs are
submitted.
Controls Multiprogramming:
Determines the degree of multiprogramming (number of processes in memory).
Process Selection:
Ensures a balanced mix of I/O-bound and CPU-bound processes to optimize system performance.
Resource Consideration:
Allocates resources for processes being brought into memory.
Example:
A batch-processing system selecting jobs from a disk queue and loading them into memory.
Impact:
Directly influences system throughput and the time-sharing ability of the system.

CPU Scheduling (Short-Term Scheduling)


Purpose:
Determines which process in the ready queue gets the CPU next for execution.
Key Characteristics:
Frequency:
Invoked frequently, often every few milliseconds, as processes enter and leave the CPU.
Responsiveness:
Critical for maintaining system responsiveness, especially in time-sharing systems.
Algorithm Usage:
Employs algorithms to decide process selection (e.g., FCFS, SJF, Round Robin, Priority Scheduling).
Process Context Switch:
Saves and restores process states (context) during a switch to ensure seamless execution.
Example:
Selecting the next process in the ready queue when the current process is waiting for I/O or has completed
execution.
Impact:
Directly affects CPU utilization and overall system performance.

Comparison
Feature Job Scheduling CPU Scheduling
Also Known As Long-Term Scheduling Short-Term Scheduling
Objective Selects processes to load into memory. Allocates CPU to processes in the ready queue.
Frequency Infrequent (e.g., minutes) Frequent (e.g., milliseconds)
Focus System throughput and resource mix CPU utilization and process responsiveness
Feature Job Scheduling CPU Scheduling
Algorithms Used Simple or weighted algorithms FCFS, SJF, Round Robin, Priority, etc.
Resource Managed Memory CPU

Context Switching in Computer Graphics (CG)


Context Switching in computer graphics involves transitioning between different graphics contexts during
rendering tasks, typically handled by the GPU or CPU. It is akin to the context switching in operating systems
but specific to graphics pipelines and processes.

Key Points:
Graphics Context: Contains essential state information for rendering, including shaders, buffers, textures,
and matrices.
Scenarios:
Multi-threaded rendering.
Switching between tasks (e.g., UI rendering, game scenes).
Resource management (shared textures, buffers).
Components Saved:
GPU state (pipeline settings, shaders).
Command buffers and framebuffers.
Overhead:
Similar to CPU context switching, it involves saving and restoring state, which can cause performance delays.
Hardware & API Support:
Modern GPUs and APIs (e.g., Vulkan, DirectX 12) help reduce overhead by managing state changes and
command queues efficiently.
Optimizing Context Switching:
Minimize Frequency:
Batch rendering tasks and sort states to reduce context changes.
Efficient Multi-threading:
Distribute workloads to avoid unnecessary synchronization.
Pipeline Optimization:
Use modern graphics APIs for better control over contexts and reduced overhead.
Shared Contexts:
Use shared contexts for tasks accessing common resources to avoid redundant state switches.
Comparison to CPU Context Switching:
Feature CPU GPU
Purpose Process/thread switching. Switching between rendering tasks.
State Saved Registers, counters. Shaders, textures, buffers.
Frequency Frequent in multitasking. Less frequent, task-dependent.
Overhead Pure overhead. May cause pipeline stalls.
Optimization Hardware-assisted. Optimized with modern APIs.
Critical Section Problem
In a system with n processes {P0, P1, ..., Pn-1}, each process has a segment of code known as the critical
section where it can modify shared resources. The goal is to design a protocol to ensure that no two
processes execute in their critical sections simultaneously, ensuring proper synchronization.
Key Concepts:
Critical Section: A part of a process where shared resources are accessed or modified.
Entry Section: Code that requests permission to enter the critical section.
Exit Section: Code that handles the process of leaving the critical section.
Remainder Section: Code that runs after the critical section.
The critical-section problem requires designing a protocol that allows processes to cooperate while ensuring:
Mutual Exclusion: Only one process can be in its critical section at any time.
Progress: If no process is in its critical section and some processes want to enter, only those processes not in
their remainder sections can decide who enters next. This decision must not be postponed indefinitely.
Bounded Waiting: There is a limit on how many times other processes can enter their critical sections after a
process has requested entry but before it is granted access.
Approaches for Handling Critical Sections:
Preemptive Kernels: Allow a process to be preempted while running in kernel mode. While this can be more
responsive and suitable for real-time programming, it requires careful design to avoid race conditions,
especially in systems with multiple processors.
Nonpreemptive Kernels: Do not allow a process to be preempted while running in kernel mode. This type of
kernel avoids race conditions since only one process runs in kernel mode at a time. However, it is less
suitable for real-time tasks as it can delay critical processes.
Deadlocks
In a multiprogramming environment, several processes may compete for a finite number of resources. A
process requests resources; if the resources are not available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change state, because the resources it has requested are
held by other waiting processes. This situation is called a deadlock.
Paging in Memory Management
Paging is a memory-management technique that enables the physical address space of a process to be
non-contiguous, helping to avoid external fragmentation and eliminate the need for compaction. It also
addresses issues with fitting memory chunks of varying sizes in earlier systems that suffered from
fragmentation.
Basic Paging Method:
Physical Memory is divided into fixed-sized blocks called frames.
Logical Memory (used by the process) is divided into blocks of the same size, called pages.
When a process runs, its pages are loaded into available frames in physical memory.
The backing store (e.g., disk) is also divided into blocks of the same size as the memory frames.
Address Translation in Paging:
Every CPU-generated address is split into:
Page number (p): Used to index into the page table.
Page offset (d): Specifies the position within the page.
The page table contains the base address of each page in physical memory. The CPU uses the page number
to find the corresponding frame in the page table and combines it with the offset to get the physical memory
address.
Example:
If the logical memory size is 32 bytes with 4-byte pages and physical memory has 8 pages (totaling 32 bytes),
the mapping would be:
Logical address 0 → page 0, offset 0 → maps to physical address 20.
Logical address 4 → page 1, offset 0 → maps to physical address 24.
Key Concept:
Paging functions as a form of dynamic relocation, where logical addresses are mapped to physical addresses
by the hardware. This method simplifies memory allocation and management.

Demand Paging in Virtual Memory


Demand Paging is a memory management technique in virtual memory systems that only loads the pages of
a program that are actually needed, rather than loading the entire program into memory at once. This
approach saves memory and reduces the time spent swapping unnecessary pages into memory.
Concept of Demand Paging:
In traditional memory management, an entire program is loaded into memory, even if only parts of it are
needed. This wastes memory and increases loading times.
With demand paging, only the pages that are accessed are loaded into memory, while unused parts remain
on disk.
Basic Process of Demand Paging:
Page Table: Tracks the location of pages in memory and on disk. Entries are either valid (page is in memory)
or invalid (page is on disk).
Page Fault: If a process accesses a page not currently in memory, a page fault occurs, prompting the OS to
load the missing page from disk.
Handling a Page Fault:
Trap to OS: The CPU stops the process and signals a page fault.
Check Validity: The OS checks if the access is legal (within the process's address space).
Load the Page: If the page is not in memory, the OS loads it from disk into a free frame.
Disk Operation: The OS schedules a disk read to transfer the page from disk to memory.
Update Page Table: Once the page is loaded, the page table is updated.
Resume Execution: The process continues as if the page was always in memory.
Performance of Demand Paging:
Memory Access Time (ma): Time to access a page in memory, typically between 10 to 200 nanoseconds.
Page Fault Time: Time to handle a page fault, including disk I/O and memory updates.
Effective Access Time (EAT): Average time to access memory, accounting for both regular memory accesses
and page faults: EAT=(1−p)×ma+p×page fault timeEAT = (1 - p) \times ma + p \times \text{page fault
time}EAT=(1−p)×ma+p×page fault time Where p is the probability of a page fault.
Steps in Handling a Page Fault:
OS traps the process and saves its state.
OS checks the page’s validity.
If the page is valid but not in memory, it is located on disk.
Disk operation is scheduled to load the page into memory.
The CPU may execute another process while waiting.
Once the page is loaded, the page table is updated, and the process resumes.
Swap Space Optimization:
Swap space on disk is used to store pages not in memory, allowing efficient management of memory by
swapping pages in and out. Swap space is faster than general file systems because it uses large, contiguous
blocks.
Page Replacement in Virtual Memory
Page replacement is a critical technique in virtual memory systems used to manage memory when there are
no free frames available during a page fault. It involves selecting a page to swap out to make room for a new
page.
Need for Page Replacement:
When a page fault occurs and there are no free frames, the operating system has a few options:
Terminate the user process (undesirable).
Swap out another process to free memory.
Use page replacement to swap out an existing page.
Basic Page Replacement Process:
Find the Desired Page: Locate the page on disk.
Find a Free Frame: If no free frame exists, select a page to evict using a replacement algorithm.
Swap Out a Victim Page: Write the evicted page to disk and update page and frame tables.
Load the Desired Page: Read the new page into the freed frame and update the page and frame tables.
Resume Process: The process continues after the page is loaded into memory.
Page Replacement Algorithms:
FIFO (First-In-First-Out):
Replaces the oldest page.
Advantages: Simple to implement.
Disadvantages: May replace frequently used pages, causing poor performance.
Optimal Page Replacement (OPT):
Replaces the page that will not be used for the longest time.
Advantages: Best performance (lowest page fault rate).
Disadvantages: Requires knowledge of future page references, which is impractical.
Least Recently Used (LRU):
Replaces the page not used for the longest time.
Advantages: Better than FIFO and simpler than OPT.
Disadvantages: Requires tracking the time of last use, which may need hardware support.
LRU Approximation:
Uses reference bits set when a page is accessed, approximating true LRU.
The operating system periodically checks reference bits to determine which pages have been least recently
used.
Counting-Based Page Replacement:
LFU (Least Frequently Used): Replaces the page with the smallest access count. May retain pages with initial
heavy use but little use later.
MFU (Most Frequently Used): Replaces the page with the highest access count, assuming it will not be used
again soon.
Page-Buffering Algorithms:
Keep a pool of free frames and load a page into a free frame before evicting a victim page, reducing waiting
time during a page fault.
In some systems, frame contents are preserved in the free-frame pool for reuse.
Challenges with Memory Allocation for I/O:
Balancing memory allocation between user processes and I/O buffers is a challenge. Over-allocating memory
to I/O buffers can lead to a lack of free frames for processes, increasing the need for efficient page
replacement.
Impact on System Performance:
FIFO may lead to a high page fault rate, while LRU and OPT generally perform better, though OPT is
impractical due to its requirement for future knowledge.
Efficient page replacement is crucial for optimizing system performance and ensuring that memory is used
effectively.
Memory Allocation
Memory allocation is the process of assigning memory space to processes in an operating system. The two
primary methods for allocating memory are fixed-partition allocation and variable-partition allocation.
Fixed-Partition Allocation:
Memory is divided into fixed-sized partitions, with each partition containing one process.
Multiprogramming Degree: Determined by the number of partitions.
Process Allocation: When a partition is free, the operating system loads a process into it.
Termination: When a process finishes, its partition becomes available for another process.
Queue Management: The operating system manages the input queue of processes using a scheduling
algorithm.
Variable-Partition Allocation:
Memory is managed as a collection of blocks or "holes."
Initially, memory is one large hole, which fragments as processes are loaded.
Allocation: The system searches for a hole large enough for a new process. If a hole is too large, it is split.
Recombination: When a process terminates, its hole is freed. If adjacent to another hole, the holes are
merged.
Fragmentation: Over time, small gaps (fragmentation) can appear, making it difficult to use available
memory efficiently.
Memory Allocation Strategies:
First-Fit:
Allocates the first hole that is large enough.
Efficiency: Fast, as it stops searching once a suitable hole is found.
Performance: Can leave small fragmented holes, especially at the end.
Best-Fit:
Allocates the smallest hole that fits the request.
Efficiency: Requires searching the entire list unless sorted by hole size.
Performance: Minimizes leftover space, but may cause many small unusable holes over time.
Worst-Fit:
Allocates the largest hole.
Efficiency: Requires searching the entire list unless sorted by size.
Performance: Results in large leftover holes, which could be useful for future processes.
Performance Comparison:
First-Fit is generally faster and performs better in terms of allocation speed compared to Best-Fit, which
requires searching the entire list.
First-Fit and Best-Fit tend to offer similar memory utilization, but First-Fit is preferred for its speed.
Worst-Fit is typically the least efficient due to creating large unusable gaps in memory.
Conclusion:
First-Fit is preferred for speed.
Best-Fit is more efficient at minimizing leftover space but can be slower.
Worst-Fit is generally less effective due to its tendency to waste memory.
File System For most users, the file system is the most visible aspect of an operating system. It provides the
mechanism for on-line storage of and access to both data and programs of the operating system and all the
users of the computer system. The file system consists of two distinct parts: a collection of files, each storing
related data, and a directory structure, which organizes and provides information about all the files in the
system. File systems reside permanently on secondary storage devices.

Implementing File Systems


Implementing File Systems
The file system is essential for managing and accessing data stored on secondary storage, primarily disks. It
ensures efficient and organized storage and retrieval of file contents and interfaces with other parts of the
operating system.

File System Overview


Purpose:
Enables online storage and access to data and programs.
Structures file usage, allocates disk space, tracks data locations, and interfaces with secondary storage.
Disk Characteristics:
Rewrite Capability: Blocks can be read, modified, and rewritten in place.
Direct Access: Any block can be accessed randomly, enabling both sequential and random file access.
I/O Efficiency:
Data transfers between memory and disk are in blocks (units with one or more sectors).
Typical sector size ranges from 32 bytes to 4,096 bytes, with 512 bytes being standard.

File System Design Challenges


User Interface:
Defines how users interact with the file system:
File attributes
Operations on files
Directory structure for file organization
Logical-to-Physical Mapping:
Algorithms and data structures map the logical file system to the physical storage medium.

File System Structure


The file system is composed of layers, with each layer building on the functionalities of the one below it:
I/O Control Layer:
Includes device drivers and interrupt handlers for transferring data between memory and the disk.
Device Driver: Acts as a translator, converting high-level commands (e.g., "retrieve block 123") into
hardware-specific instructions for the disk controller.
Manages memory buffers and caches to temporarily store file-system data and directory blocks.
Basic File System:
Issues generic commands to device drivers for reading and writing disk blocks.
Identifies blocks using numeric disk addresses (e.g., drive, cylinder, track, sector).
File Organization Module:
Translates logical block addresses into physical block addresses.
Includes the free-space manager to track and allocate unoccupied disk blocks.
Logical File System:
Manages metadata (information about files, not their content), such as file ownership, permissions, and
location.
Maintains the directory structure and uses file control blocks (FCBs) to store file-specific details.

Key Components
File Control Block (FCB):
Contains metadata like ownership, permissions, and the file's physical location.
Free-Space Manager:
Tracks and allocates unoccupied storage blocks.
Buffer Manager:
Handles memory buffers and ensures sufficient space for ongoing I/O operations.
File Access Methods
Files store information that must be accessed and read into computer memory for use. Different access
methods cater to various application needs, depending on the system and use case.

Access Methods
Sequential Access:
Data is processed one record at a time in order.
Operations:
Read next: Reads the next portion and automatically moves the file pointer.
Write next: Appends data to the end and updates the pointer.
Additional Features:
Files can be reset to the beginning.
Some systems allow skipping records.
Example: Modeled on tape storage but can also work with random-access devices.
Direct Access:
Files are treated as a sequence of numbered blocks or records.
Programs can read or write blocks in any order.
Example Operations:
Read block 14 → Read block 53 → Write block 7.
Suitability: Ideal for large data sets like databases.
Indexed Access:
An index with pointers to specific blocks is created.
Access Process:
Search the index → Use pointers to retrieve the desired record.
For large files:
Primary index points to secondary indices.
Secondary indices point to data, reducing memory overhead.

Disk Scheduling
Objective: Optimize access time (seek time + rotational latency) and disk bandwidth.
Key Concepts:
Access Time: Includes seek time (disk arm movement) and rotational latency (disk rotation time).
Disk Bandwidth: Total bytes transferred divided by total time taken.
I/O Request Parameters: Type of operation, disk address, memory address, and sectors to transfer.

Disk Scheduling Algorithms:


FCFS (First-Come, First-Served):
Requests processed in the order they arrive.
Advantages: Simple and fair.
Disadvantages: May cause long seek times.
Example: Queue: 98, 183, 37, 122, 14, 124, 65, 67. Total movement: 640 cylinders.
SSTF (Shortest Seek Time First):
Serves the closest request to the current head position.
Advantages: Reduces seek time.
Disadvantages: Can cause starvation for distant requests.
Example: Sequence: 65 → 67 → 37 → 14 → 98 → 122 → 124 → 183. Total movement: 236 cylinders.
SCAN (Elevator Algorithm):
Disk arm moves in one direction, servicing requests, then reverses direction.
Advantages: Fairer than SSTF; avoids starvation.
Disadvantages: May involve unnecessary movement to ends.
Example: Sequence: 37 → 14 → 0 → 65 → 67 → 98 → 122 → 124 → 183.
C-SCAN (Circular SCAN):
Similar to SCAN but treats the disk as a circular list.
Arm moves in one direction and jumps back to the start after reaching the end.
Advantages: Uniform wait times for requests.
Example: Sequence: 37 → 14 → 0 → (jump to end) → 65 → 67 → 98 → 122 → 124 → 183.
LOOK:
Optimized SCAN that only moves to the last request in each direction before reversing.
Advantages: Reduces unnecessary movement to disk ends.
Example: Stops at 14 before reversing instead of moving to 0.
C-LOOK:
Circular version of LOOK.
Moves in one direction to the farthest request, then jumps to the start.
Advantages: Balances uniform wait times and reduced movement.
Example: Sequence: 14 → 37 → 65 → 67 → 98 → 122 → 124 → 183, then jumps to start.

File Operations
A file is an abstract data type that supports several basic operations, provided by the operating system
through system calls. These operations include creating, writing, reading, repositioning, deleting, truncating,
and more.

Basic File Operations


Creating a File:
Steps:
Allocate space for the file in the file system.
Add an entry for the new file in the directory.
Writing to a File:
Requires the file name and the data to be written.
The operating system locates the file in the directory and uses a write pointer to track where the next data
should be written. The pointer updates after each write.
Reading from a File:
Requires the file name and a memory location to store the data.
A read pointer tracks the current position in the file and updates after each read.
Repositioning within a File (File Seek):
Changes the current position in the file using a file-position pointer.
No actual I/O is involved in this operation.
Deleting a File:
Searches the directory for the file, releases its space, and removes its directory entry.
Truncating a File:
Erases the file’s contents but retains its attributes (e.g., file name, permissions).

Additional Operations
Appending: Add new data to the end of an existing file.
Renaming: Change the name of a file.
Copying: Combine read and write operations to duplicate a file or transfer its data to another I/O device,
such as a printer.

Open File Details


Most file operations involve directory searches to locate the file entry. To reduce repeated searches, the
open() system call is used to prepare a file for active use. The system maintains several pieces of information
for open files:
File Pointer:
Tracks the current read/write position in the file.
File-Open Count:
Tracks the number of active references to the file.
When this count reaches zero (on the last close operation), the system removes the file's entry from the
open-file table.
Disk Location:
Stores the file's location in memory to avoid repeated disk reads.
Access Rights:
Specifies the mode (e.g., read-only, write-only) in which the file is opened.
Ensures the operating system enforces correct access permissions during file operations.

You might also like