OS3 RD Sem
OS3 RD Sem
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:
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.
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.
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.
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.
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.
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.
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.
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.
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).
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];
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.
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.
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.
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).
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
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.
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.
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.
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.