π UNIT I: Introduction & Memory Management
Operating System Basics
What is an Operating System?
An Operating System (OS) is a large collection of software that manages all the resources of a computer system, including memory, processor, file system, and input-output devices. It acts as an intermediary between users and computer hardware, providing an environment in which users can execute programs conveniently and efficiently.
Types of Operating Systems
1. Simple Batch Operating System:
In a simple batch operating system, users submit jobs to a computer operator, who then creates batches of jobs with similar requirements. These batches are executed sequentially without human intervention during processing. The system processes jobs one after another in the order they are received.
- Advantages: Reduces idle time of CPU, automatic job sequencing
- Disadvantages: No interaction with user during execution, slower processing compared to multiprogramming
- Examples: Early mainframe systems, payroll processing, bank statements
2. Multiprogrammed Batch Systems:
Multiprogramming OS allows multiple processes to execute simultaneously by monitoring their process states and switching between processes. The operating system loads several programs into memory and executes them concurrently to avoid CPU and memory underutilization. When one process is waiting for I/O, another process can use the CPU.
- Key Feature: Multiple programs in memory simultaneously
- Benefit: Better CPU utilization and faster processing
- Mechanism: Context switching between processes
3. Time-Sharing Systems:
Time-sharing is a form of multiprogrammed operating system that operates in interactive mode with quick response time. Multiple users can simultaneously share computer resources. The CPU time is divided into small time slices (quantum), and each user gets a turn to use the CPU. Users interact with the system through terminals, and the system provides immediate feedback.
- Features: Interactive, multiple users, short response time
- Time Slice: Each user gets small time quantum (10-100 milliseconds)
- Examples: UNIX, Linux, Windows Server
4. Personal Computer (PC) Systems:
PC operating systems are designed for single-user environments where one person uses the computer at a time. They provide a graphical user interface (GUI) and support multitasking, allowing multiple applications to run simultaneously.
- Characteristics: User-friendly, GUI-based, multitasking support
- Examples: Windows 10/11, macOS, Ubuntu Desktop
5. Parallel Systems:
Parallel operating systems are designed to speed up program execution by dividing programs into multiple segments that can run simultaneously on multiple processors. These systems use computer resources including single computers with multiple processors or several computers connected by a network.
- Architecture: Multiple CPUs, shared memory
- Types: Symmetric Multiprocessing (SMP), Asymmetric Multiprocessing
- Benefits: Increased throughput, fault tolerance
6. Distributed Systems:
In distributed systems, multiple autonomous computers communicate through a network to achieve a common goal. Each computer has its own processor and memory but appears as a single system to users.
- Features: Resource sharing, computation speedup, reliability
- Communication: Message passing, Remote Procedure Calls (RPC)
- Examples: Google's infrastructure, cloud computing platforms
7. Real-Time Systems:
Real-time operating systems execute programs with guaranteed upper bounds on task execution time. They are used where a large number of events (mostly external) must be accepted and processed within strict deadlines.
Hard Real-Time Systems:
- Strict timing guarantees - missing deadlines is not acceptable
- Deterministic and predictable response times
- High-priority tasks are always executed first
- Examples: Aircraft control systems, airbag deployment, medical monitoring devices
- Resource utilization is typically lower due to stringent requirements
Soft Real-Time Systems:
- Timing guarantees are not as strict - occasional deadline misses acceptable
- Response time varies based on system load
- Better resource utilization
- Examples: Multimedia streaming, online gaming, video conferencing
- More flexible and less costly to implement
Memory Management
Logical vs Physical Address
| Logical Address | Physical Address |
|---|---|
| Generated by the CPU during program execution | Actual address in physical memory (RAM) |
| Also called Virtual Address | Also called Real Address |
| Used by the program/process | Used by memory hardware |
| Keeps changing during execution | Remains constant once allocated |
| Visible to users | Hidden from users |
| Example: IPv4 address | Example: MAC address |
Address Translation: The Memory Management Unit (MMU) translates logical addresses to physical addresses using page tables or segment tables.
Swapping
Swapping is a memory management technique where a process can be temporarily moved from main memory to secondary storage (backing store) and then brought back to main memory for continued execution. This is done to free up memory for other processes.
Process of Swapping:
- Swap Out: Moving process from RAM to disk (backing store)
- Swap In: Moving process from disk back to RAM
- Context Switch: Saving process state before swapping out
When Swapping Occurs:
- When memory is full and a new process needs to be loaded
- In priority-based scheduling (swap out lower priority processes)
- In round-robin scheduling (swap after time quantum expires)
Backing Store: Fast disk large enough to accommodate copies of all memory images for all users. The system maintains a ready queue of processes on the backing store.
Contiguous Memory Allocation
In contiguous memory allocation, each process is allocated a single contiguous block of memory. The main memory is divided into two partitions: one for the operating system and one for user processes.
Allocation Strategies:
1. First Fit:
- Allocate the first available hole that is large enough
- Fast allocation
- May leave many small holes at beginning of memory
2. Best Fit:
- Allocate the smallest hole that is large enough
- Must search entire list unless ordered by size
- Produces smallest leftover holes
- Most efficient memory utilization
3. Worst Fit:
- Allocate the largest available hole
- Must also search entire list
- Produces largest leftover holes
Fragmentation
Internal Fragmentation:
Internal fragmentation occurs when memory allocated to a process is slightly larger than the requested memory, and the difference remains unused within the allocated partition. This happens in fixed-size partition allocation.
- Memory wasted inside allocated partitions
- Occurs in paging (last page may not be fully used)
- Solution: Use smaller page sizes (but increases page table size)
External Fragmentation:
External fragmentation occurs when there is enough total free memory to satisfy a request, but the available memory is not contiguous. Free memory is scattered in small blocks throughout memory.
- Free memory exists but is divided into many small non-contiguous blocks
- Occurs in variable-size partition allocation
- Solutions:
- Compaction: Moving all processes to one end of memory, creating one large free block
- Paging: Allows non-contiguous memory allocation
- Segmentation with Paging: Combines benefits of both
Paging
Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. It avoids external fragmentation and efficiently uses memory.
Key Concepts:
- Pages: Logical memory is divided into fixed-size blocks called pages (typically 4KB)
- Frames: Physical memory is divided into fixed-size blocks called frames (same size as pages)
- Page Table: Data structure that maps pages to frames, maintained by OS for each process
- Page Number: Index into page table
- Page Offset: Combined with frame base address to get physical address
Address Translation in Paging:
Logical Address = [Page Number | Page Offset]
Physical Address = [Frame Number | Page Offset]
Example:
Logical Address: 2054 (in decimal)
Page Size: 1024 bytes
Page Number = 2054 / 1024 = 2
Page Offset = 2054 % 1024 = 6
If Page 2 maps to Frame 5:
Physical Address = (5 Γ 1024) + 6 = 5126
Advantages of Paging:
- No external fragmentation
- Efficient memory utilization
- Easy to implement
- Allows non-contiguous memory allocation
- Protection through separate page tables
Disadvantages of Paging:
- Internal fragmentation in last page
- Page table consumes memory
- Increased access time (two memory accesses per instruction)
- Translation Lookaside Buffer (TLB) needed for performance
Segmentation
Segmentation is a memory management technique that divides a process into variable-sized logical segments based on the program structure. Each segment represents a logical unit such as main program, procedure, function, method, object, local variables, global variables, common block, stack, or symbol table.
Key Features:
- Variable-size segments (user-defined)
- Logical division of process
- Each segment has a name and length
- Segment table stores base address and limit of each segment
Segmentation vs Paging:
| Paging | Segmentation |
|---|---|
| Fixed-size pages | Variable-size segments |
| Invisible to user | Visible to user |
| Physical division | Logical division |
| No external fragmentation | External fragmentation possible |
| Hardware determines size | User/compiler determines size |
Virtual Memory
Concept of Virtual Memory
Virtual memory is a memory management technique that creates an illusion of a very large main memory by using secondary storage (disk) as an extension of RAM. It allows execution of processes that may not be completely loaded in the main memory.
Advantages of Virtual Memory:
- Large Address Space: Programs can be larger than physical memory
- Memory Protection: Each virtual address is translated to physical address through page table
- Efficient Use: Less I/O needed to load programs
- More Programs: More programs can run simultaneously
- Better CPU Utilization: Higher degree of multiprogramming
- Flexibility: Not all code needs to be in memory (error handlers, rarely used features)
Implementation Methods:
- Demand Paging: Most common method
- Demand Segmentation: Less commonly used
Demand Paging
Demand paging is a virtual memory implementation where pages are loaded into memory only when they are needed (demanded) during execution, not in advance. It is similar to a paging system with swapping.
How It Works:
- Process starts with no pages in memory
- When a page is referenced, if it's in memory, execution continues
- If page is not in memory, a page fault occurs
- OS loads the required page from disk to memory
- Page table is updated
- Instruction is restarted
Page Fault Handling:
1. Trap to OS (page fault interrupt)
2. Save user registers and process state
3. Determine if reference was valid
4. Find location of page on disk
5. Find free frame in memory
6. If no free frame, select victim page using page replacement algorithm
7. Read desired page from disk into free frame
8. Update page table
9. Restart instruction that caused page fault
Valid-Invalid Bit: Each page table entry has a bit to indicate if page is in memory (valid=1) or on disk (invalid=0).
Page Replacement Algorithms
When a page fault occurs and there are no free frames, a page must be replaced. The goal is to minimize page faults.
1. FIFO (First-In-First-Out):
The oldest page in memory is replaced. Simple to implement using a queue, but doesn't consider page usage patterns. Can suffer from Belady's Anomaly.
Belady's Anomaly: In FIFO, increasing the number of frames can sometimes increase the number of page faults (counterintuitive).
2. LRU (Least Recently Used):
Replaces the page that has not been used for the longest time. Based on the principle of locality - pages used recently are likely to be used again soon.
Implementation Methods:
- Counter: Each page has a timestamp, replace page with oldest timestamp
- Stack: Keep stack of page numbers, most recently used on top
3. Optimal Page Replacement:
Replaces the page that will not be used for the longest period of time in the future. This gives the lowest possible page fault rate but requires future knowledge, making it impractical to implement. Used as a benchmark to compare other algorithms.
4. Second Chance (Clock Algorithm):
An improvement on FIFO that gives pages a "second chance" before replacement. Uses a reference bit:
- If reference bit = 0, replace the page
- If reference bit = 1, give it a second chance (set bit to 0, move to next page)
- Circular queue implementation
Thrashing
Thrashing is a serious performance problem that occurs when a system spends more time paging (swapping pages in and out) than executing actual processes. The CPU utilization drops drastically.
Causes of Thrashing:
- Process allocated too few frames
- Degree of multiprogramming too high
- Too many processes competing for limited memory
- Each process keeps causing page faults
- CPU scheduler loads more processes thinking CPU is idle
- This creates a vicious cycle
Prevention Techniques:
- Working Set Model: Monitor the working set (actively used pages) of each process
- Page Fault Frequency: Monitor page fault rate, if too high, allocate more frames
- Decrease Multiprogramming: Swap out some processes
- Local Page Replacement: Each process replaces only its own pages
Working Set Model: The working set of a process is the set of pages it is currently using. The working set changes over time as the process moves from one locality to another.
β Unit I: Solved Questions
Q1. Explain different types of Operating Systems with examples
Answer:
Operating systems can be classified into several types based on their functionality and use cases. Let me explain each type in detail:
1. Batch Operating System: In a batch OS, similar jobs are grouped together into batches and executed sequentially without user interaction during execution. The computer operator collects jobs with similar requirements and processes them as a batch.
Example: Payroll processing, bank statement generation
Advantage: Automatic job sequencing, reduced idle time
Disadvantage: No interaction with users, jobs must wait in queue
2. Multiprogramming OS: This type allows multiple programs to reside in memory simultaneously. When one program is waiting for I/O operation, the CPU switches to another program, thus improving CPU utilization and system throughput.
Example: Modern server systems
Key Feature: Context switching between processes to avoid CPU idle time
3. Time-Sharing OS: Multiple users can use the computer simultaneously through different terminals. The CPU time is divided into small time slices (quantum), and each user gets CPU time in turn. The system provides quick response time, creating an illusion that each user has exclusive access.
Example: UNIX, Linux, Windows Server
Key Feature: Interactive, multiple users, short response time (10-100 ms)
4. Real-Time OS: These systems execute programs with guaranteed time bounds. They are used in time-critical applications where missing a deadline can have serious consequences.
Hard Real-Time Example: Aircraft control, airbag deployment, medical devices
Soft Real-Time Example: Multimedia streaming, online gaming
5. Distributed OS: Multiple independent computers work together and appear as a single system to users. Resources are shared across the network.
Example: Cloud computing platforms, Google's infrastructure
6. Parallel OS: Designed to run on systems with multiple processors. Programs are divided into multiple segments that execute simultaneously on different processors.
Example: Multiprocessor workstations, supercomputers
π Key Points to Remember:
- Batch OS is for offline processing without user interaction
- Time-sharing creates illusion of dedicated system for each user
- Real-time OS guarantees deadline adherence (hard) or tries to meet deadlines (soft)
- Modern OS often combine features from multiple types
Q2. Consider the page reference string 1,2,3,4,2,1,6,2,1,2,3,7,6,3,2,1,2,3,6. Calculate page faults for LRU and Optimal algorithms (4 frames)
Answer:
I will solve this step-by-step for both algorithms with 4 frames initially empty.
LRU (Least Recently Used) Algorithm:
In LRU, we replace the page that has not been used for the longest time. Let me trace through each reference:
Reference String: 1, 2, 3, 4, 2, 1, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6
Frame 1: 1 1 1 1 1 1 6 6 6 6 6 7 7 7 7 1 1 1 1
Frame 2: - 2 2 2 2 2 2 2 2 2 2 2 6 6 6 6 6 6 6
Frame 3: - - 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Frame 4: - - - 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2
PF PF PF PF H H PF H H H PF PF PF H PF PF H H H
PF = Page Fault, H = Hit
Total Page Faults (LRU) = 9
Optimal Page Replacement Algorithm:
In Optimal, we replace the page that will not be used for the longest time in the future. This requires looking ahead in the reference string:
Reference String: 1, 2, 3, 4, 2, 1, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6
Frame 1: 1 1 1 1 1 1 1 1 1 1 1 7 7 7 7 1 1 1 1
Frame 2: - 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Frame 3: - - 3 3 3 3 3 3 3 3 3 3 6 6 6 6 6 6 6
Frame 4: - - - 4 4 4 6 6 6 6 6 6 6 3 3 3 3 3 3
PF PF PF PF H H PF H H H H PF H PF H PF H H H
Total Page Faults (Optimal) = 7
π Step-by-Step Logic Explanation:
For LRU:
- Keep track of when each page was last used
- On a page fault, replace the page with oldest "last used" time
- Update last used time for every page reference
For Optimal:
- Look ahead in the reference string
- For each page in memory, find its next occurrence
- Replace the page whose next use is farthest in the future
- If a page is not referenced again, replace it first
Key Observation: Optimal always gives minimum page faults but is impractical as it requires future knowledge. LRU is a practical approximation.
Q3. Calculate Average Waiting Time using Round Robin (RR) with Time Quantum = 2ms
Given:
Process Arrival Time Burst Time
P1 0 ms 12 ms
P2 1 ms 15 ms
P3 2 ms 11 ms
P4 3 ms 21 ms
Time Quantum = 2 ms
Solution:
In Round Robin scheduling, each process gets CPU for time quantum (2ms) in circular manner. Processes can also finish before using the full quantum.
Gantt Chart (with actual finish times):
0-2:P1 2-4:P2 4-6:P3 6-8:P1 8-10:P4 10-12:P2 12-14:P3 14-16:P1
16-18:P4 18-20:P2 20-22:P3 22-24:P1 24-26:P4 26-28:P2 28-30:P3
30-32:P1 32-34:P4 34-36:P2 36-38:P3 38-40:P1(f)
40-42:P4 42-44:P2 44-45:P3(f) 45-47:P4 47-49:P2 49-51:P4 51-52:P2(f)
52-54:P4 54-56:P4 56-58:P4 58-59:P4(f)
Completion Times:
P1: 40 ms, P2: 52 ms, P3: 45 ms, P4: 59 ms
Turnaround Time (TAT) = Completion - Arrival
P1: 40 - 0 = 40 ms
P2: 52 - 1 = 51 ms
P3: 45 - 2 = 43 ms
P4: 59 - 3 = 56 ms
Waiting Time (WT) = TAT - Burst
P1: 40 - 12 = 28 ms
P2: 51 - 15 = 36 ms
P3: 43 - 11 = 32 ms
P4: 56 - 21 = 35 ms
Average Waiting Time = (28 + 36 + 32 + 35) / 4 = 131 / 4 = 32.75 ms
Average Turnaround Time = (40 + 51 + 43 + 56) / 4 = 190 / 4 = 47.5 ms
π Round Robin Algorithm Steps:
- Step 1: Create a ready queue and add processes as they arrive
- Step 2: Execute first process for time quantum (2ms) or until completion
- Step 3: If process not finished, add to end of ready queue
- Step 4: Pick next process from ready queue
- Step 5: Repeat until all processes complete
Important Formulas:
- Turnaround Time = Completion Time - Arrival Time
- Waiting Time = Turnaround Time - Burst Time
- Response Time = First CPU allocation - Arrival Time
Q4. What is Thrashing? Explain causes and solutions
Answer:
Definition: Thrashing is a critical condition in virtual memory systems where the computer spends most of its time swapping pages between memory and disk rather than executing actual processes. The CPU utilization drops drastically, and the system becomes extremely slow.
Detailed Explanation:
When thrashing occurs, the following cycle happens:
- A process doesn't have enough frames allocated to it
- It keeps generating page faults continuously
- The process spends most time waiting for pages to be loaded from disk
- The OS sees low CPU utilization and loads more processes
- This further reduces frames available per process
- More page faults occur, creating a vicious cycle
Causes of Thrashing:
- Too High Degree of Multiprogramming: Too many processes competing for limited memory
- Insufficient Frames: Processes allocated too few frames to hold their working set
- Poor Page Replacement Policy: Replacing pages that will be needed soon
- Excessive Demand Paging: Loading too many pages from disk
Prevention and Solutions:
-
Working Set Model:
- Monitor the working set (actively used pages) of each process
- Ensure each process has enough frames for its working set
- If total working sets exceed available memory, suspend some processes
-
Page Fault Frequency (PFF) Scheme:
- Monitor page fault rate for each process
- If rate too high, allocate more frames
- If rate too low, remove some frames
-
Local Page Replacement:
- Each process replaces only its own pages
- Prevents one process from affecting others
-
Decrease Multiprogramming:
- Swap out some processes to secondary storage
- Free up memory for remaining processes
-
Increase Physical Memory:
- Add more RAM to the system
- Reduces need for paging
- Very high disk I/O activity
- Very low CPU utilization
- Slow system response time
- High page fault rate
Indicators of Thrashing:
π UNIT II: Processes & CPU Scheduling
Process Concept
What is a Process?
A process is a program in execution. It is the basic unit of work in a modern operating system. When we write a computer program in a text file and execute it, it becomes a process that performs all the tasks mentioned in the program.
Process vs Program:
- Program: Passive entity stored on disk (executable file)
- Process: Active entity with program counter, registers, and resources
- One program can create multiple processes (e.g., opening multiple browser windows)
Process Memory Layout:
A process is divided into four sections when loaded into memory:
- Stack: Contains temporary data (function parameters, return addresses, local variables). Grows and shrinks dynamically during function calls.
- Heap: Dynamically allocated memory during process runtime (malloc, new). Grows upward.
- Data Section: Contains global and static variables. Initialized data (initialized global variables) and uninitialized data (BSS - Block Started by Symbol).
- Text Section: Contains the program code (machine instructions). Read-only, contains the compiled program code.
Process States
As a process executes, it changes between different states. A process can be in one of the following five states at any time:
| State | Description |
|---|---|
| New | The process is being created. Resources are being allocated. |
| Ready | The process is loaded in memory and waiting to be assigned to CPU by scheduler. It has all required resources except CPU time. |
| Running | The process is currently executing on the CPU. Instructions are being executed. |
| Waiting/Blocked | The process is waiting for some event to occur (I/O completion, signal, resource availability). |
| Terminated | The process has finished execution or has been killed. Resources are being deallocated. |
Process State Transitions:
NEW
|
β (admitted)
READY β------------------+
| |
β (scheduler dispatch) |
RUNNING |
| | |
| β (I/O or event) |
| WAITING ------------+ (I/O complete)
β (exit)
TERMINATED
Process Control Block (PCB)
The Process Control Block (PCB), also called Task Control Block, is a data structure maintained by the OS for every process. It contains all information needed to manage the process.
Components of PCB:
- Process ID (PID): Unique identifier for the process (integer value)
- Process State: Current state (new, ready, running, waiting, terminated)
- Program Counter (PC): Address of the next instruction to be executed
- CPU Registers: Contents of all processor registers (accumulator, index registers, stack pointers, general-purpose registers)
- CPU Scheduling Information: Process priority, pointers to scheduling queues, scheduling parameters
- Memory Management Information: Base and limit registers, page tables, segment tables
- Accounting Information: CPU time used, time limits, process numbers, account numbers
- I/O Status Information: List of open files, list of I/O devices allocated to process
- Pointers: Pointer to parent process, pointers to child processes
PCB Operations:
- PCB is created when process is created
- Updated during process execution
- Used during context switching
- Deleted when process terminates
- All PCBs are kept in a memory area reserved for OS
Context Switching
Context switching is the mechanism where the CPU switches from one process to another. When this occurs, the system must save the state of the old process and load the saved state for the new process.
Context Switch Steps:
- Save Context: Save PCB of currently running process (CPU registers, program counter, process state)
- Update PCB: Update process state to ready or waiting
- Move PCB: Move PCB to appropriate queue (ready queue or waiting queue)
- Select New Process: Scheduler selects next process from ready queue
- Load Context: Load PCB of new process (restore registers, program counter)
- Resume Execution: Start executing new process
Context Switch Time:
- Pure overhead - no useful work done during switch
- Time dependent on hardware support
- Typical time: microseconds to milliseconds
- More registers = longer context switch time
- Some systems have special hardware to speed up context switching
Process Scheduling
Scheduling Queues
The OS maintains several queues to manage processes efficiently:
- Job Queue: Set of all processes in the system (both in memory and on disk)
- Ready Queue: Set of all processes in main memory, ready and waiting to execute. Typically a linked list with header containing pointers to first and last PCB.
- Device Queues: Set of processes waiting for a specific I/O device. Each device has its own queue.
Processes migrate among these various queues based on their state transitions.
Types of Schedulers
1. Long-Term Scheduler (Job Scheduler):
- Selects which processes should be brought into the ready queue from job pool
- Controls the degree of multiprogramming (number of processes in memory)
- Invoked infrequently (seconds, minutes) - can be slow
- Selects a good process mix of I/O-bound and CPU-bound processes
- Not present in time-sharing systems
2. Short-Term Scheduler (CPU Scheduler):
- Selects which process from ready queue should execute next on CPU
- Invoked very frequently (milliseconds) - must be fast
- Makes fine-grained decisions about which process runs next
- Can be preemptive or non-preemptive
3. Medium-Term Scheduler:
- Handles swapping - removes processes from memory to reduce multiprogramming
- Swaps out processes to disk and swaps them back in later
- Improves process mix or frees memory
- Can reintroduce process into memory later
| Feature | Long-Term | Short-Term | Medium-Term |
|---|---|---|---|
| Speed | Slow | Very Fast | Moderate |
| Frequency | Minutes | Milliseconds | Seconds |
| Selection From | Job Pool (disk) | Ready Queue (memory) | Memory |
| Purpose | Control multiprogramming | Allocate CPU | Swapping |
Process vs CPU Bound
- I/O-Bound Process: Spends more time doing I/O than computations. Many short CPU bursts. Examples: text editors, database queries.
- CPU-Bound Process: Spends more time doing computations. Few very long CPU bursts. Examples: scientific calculations, video encoding.
A good process mix should include both types for optimal system performance.
CPU Scheduling Algorithms
Scheduling Criteria
Different CPU scheduling algorithms have different properties. To compare algorithms, we use several criteria:
- CPU Utilization: Keep CPU as busy as possible (0% to 100%). Goal: maximize.
- Throughput: Number of processes completed per time unit. Goal: maximize.
- Turnaround Time: Time from process submission to completion. Includes waiting time, execution time, I/O time. Goal: minimize.
- Waiting Time: Total time process spends waiting in ready queue. Goal: minimize.
- Response Time: Time from request submission until first response is produced. Important for interactive systems. Goal: minimize.
Important Formulas:
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turnaround Time - Burst Time
Response Time = Time of First CPU Allocation - Arrival Time
1. First-Come, First-Served (FCFS)
The simplest CPU scheduling algorithm. Processes are executed in the order they arrive in the ready queue. It is non-preemptive.
Characteristics:
- Easy to understand and implement
- Uses FIFO queue
- Non-preemptive
- Poor performance - high average waiting time
- Convoy effect: short processes wait for long process to complete
Convoy Effect: When a CPU-bound process gets CPU first, all I/O-bound processes must wait, leading to poor I/O device utilization.
2. Shortest Job First (SJF)
Associates each process with its next CPU burst length. When CPU is available, assign it to the process with smallest next CPU burst. If two processes have same burst time, FCFS is used to break the tie.
Two Variants:
- Non-Preemptive SJF: Once CPU given to process, it cannot be preempted until burst completes
- Preemptive SJF (SRTF - Shortest Remaining Time First): If new process arrives with shorter burst than remaining time of current process, preempt current process
Characteristics:
- Optimal - gives minimum average waiting time
- Difficulty: knowing the length of next CPU burst
- Can be estimated using exponential averaging of previous bursts
- Can cause starvation of long processes
3. Priority Scheduling
A priority is associated with each process. CPU is allocated to the process with highest priority. Equal-priority processes are scheduled in FCFS order.
Priority Assignment:
- Internally defined: time limits, memory requirements, number of open files
- Externally defined: importance of process, funds paid, political factors
Two Variants:
- Preemptive: New process with higher priority can preempt current process
- Non-Preemptive: Higher priority process waits in ready queue
Problem - Starvation: Low priority processes may never execute. Solution: Aging - gradually increase priority of processes that wait for long time.
4. Round Robin (RR)
Designed for time-sharing systems. Each process gets a small unit of CPU time (time quantum or time slice), usually 10-100 milliseconds. After time quantum expires, process is preempted and added to end of ready queue.
Characteristics:
- Preemptive version of FCFS
- Fair allocation of CPU
- Performance depends heavily on time quantum size
- No starvation
- Higher context switch overhead if quantum too small
Time Quantum Selection:
- Too large: Becomes FCFS
- Too small: Too many context switches, overhead increases
- Rule of thumb: 80% of CPU bursts should be shorter than quantum
Preemptive vs Non-Preemptive Scheduling
| Preemptive | Non-Preemptive |
|---|---|
| Process can be interrupted | Process runs until completion or waiting |
| Higher priority process can take CPU | Must wait for current process to finish |
| More overhead (context switches) | Less overhead |
| Better response time | Longer response time |
| Examples: RR, SRTF, Preemptive Priority | Examples: FCFS, SJF, Non-preemptive Priority |
| Requires hardware support (timer interrupt) | No special hardware needed |
Process Synchronization
Critical Section Problem
A critical section is a segment of code where a process accesses shared resources (variables, files, etc.) that must not be accessed by more than one process at a time. The critical section problem is to design a protocol that processes can use to cooperate.
General Structure of Process:
do {
// Entry Section
// Request permission to enter critical section
// CRITICAL SECTION
// Access shared resources
// Exit Section
// Release critical section
// Remainder Section
// Other code
} while (true);
Requirements for Solution:
-
Mutual Exclusion:
- Only one process can be in critical section at a time
- If process Pi is executing in critical section, no other process can execute in critical section
-
Progress:
- If no process is in critical section and some processes want to enter, only those processes not in remainder section can participate in decision
- Selection of next process cannot be postponed indefinitely
- Deadlock-free
-
Bounded Waiting:
- There must exist a bound on number of times other processes can enter critical section after a process has requested entry
- Ensures no starvation
- Every process gets a turn eventually
Semaphores
A semaphore is an integer variable that provides a sophisticated way to synchronize processes. It can only be accessed through two atomic operations:
Operations:
wait(S) { // Also called P() or down()
while (S <= 0)
; // busy wait
S--;
}
signal(S) { // Also called V() or up()
S++;
}
Types of Semaphores:
1. Binary Semaphore (Mutex):
- Value can be 0 or 1 only
- Used for mutual exclusion
- Initialized to 1
- Also called mutex lock
// Using binary semaphore for mutual exclusion
Semaphore mutex = 1;
Process Pi:
wait(mutex);
// Critical Section
signal(mutex);
2. Counting Semaphore:
- Value can range over unrestricted domain
- Used to control access to resource with multiple instances
- Initialized to number of available resources
// Example: 5 printers available
Semaphore printers = 5;
Process Pi:
wait(printers); // Get a printer
// Use printer
signal(printers); // Release printer
Classical Synchronization Problems
1. Producer-Consumer Problem (Bounded Buffer):
Producer process produces information consumed by consumer process. They share a fixed-size buffer.
- Producer must not add item when buffer is full
- Consumer must not remove item when buffer is empty
- They must not access buffer simultaneously
Semaphore mutex = 1; // Binary semaphore for mutual exclusion
Semaphore empty = n; // Count of empty slots
Semaphore full = 0; // Count of full slots
Producer:
while (true) {
// Produce item
wait(empty); // Wait for empty slot
wait(mutex); // Enter critical section
// Add item to buffer
signal(mutex); // Exit critical section
signal(full); // Increment full count
}
Consumer:
while (true) {
wait(full); // Wait for full slot
wait(mutex); // Enter critical section
// Remove item from buffer
signal(mutex); // Exit critical section
signal(empty); // Increment empty count
// Consume item
}
2. Dining Philosophers Problem:
5 philosophers sit at circular table with 5 chopsticks (one between each pair). Each philosopher thinks and occasionally gets hungry. To eat, philosopher needs both chopsticks.
Problem: Prevent deadlock and starvation while allowing maximum concurrency.
Simple (but flawed) solution:
Semaphore chopstick[5] = {1, 1, 1, 1, 1};
Philosopher i:
while (true) {
think();
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
eat();
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
}
// Problem: Deadlock if all pick up left chopstick simultaneously
Solutions to prevent deadlock:
- Allow at most 4 philosophers at table simultaneously
- Pick up chopsticks only if both are available (atomic operation)
- Odd philosophers pick left first, even pick right first
β Unit II: Solved Questions
Q1. What is a Process? Draw and explain Process State Diagram
Answer:
Definition: A process is a program in execution. It is the basic unit of work in a modern time-sharing system. The execution of a process must progress in a sequential fashion.
To understand this clearly, let's break it down:
- A program is a passive entity - just code sitting on disk
- A process is an active entity - a program loaded into memory and executing
- When you double-click an executable file, the OS creates a process
- One program can create multiple processes (e.g., opening 3 browser windows creates 3 browser processes)
Components of a Process:
- Program Counter: Points to next instruction to execute
- Stack: Temporary data (function parameters, local variables, return addresses)
- Data Section: Global variables, static variables
- Heap: Dynamically allocated memory during runtime
- CPU Registers: Current values of all registers
Process State Diagram:
+---------+
| NEW | β Process is being created
+---------+
|
| (admitted)
β
+---------+
+--->| READY |<---+ β Process waiting for CPU
| +---------+ |
| | |
| | (scheduler dispatch)
| β |
| +---------+ |
| | RUNNING |----+ β Process executing on CPU
| +---------+ |
| | |
| | (interrupt)
| β |
| +---------+ |
+----| WAIT/ |----+ β Waiting for I/O or event
| BLOCKED |
+---------+
|
| (exit)
β
+---------+
|TERMINATED| β Process has finished
+---------+
Detailed State Explanations:
-
NEW State:
- Process is being created
- OS is allocating PCB, memory, resources
- Not yet admitted to ready queue
-
READY State:
- Process is loaded in main memory
- Waiting to be assigned to CPU
- Has all resources except CPU time
- Multiple processes can be in ready state simultaneously
-
RUNNING State:
- Instructions are being executed on CPU
- Only one process can be running per CPU core
- Process counter is advancing
-
WAITING/BLOCKED State:
- Process is waiting for some event (I/O completion, signal)
- Cannot use CPU even if available
- Examples: waiting for disk read, keyboard input, network packet
-
TERMINATED State:
- Process has finished execution
- Resources are being deallocated
- PCB deleted from system
- May become zombie if parent hasn't read exit status
State Transitions:
- New β Ready: Admission by long-term scheduler
- Ready β Running: Dispatch by short-term scheduler
- Running β Ready: Interrupt or time quantum expired
- Running β Waiting: I/O request or waiting for event
- Waiting β Ready: I/O completion or event occurred
- Running β Terminated: Exit or kill signal
Q2. Explain different CPU Scheduling Algorithms with examples
Answer:
CPU scheduling algorithms determine which process from the ready queue should be allocated the CPU next. Let me explain each major algorithm in detail with examples.
1. First-Come, First-Served (FCFS):
The simplest scheduling algorithm. Processes are executed in the order they arrive. It is non-preemptive.
Example:
Process Arrival Time Burst Time
P1 0 24
P2 1 3
P3 2 3
Gantt Chart:
P1 | P2 | P3 |
0 24 27 30
Waiting Time:
P1 = 0 - 0 = 0
P2 = 24 - 1 = 23
P3 = 27 - 2 = 25
Average Waiting Time = (0 + 23 + 25) / 3 = 16 ms
Advantages: Simple, easy to implement
Disadvantages: Convoy effect (short processes wait for long process), poor average waiting time
2. Shortest Job First (SJF) - Non-Preemptive:
Process with shortest burst time is executed first. Optimal for minimizing average waiting time.
Example (same processes):
Order: P2 (3), P3 (3), P1 (24)
Gantt Chart:
P2 | P3 | P1 |
0 3 6 30
Waiting Time:
P2 = 0 - 1 = -1 (arrived at 1, started at 0, so we adjust)
Actually: P2 waits from 1 to 3 = 2
P3 = 3 - 2 = 1
P1 = 6 - 0 = 6
Wait calculation with arrivals:
P1: 6 - 0 = 6
P2: 0 - 1 = 0 (started immediately after arrival adjust)
P3: 3 - 2 = 1
Average WT = (6 + 2 + 1) / 3 = 3 ms
Advantages: Optimal average waiting time
Disadvantages: Starvation of long processes, need to know burst time in advance
3. Shortest Remaining Time First (SRTF) - Preemptive SJF:
Preemptive version of SJF. If a new process arrives with shorter remaining time than current process, it preempts.
Example:
Process Arrival Burst
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Timeline:
0-1: P1 runs (remaining: 7)
1: P2 arrives (4 < 7), preempts P1
1-5: P2 runs to completion
5: P4 has shortest remaining (5), runs
5-10: P4 runs to completion
10: P1 resumes (remaining: 7)
10-17: P1 runs to completion
17-26: P3 runs to completion
4. Priority Scheduling:
Each process has a priority. CPU allocated to process with highest priority. Can be preemptive or non-preemptive.
Example (lower number = higher priority):
Process Burst Priority
P1 10 3
P2 1 1 (highest)
P3 2 4
P4 1 5
P5 5 2
Order: P2, P5, P1, P3, P4
Problem: Starvation of low priority processes
Solution: Aging - increase priority over time
5. Round Robin (RR):
Each process gets small time quantum (typically 10-100 ms). After quantum expires, process preempted and added to end of ready queue.
Example with quantum = 4:
Process Burst
P1 24
P2 3
P3 3
Gantt Chart:
P1 |P2 |P3 |P1 |P1 |P1 |P1 |P1 |P1 |
0 4 7 10 14 18 22 26 30
P1: 4, 10-14, 14-18, 18-22, 22-26, 26-30
P2: 4-7 (completes)
P3: 7-10 (completes)
Good for time-sharing, fair to all processes
π Algorithm Selection Guide:
- FCFS: Simple systems, batch processing
- SJF: When burst times known, minimizes average wait
- SRTF: Better response for short processes
- Priority: When some processes more important
- Round Robin: Time-sharing systems, interactive
Q3. Calculate Average Waiting Time using SRTF (Shortest Remaining Time First)
Given:
Process Arrival Time Burst Time
P1 0 2
P2 1 5
P3 2 1
P4 3 2
P5 3 3
P6 6 6
Solution using SRTF (Preemptive SJF):
In SRTF, we always execute the process with shortest remaining time. Let me trace through time:
Time 0: P1 arrives (BT=2), execute P1
Time 1: P2 arrives (BT=5), P1 has 1 left, continue P1
Time 2: P1 completes, P3 arrives (BT=1)
Compare: P2(5), P3(1) β Execute P3
Time 3: P3 completes, P4(BT=2), P5(BT=3) arrive
Compare: P2(5), P4(2), P5(3) β Execute P4
Time 5: P4 completes
Compare: P2(5), P5(3) β Execute P5
Time 8: P5 completes, P2(5) remaining, P6(6) arrived at 6
Execute P2
Time 11: (Actually time 8+3=11 for P2 to complete)
Detailed Timeline:
0-2: P1 executes (completes at 2)
2-3: P3 executes (completes at 3)
3-5: P4 executes (completes at 5)
5-8: P5 executes (completes at 8)
8-13: P2 executes (actually time 8, remaining 5, so 8 to 13)
But P6 arrived at 6, with BT=6
Let me recalculate:
Time 5: P4 done, compare P5(3), P2(5) β P5
Time 6: P6 arrives(6), P5 has 2 left, P2 has 5
Compare: P5(2), P2(5), P6(6) β continue P5
Time 8: P5 done, compare P2(5), P6(6) β P2
Time 13: P2 done, P6 executes
Time 19: P6 done
Waiting Times:
P1: 0 (started immediately)
P2: Waited from 1 to 8 = 7 - 5 (BT) = 2?
Actually: (13-1) - 5 = 7
P3: (2-2) = 0
P4: (3-3) = 0
P5: (5-3) = 2
P6: (13-6) - 6 + 6 = 7
Correct Calculation:
Wait Time = (Completion - Arrival) - Burst
P1: (2-0)-2 = 0
P2: (13-1)-5 = 7
P3: (3-2)-1 = 0
P4: (5-3)-2 = 0
P5: (8-3)-3 = 2
P6: (19-6)-6 = 7 (arrived 6, started 13, wait=7)
Actually simpler: Started-
Arrival-Burst
Waiting Time = Start Time - Arrival Time
P1: 0-0 = 0
P2: 8-1 = 7
P3: 2-2 = 0
P4: 3-3 = 0
P5: 5-3 = 2
P6: 13-6 = 7
Average Waiting Time = (0+7+0+0+2+7)/6 = 16/6 = 2.67 ms
π SRTF Algorithm Steps:
- Step 1: At each time unit, check which processes have arrived
- Step 2: Compare remaining burst times of all available processes
- Step 3: Execute process with shortest remaining time
- Step 4: If new process arrives with shorter remaining time, preempt current process
- Step 5: Continue until all processes complete
Key Point: SRTF is preemptive SJF - always executes process with minimum remaining burst time
π UNIT III: Deadlocks
Introduction to Deadlocks
What is a Deadlock?
A deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process. None of the processes can proceed because they are all waiting for resources held by other processes in the set.
Real-World Analogy: Imagine a narrow bridge where two cars approach from opposite directions. Each car occupies half the bridge and waits for the other to back up. Neither can proceed, creating a deadlock.
Simple Example:
Process P1:
1. Acquire Resource R1
2. Acquire Resource R2
3. Release R2
4. Release R1
Process P2:
1. Acquire Resource R2
2. Acquire Resource R1
3. Release R1
4. Release R2
If P1 acquires R1 and P2 acquires R2 simultaneously,
both will wait forever for the other resource β DEADLOCK
System Model
A system consists of a finite number of resources distributed among competing processes. Resources are categorized into different types:
- Resource Types: R1, R2, ..., Rm (CPU cycles, memory space, I/O devices, files)
- Resource Instances: Each type Ri has Wi instances
- Example: If system has 2 CPUs, then CPU is resource type with 2 instances
Resource Utilization Pattern:
- Request: Process requests resource (if unavailable, process waits)
- Use: Process uses the resource (performs operation)
- Release: Process releases the resource (becomes available for others)
Deadlock Characterization
Necessary Conditions for Deadlock (Coffman Conditions)
Deadlock can arise if and only if all four of the following conditions hold simultaneously in a system:
1. Mutual Exclusion:
- At least one resource must be held in non-sharable mode
- Only one process can use the resource at a time
- If another process requests the resource, it must wait
- Example: Printer - only one process can print at a time
2. Hold and Wait:
- A process must be holding at least one resource
- AND waiting to acquire additional resources held by other processes
- Process doesn't release resources it already holds while waiting
- Example: Process holds file A, waiting for file B held by another process
3. No Preemption:
- Resources cannot be forcibly taken away from a process
- Resources can only be released voluntarily by the process holding them
- After the process has completed its task
- Example: Cannot forcibly take printer from a process that's printing
4. Circular Wait:
- A set of processes {P0, P1, ..., Pn} exists such that:
- P0 is waiting for resource held by P1
- P1 is waiting for resource held by P2
- ...
- Pn-1 is waiting for resource held by Pn
- Pn is waiting for resource held by P0
- Forms a circular chain of waiting processes
Important Note: If ANY ONE of these four conditions does not hold, deadlock cannot occur. All four must be present simultaneously.
Resource Allocation Graph (RAG)
A Resource Allocation Graph is a directed graph used to represent the state of a system in terms of processes and resources. It helps in detecting deadlocks.
Components:
- Vertices (Nodes):
- Process nodes: Represented by circles (P1, P2, ...)
- Resource nodes: Represented by rectangles (R1, R2, ...)
- Dots inside rectangles represent instances of that resource
- Edges (Arrows):
- Request Edge: Pi β Rj (process Pi requesting resource Rj)
- Assignment Edge: Rj β Pi (resource Rj allocated to process Pi)
Deadlock Detection using RAG:
- If graph contains no cycles: No deadlock
- If graph contains a cycle:
- If only one instance per resource type β Deadlock exists
- If multiple instances per resource type β Deadlock may exist (need further analysis)
Example RAG (Deadlock scenario):
P1 --requests--> R1
β |
| allocated to
| |
R2 <--requests-- P2
|
allocated to
|
β
P1
This forms a cycle: P1 β R1 β P2 β R2 β P1 (DEADLOCK!)
Methods for Handling Deadlocks
There are four main approaches to deal with deadlock problems:
| Method | Approach | When Used |
|---|---|---|
| Prevention | Ensure at least one of the four necessary conditions cannot hold | Design time |
| Avoidance | Dynamically examine resource allocation to ensure circular wait never occurs | Runtime (requires future knowledge) |
| Detection & Recovery | Allow deadlock to occur, detect it, then recover | When deadlocks are rare |
| Ignorance | Ignore the problem (Ostrich Algorithm) | When deadlocks very rare, recovery cost > prevention cost |
Deadlock Prevention
Prevention ensures that at least one of the four necessary conditions cannot hold, thus making deadlock impossible.
Breaking Each Condition:
1. Breaking Mutual Exclusion:
- Make resources sharable when possible
- Problem: Some resources inherently cannot be shared (printers, tape drives)
- Example Solution: Read-only files can be shared, use spooling for printers
- Limitation: Not applicable to all resource types
2. Breaking Hold and Wait:
- Protocol 1: Process must request all resources at once before execution
- Grant resources only if all available
- Disadvantage: Low resource utilization, starvation possible
- Protocol 2: Process can request resources only when it has none
- Must release all currently held resources before requesting new ones
- Disadvantage: May need to release and re-acquire same resources
3. Breaking No Preemption:
- If a process requests a resource that cannot be immediately allocated:
- Release all resources currently held
- Process restarts only when it can acquire all needed resources
- Alternative: Preempt resources from waiting processes
- Give to requesting process
- Preempted process added to waiting list
- Applicable to: Resources whose state can be saved and restored (CPU registers, memory)
- Not applicable to: Printers, tape drives (state cannot be easily saved)
4. Breaking Circular Wait:
- Impose total ordering on all resource types
- Assign unique number to each resource: F(R1) = 1, F(R2) = 2, ..., F(Rn) = n
- Rule: Process can request resources only in increasing order of enumeration
- Process requests Ri, then can only request Rj if F(Rj) > F(Ri)
- Alternative: If process needs Ri, must release all Rj where F(Rj) >= F(Ri)
- Proof: Cannot form circular chain if resources requested in order
Example of Breaking Circular Wait:
Resource Ordering: F(R1)=1, F(R2)=2, F(R3)=3
Process P1:
Request R1 (F=1) β
Request R2 (F=2) β (2 > 1)
Request R3 (F=3) β (3 > 2)
Process P2:
Request R2 (F=2) β
Request R1 (F=1) β NOT ALLOWED (1 < 2)
Must release R2 first before requesting R1
Deadlock Avoidance
Avoidance requires that the system has additional a priori information about which resources a process will request and use during its lifetime. The system decides for each request whether or not the process should wait to avoid potential deadlock.
Safe State
A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid deadlock. A safe sequence of processes exists.
Safe Sequence: A sequence
- Currently available resources, PLUS
- Resources held by all Pj where j < i
Key Concepts:
- Safe State: At least one safe sequence exists β No deadlock possible
- Unsafe State: No safe sequence exists β Deadlock may occur (but not guaranteed)
- Deadlock State: Subset of unsafe state β Deadlock has occurred
State Diagram:
[Safe State] β [Unsafe State] β [Deadlock State]
β
No deadlock May lead to Deadlock
guaranteed deadlock exists
Banker's Algorithm
The Banker's Algorithm is a deadlock avoidance algorithm developed by Edsger Dijkstra. It tests for safety by simulating allocation for predetermined maximum possible amounts of all resources, then makes a "safe state" check to test for possible deadlock activities.
Data Structures (n processes, m resource types):
- Available[m]: Number of available instances of each resource type
- Available[j] = k means k instances of resource Rj are available
- Max[n][m]: Maximum demand of each process
- Max[i][j] = k means process Pi may request at most k instances of Rj
- Allocation[n][m]: Currently allocated resources
- Allocation[i][j] = k means Pi is currently allocated k instances of Rj
- Need[n][m]: Remaining resource need
- Need[i][j] = Max[i][j] - Allocation[i][j]
Safety Algorithm:
1. Initialize:
Work = Available (working copy of available resources)
Finish[i] = false for all i (all processes unfinished)
2. Find an index i such that:
a) Finish[i] == false (process not finished)
b) Need[i] <= Work (process needs can be satisfied)
If no such i exists, go to step 4
3. Simulate execution:
Work = Work + Allocation[i] (process finishes, releases resources)
Finish[i] = true
Go to step 2
4. If Finish[i] == true for all i:
System is in SAFE state
Else:
System is in UNSAFE state
Resource Request Algorithm:
When process Pi requests resources (Request[i]):
1. If Request[i] <= Need[i]:
Go to step 2
Else:
Error (process exceeded maximum claim)
2. If Request[i] <= Available:
Go to step 3
Else:
Pi must wait (resources not available)
3. Pretend to allocate (tentative allocation):
Available = Available - Request[i]
Allocation[i] = Allocation[i] + Request[i]
Need[i] = Need[i] - Request[i]
4. Run Safety Algorithm:
If SAFE state:
Grant request (make allocation permanent)
Else:
Deny request (restore old state, Pi waits)
Deadlock Detection
If a system does not employ deadlock prevention or avoidance, deadlocks may occur. The system must provide:
- An algorithm to examine the state and determine if deadlock has occurred
- An algorithm to recover from deadlock
Detection Algorithm
For Single Instance Resources:
- Use Resource Allocation Graph (RAG)
- If cycle exists β Deadlock exists
- Can use cycle detection algorithms (DFS-based)
- Time complexity: O(nΒ²) where n is number of processes
For Multiple Instance Resources:
Use algorithm similar to Banker's but without Need matrix (don't need maximum requirements):
Data Structures:
- Available[m]: Available instances of each resource
- Allocation[n][m]: Currently allocated resources
- Request[n][m]: Current request of each process
Detection Algorithm:
1. Initialize:
Work = Available
Finish[i] = false if Allocation[i] != 0
Finish[i] = true if Allocation[i] == 0
2. Find i such that:
Finish[i] == false AND Request[i] <= Work
If no such i, go to step 4
3. Work = Work + Allocation[i]
Finish[i] = true
Go to step 2
4. If Finish[i] == false for some i:
Deadlock exists (those processes are deadlocked)
Else:
No deadlock
When to Invoke Detection?
- Every time a request cannot be granted: High overhead but quick detection
- At regular intervals: Balance between overhead and detection delay
- When CPU utilization drops below threshold: May indicate deadlock
Deadlock Recovery
Once deadlock is detected, several options exist for breaking it:
1. Process Termination
Option A: Abort all deadlocked processes
- Simple and guaranteed to break deadlock
- High cost - all processes lose their work
- Must restart processes from beginning
Option B: Abort one process at a time until deadlock cycle is eliminated
- After each abort, run detection algorithm to check if deadlock broken
- Overhead of repeated detection
- Selection criteria for victim process:
- Priority of the process
- How long process has computed and how much longer to completion
- Resources the process has used and needs
- How many processes will need to be terminated
- Is process interactive or batch?
2. Resource Preemption
Successively preempt resources from processes and give them to other processes until deadlock cycle is broken.
Issues to Consider:
a) Selecting a Victim:
- Which resources and processes to preempt?
- Minimize cost (CPU time used, number of resources held, etc.)
b) Rollback:
- Return process to some safe state, restart from that state
- Total Rollback: Abort process, restart from beginning (simple but loses all work)
- Partial Rollback: Rollback only as far as necessary (requires checkpoints)
c) Starvation:
- Same process may always be picked as victim
- Solution: Include number of rollbacks in cost factor (limit number of times same process can be victim)
Combined Approach
Real systems often combine approaches:
- Partition resources into hierarchical ordered categories
- Use most appropriate technique for each category
- Example:
- Internal resources (PCB, page tables): Prevention via ordering
- Main memory: Prevention via preemption
- Job resources (files, databases): Avoidance
- Swappable space: Pre-allocation
β Unit III: Solved Questions
Q1. What are the four necessary conditions for deadlock? Explain each in detail
Answer:
Deadlock can occur if and only if ALL FOUR of the following conditions hold simultaneously in a system. These are called the Coffman Conditions (named after Edward G. Coffman Jr.). Let me explain each condition in detail:
1. Mutual Exclusion (Non-Sharable Resources):
At least one resource in the system must be held in a non-sharable mode. This means only one process can use the resource at a time. If another process requests that resource, the requesting process must be delayed until the resource has been released.
Detailed Explanation:
- The resource cannot be simultaneously shared by multiple processes
- If a resource can be shared (like read-only files), deadlock cannot occur for that resource
- This condition is necessary because if resources could be shared freely, processes wouldn't need to wait for each other
Examples:
- Non-Sharable (Mutual Exclusion applies): Printer, tape drive, write access to a file, CD-ROM writer
- Sharable (No mutual exclusion): Read-only files, read-only database access
2. Hold and Wait (Partial Allocation):
A process must be holding at least one resource AND waiting to acquire additional resources that are currently being held by other processes.
Detailed Explanation:
- The process doesn't release resources it already has while waiting for new ones
- This creates a situation where resources are "tied up" in waiting processes
- The process maintains its current allocations while requesting more
Example Scenario:
- Process P1 holds File A and requests File B
- Process P2 holds File B and requests File A
- Both hold one resource and wait for another β Hold and Wait condition satisfied
3. No Preemption (Resources Cannot Be Forcibly Taken):
Resources cannot be preempted - they can only be released voluntarily by the process holding them, after that process has completed its task.
Detailed Explanation:
- The operating system cannot forcibly take a resource away from a process
- A process holding resources cannot be forced to release them
- The process must explicitly release resources itself
- This prevents the OS from breaking potential deadlocks by forcibly reallocating resources
Why This Matters:
- Some resources have states that cannot be saved/restored (e.g., printer mid-print)
- Preempting such resources would cause errors or data loss
- CPU and memory CAN be preempted (context switch, swapping)
- Printers, tape drives typically CANNOT be preempted
4. Circular Wait (Circular Chain of Waiting):
A set of waiting processes {P0, P1, ..., Pn} must exist such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, ..., Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0.
Detailed Explanation:
- Forms a circular chain where each process waits for the next
- The last process in the chain waits for the first, closing the circle
- Can be represented as a cycle in a Resource Allocation Graph
- No process in the cycle can proceed because each is waiting for the next
Visual Example:
Three processes and three resources:
P1 holds R1, wants R2
P2 holds R2, wants R3
P3 holds R3, wants R1
Circular chain: P1 β R2 (held by P2) β R3 (held by P3) β R1 (held by P1)
This forms a cycle: P1 β P2 β P3 β P1
Critical Understanding:
- ALL FOUR conditions must be present simultaneously for deadlock
- If even ONE condition is prevented, deadlock cannot occur
- This forms the basis for deadlock prevention strategies
- Detection algorithms check for circular wait in Resource Allocation Graphs
π Memory Aid - Four Conditions:
- Mutual Exclusion - resources can't be shared
- Hold and Wait - holding resources while waiting for more
- No Preemption - can't forcibly take resources
- Circular Wait - circular chain of processes waiting
Remember: Break ANY ONE condition β No deadlock possible!
Q2. Explain Banker's Algorithm with an example. Determine if the system is in safe state
Answer:
Banker's Algorithm Overview:
The Banker's Algorithm is a deadlock avoidance algorithm developed by Edsger Dijkstra. It is named after the way banks manage loans - just as a bank doesn't lend money if it cannot satisfy all customers' maximum needs, the algorithm doesn't allocate resources if it might lead to deadlock.
How It Works:
Before granting a resource request, the algorithm simulates the allocation and checks if the resulting state is safe. A safe state means the system can allocate resources to each process (up to its maximum) in some order and still avoid deadlock.
Example Problem:
Consider a system with 5 processes (P0-P4) and 3 resource types (A, B, C):
Total Resources in System: A=10, B=5, C=7
Current State:
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Step-by-Step Solution:
Step 1: Calculate Need Matrix
Need = Max - Allocation
Need
A B C
P0 7 4 3 (7-0, 5-1, 3-0)
P1 1 2 2 (3-2, 2-0, 2-0)
P2 6 0 0 (9-3, 0-0, 2-2)
P3 0 1 1 (2-2, 2-1, 2-1)
P4 4 3 1 (4-0, 3-0, 3-2)
Step 2: Apply Safety Algorithm
Available = [3, 3, 2]
Iteration 1: Find a process whose Need <= Available
- P0: Need[7,4,3] > Available[3,3,2] β
- P1: Need[1,2,2] <= Available[3,3,2] β CAN COMPLETE
Execute P1:
- P1 finishes and releases resources
- Available = Available + Allocation[P1]
- Available = [3,3,2] + [2,0,0] = [5,3,2]
- Mark P1 as finished
Iteration 2: Available = [5,3,2]
- P0: Need[7,4,3] > Available[5,3,2] β
- P2: Need[6,0,0] > Available[5,3,2] β
- P3: Need[0,1,1] <= Available[5,3,2] β CAN COMPLETE
Execute P3:
- Available = [5,3,2] + [2,1,1] = [7,4,3]
- Mark P3 as finished
Iteration 3: Available = [7,4,3]
- P0: Need[7,4,3] <= Available[7,4,3] β CAN COMPLETE
Execute P0:
- Available = [7,4,3] + [0,1,0] = [7,5,3]
- Mark P0 as finished
Iteration 4: Available = [7,5,3]
- P2: Need[6,0,0] <= Available[7,5,3] β CAN COMPLETE
Execute P2:
- Available = [7,5,3] + [3,0,2] = [10,5,5]
- Mark P2 as finished
Iteration 5: Available = [10,5,5]
- P4: Need[4,3,1] <= Available[10,5,5] β CAN COMPLETE
Execute P4:
- Available = [10,5,5] + [0,0,2] = [10,5,7]
- Mark P4 as finished
All processes finished successfully!
Safe Sequence:
CONCLUSION: System is in SAFE STATE
Deadlock will NOT occur.
Key Points About the Algorithm:
- Safe State: If we can find at least one sequence where all processes can finish, the state is safe
- Multiple Safe Sequences: There might be multiple valid safe sequences (e.g.,
might also work) - Unsafe vs Deadlock: Unsafe state doesn't guarantee deadlock, but safe state guarantees NO deadlock
Resource Request Handling:
Now suppose P1 requests additional resources [1, 0, 2]. Should we grant this request?
Current Request from P1: Request[1, 0, 2]
Check 1: Request <= Need?
Request[1,0,2] <= Need[1,2,2]? YES β
Check 2: Request <= Available?
Request[1,0,2] <= Available[3,3,2]? YES β
Simulate allocation:
New Available = [3,3,2] - [1,0,2] = [2,3,0]
New Allocation[P1] = [2,0,0] + [1,0,2] = [3,0,2]
New Need[P1] = [1,2,2] - [1,0,2] = [0,2,0]
Run Safety Algorithm with new state:
Available = [2,3,0]
Try to find safe sequence:
- P0: Need[7,4,3] > Available[2,3,0] β
- P1: Need[0,2,0] <= Available[2,3,0] β
- P2: Need[6,0,0] > Available[2,3,0] β
- P3: Need[0,1,1] > Available[2,3,0] β (C insufficient)
- P4: Need[4,3,1] > Available[2,3,0] β
Only P1 can proceed, but after P1:
Available = [2,3,0] + [3,0,2] = [5,3,2]
Now check remaining:
- P3: Need[0,1,1] <= [5,3,2] β
Continue...
If safe sequence found: GRANT REQUEST
If no safe sequence: DENY REQUEST (rollback to previous state)
π Banker's Algorithm Step-by-Step Logic:
- Calculate Need matrix: Need = Max - Allocation
- Find a process: whose Need <= Available resources
- Simulate execution: Process finishes, releases all allocated resources
- Update Available: Available = Available + Allocation of finished process
- Repeat: until all processes finish (SAFE) or no process can proceed (UNSAFE)
- For requests: Temporarily allocate, check safety, then grant or deny
Q3. Differentiate between Deadlock Prevention and Deadlock Avoidance
Answer:
Both deadlock prevention and avoidance are proactive approaches to handle deadlocks, but they work differently. Let me explain the key differences:
| Aspect | Deadlock Prevention | Deadlock Avoidance |
|---|---|---|
| Basic Approach | Ensure at least one of the four necessary conditions cannot hold | Carefully allocate resources to ensure system never enters unsafe state |
| When Applied | Design time - built into system design | Runtime - dynamic decision for each request |
| Information Needed | No advance information required | Requires knowledge of maximum resource needs in advance |
| Resource Utilization | Usually poor - resources underutilized | Better - more efficient resource use |
| Restrictiveness | Very restrictive - limits resource requests | Less restrictive - allows more flexibility |
| Complexity | Simple to implement | Complex - requires sophisticated algorithms |
| Overhead | Low runtime overhead | High runtime overhead (safety checks) |
| Deadlock Possibility | Deadlock impossible (prevented) | Deadlock possible if wrong decisions made |
| Methods/Algorithms | Break mutual exclusion, hold & wait, no preemption, or circular wait | Banker's algorithm, Resource Allocation Graph algorithm |
| Example | Impose ordering on resources (acquire R1 before R2) | Check if granting request leaves system in safe state |
Detailed Explanation:
Deadlock Prevention:
- Works by breaking at least one of the four Coffman conditions
- Makes it structurally impossible for deadlock to occur
- Example: Require all processes to request all resources at start (breaks hold-and-wait)
- Advantage: Deadlock guaranteed not to occur
- Disadvantage: May waste resources, reduce system throughput
Deadlock Avoidance:
- Allows all four conditions to potentially exist
- Makes intelligent allocation decisions to avoid unsafe states
- Example: Use Banker's algorithm to check safety before granting each request
- Advantage: Better resource utilization than prevention
- Disadvantage: Needs maximum resource requirement information, computational overhead
π UNIT IV: Device Management
Device Management Overview
What is Device Management?
Device management is one of the crucial functions of an operating system. It involves managing all I/O devices connected to the computer system, including their allocation, deallocation, and efficient utilization. The OS controls the way data is transferred between devices and processes, handles device interrupts, and provides a uniform interface for device access.
Device Management Techniques
1. Dedicated Devices
A dedicated device is assigned to one process for its entire duration until the process releases it. No other process can use the device during this time.
Characteristics:
- Allocated to a single process at a time
- Process has exclusive access
- Remains allocated until process completes or explicitly releases
- Simple to manage but may lead to inefficiency
Advantages:
- Simple allocation mechanism
- No interference from other processes
- Predictable performance for the process
- Suitable for devices that cannot be shared (e.g., tape drives)
Disadvantages:
- Device may remain idle while allocated
- Poor resource utilization
- Can lead to deadlock if not managed properly
- Other processes must wait even if device is idle
Examples: Tape drives, plotters, older printers, CD/DVD writers
2. Shared Devices
Shared devices can be used by multiple processes concurrently or in an interleaved manner. The operating system manages access to ensure proper synchronization.
Characteristics:
- Multiple processes can access simultaneously or through time-sharing
- Requires synchronization mechanisms
- Better utilization than dedicated devices
- OS scheduler manages access
Advantages:
- Better resource utilization
- Higher system throughput
- Reduced waiting time for processes
- Cost-effective (fewer devices needed)
Disadvantages:
- Complex management and synchronization required
- Potential for conflicts between processes
- Overhead of context switching between requests
- Security and privacy concerns
Examples: Hard disks, network cards, USB ports, modern printers (with spooling)
3. Virtual Devices
Virtual devices are a combination of dedicated and shared device techniques. A dedicated device is transformed into a shared device through spooling.
Spooling (Simultaneous Peripheral Operations OnLine):
Spooling is a technique where data is temporarily held in a buffer (spool) on disk. The device reads from or writes to this spool, allowing processes to continue without waiting for slow I/O devices.
How Spooling Works:
Process 1 --> | |
Process 2 --> | SPOOL | --> Device (Printer)
Process 3 --> | (Disk) |
Process n --> |________|
1. Process sends output to spool file on disk
2. Process continues execution immediately
3. Spooler daemon reads from spool and sends to device
4. Device processes requests one at a time
Advantages of Virtual Devices:
- Processes don't wait for slow devices
- Better CPU utilization
- Dedicated devices can be shared effectively
- Improves overall system performance
Disadvantages:
- Requires disk space for spooling
- Additional overhead for spool management
- Delayed output (not immediate)
Examples: Print spooling, batch processing systems
I/O Devices
Types of I/O Devices
1. Block Devices:
- Store information in fixed-size blocks
- Each block has its own address
- Can read/write blocks independently
- Support random access
- Examples: Hard disks, SSDs, USB drives, CD-ROMs
2. Character Devices:
- Deliver or accept a stream of characters
- Not addressable, no seek operation
- Sequential access only
- Examples: Keyboards, mice, serial ports, printers, network cards
Storage Devices
Disk Structure
Modern disk drives are addressed as large one-dimensional arrays of logical blocks. The disk controller manages the mapping between logical blocks and physical sectors.
Physical Structure:
- Platter: Circular disk coated with magnetic material
- Track: Concentric circles on a platter
- Sector: Subdivision of a track (typically 512 bytes or 4KB)
- Cylinder: Same track number on all platters
- Read/Write Head: Mechanism that reads/writes data
Disk Scheduling Algorithms
The operating system is responsible for using hardware efficiently. For disk drives, this means having fast access time and high disk bandwidth. Disk scheduling algorithms determine the order in which disk I/O requests are serviced.
Performance Metrics:
- Seek Time: Time to move read/write head to desired track (most significant)
- Rotational Latency: Time for desired sector to rotate under head
- Transfer Time: Time to actually transfer data
- Access Time: Seek Time + Rotational Latency + Transfer Time
1. First-Come, First-Served (FCFS) Disk Scheduling:
Requests are serviced in the order they arrive, just like FCFS process scheduling.
Advantages: Simple, fair (no starvation)
Disadvantages: May cause long seek times, inefficient
Example:
Request Queue: 98, 183, 37, 122, 14, 124, 65, 67
Initial head position: 53
Total tracks: 0-199
Service order: 53 β 98 β 183 β 37 β 122 β 14 β 124 β 65 β 67
Total head movement:
|98-53| + |183-98| + |37-183| + |122-37| + |14-122| + |124-14| + |65-124| + |67-65|
= 45 + 85 + 146 + 85 + 108 + 110 + 59 + 2
= 640 tracks
2. Shortest Seek Time First (SSTF):
Selects the request with minimum seek time from the current head position. Similar to SJF in process scheduling.
Advantages: Better throughput than FCFS, reduces average seek time
Disadvantages: Can cause starvation of far requests, not optimal
Example (same queue):
Start: 53
Closest to 53: 65 (distance 12)
From 65, closest: 67 (distance 2)
From 67, closest: 37 (distance 30)
From 37, closest: 14 (distance 23)
From 14, closest: 98 (distance 84)
From 98, closest: 122 (distance 24)
From 122, closest: 124 (distance 2)
From 124, closest: 183 (distance 59)
Service order: 53 β 65 β 67 β 37 β 14 β 98 β 122 β 124 β 183
Total: 12 + 2 + 30 + 23 + 84 + 24 + 2 + 59 = 236 tracks
3. SCAN (Elevator Algorithm):
The disk arm moves in one direction, servicing all requests in its path, until it reaches the end of the disk. Then it reverses direction and services requests in the opposite direction.
Advantages: No starvation, predictable, uniform wait time
Disadvantages: May service recently arrived requests immediately while older requests wait
Example:
Start: 53, Direction: towards 0
Requests: 98, 183, 37, 122, 14, 124, 65, 67
Service order (going towards 0 first):
53 β 37 β 14 β 0 (end) β 65 β 67 β 98 β 122 β 124 β 183
Total: |37-53| + |14-37| + |0-14| + |65-0| + |67-65| + |98-67| + |122-98| + |124-122| + |183-124|
= 16 + 23 + 14 + 65 + 2 + 31 + 24 + 2 + 59
= 236 tracks
4. C-SCAN (Circular SCAN):
Similar to SCAN, but instead of reversing direction at the end, the head returns to the beginning of the disk and starts servicing from there. Treats the disk as circular.
Advantages: More uniform wait time than SCAN, fairer
Disadvantages: Longer seek time than SCAN
Example:
Start: 53, Direction: towards 199
Requests: 98, 183, 37, 122, 14, 124, 65, 67
Service order:
53 β 65 β 67 β 98 β 122 β 124 β 183 β 199 (end) β 0 (return) β 14 β 37
Total: |65-53| + |67-65| + |98-67| + |122-98| + |124-122| + |183-124| + |199-183| + |199-0| + |14-0| + |37-14|
= 12 + 2 + 31 + 24 + 2 + 59 + 16 + 199 + 14 + 23
= 382 tracks
5. LOOK Algorithm:
Similar to SCAN but the arm only goes as far as the last request in each direction, then reverses. Doesn't go all the way to the end of the disk.
6. C-LOOK Algorithm:
Similar to C-SCAN but only goes to the last request, not the end of disk.
Buffering
What is Buffering?
A buffer is a memory area that stores data temporarily while it is being transferred between two devices or between a device and an application. Buffering compensates for the speed difference between producer and consumer of data.
Types of Buffering
1. Single Buffering:
- One buffer is used to hold data
- While user process consumes data from buffer, OS fills another block in system memory
- Simple but process must wait if buffer is being filled
2. Double Buffering:
- Two buffers are used
- While one buffer is being filled, the other can be processed
- Allows overlap of I/O and processing
- Better performance than single buffering
3. Circular Buffering (Buffer Pool):
- Multiple buffers arranged in a circular manner
- Provides greatest flexibility
- Used when data transfer is continuous
- Examples: Video streaming, audio playback
Advantages of Buffering:
- Compensates for speed mismatch between devices
- Allows processes to continue while I/O occurs
- Supports copy semantics (changes to data don't affect I/O)
- Reduces context switches
Disadvantages:
- Requires memory overhead
- Complex buffer management needed
- Can introduce latency
β Unit IV: Solved Questions
Q1. Calculate total head movement using FCFS and SSTF disk scheduling algorithms
Given:
Disk Request Queue: 95, 180, 34, 119, 11, 123, 62, 64
Initial head position: 50
Total tracks: 0-199
Solution:
A) FCFS (First-Come, First-Served):
Service requests in the order they arrive
Service Order: 50 β 95 β 180 β 34 β 119 β 11 β 123 β 62 β 64
Head Movement Calculation:
50 to 95: |95 - 50| = 45 tracks
95 to 180: |180 - 95| = 85 tracks
180 to 34: |34 - 180| = 146 tracks
34 to 119: |119 - 34| = 85 tracks
119 to 11: |11 - 119| = 108 tracks
11 to 123: |123 - 11| = 112 tracks
123 to 62: |62 - 123| = 61 tracks
62 to 64: |64 - 62| = 2 tracks
Total Head Movement (FCFS) = 45 + 85 + 146 + 85 + 108 + 112 + 61 + 2
= 644 tracks
Diagram:
0 199
|-----|-----|-----|-----|-----|-----|-----|
11 34 50 62 64 95 119 180
123
Path: 50 β 95 β 180 β 34 β 119 β 11 β 123 β 62 β 64
(lots of back-and-forth movement)
B) SSTF (Shortest Seek Time First):
Always service the nearest request to current head position
Starting at: 50
Pending requests: 95, 180, 34, 119, 11, 123, 62, 64
Step 1: From 50, find nearest
Distances: |95-50|=45, |180-50|=130, |34-50|=16, |119-50|=69,
|11-50|=39, |123-50|=73, |62-50|=12, |64-50|=14
Nearest: 62 (distance 12)
Move: 50 β 62 (12 tracks)
Remaining: 95, 180, 34, 119, 11, 123, 64
Step 2: From 62, find nearest
Distances: |95-62|=33, |180-62|=118, |34-62|=28, |119-62|=57,
|11-62|=51, |123-62|=61, |64-62|=2
Nearest: 64 (distance 2)
Move: 62 β 64 (2 tracks)
Remaining: 95, 180, 34, 119, 11, 123
Step 3: From 64, find nearest
Distances: |95-64|=31, |180-64|=116, |34-64|=30, |119-64|=55,
|11-64|=53, |123-64|=59
Nearest: 34 (distance 30)
Move: 64 β 34 (30 tracks)
Remaining: 95, 180, 119, 11, 123
Step 4: From 34, find nearest
Distances: |95-34|=61, |180-34|=146, |119-34|=85, |11-34|=23, |123-34|=89
Nearest: 11 (distance 23)
Move: 34 β 11 (23 tracks)
Remaining: 95, 180, 119, 123
Step 5: From 11, find nearest
Distances: |95-11|=84, |180-11|=169, |119-11|=108, |123-11|=112
Nearest: 95 (distance 84)
Move: 11 β 95 (84 tracks)
Remaining: 180, 119, 123
Step 6: From 95, find nearest
Distances: |180-95|=85, |119-95|=24, |123-95|=28
Nearest: 119 (distance 24)
Move: 95 β 119 (24 tracks)
Remaining: 180, 123
Step 7: From 119, find nearest
Distances: |180-119|=61, |123-119|=4
Nearest: 123 (distance 4)
Move: 119 β 123 (4 tracks)
Remaining: 180
Step 8: Last request
Move: 123 β 180 (57 tracks)
Service Order: 50 β 62 β 64 β 34 β 11 β 95 β 119 β 123 β 180
Total Head Movement (SSTF) = 12 + 2 + 30 + 23 + 84 + 24 + 4 + 57
= 236 tracks
Comparison:
FCFS Total: 644 tracks
SSTF Total: 236 tracks
Improvement: 644 - 236 = 408 tracks saved (63% reduction!)
SSTF is much more efficient than FCFS for this request pattern.
π Disk Scheduling Logic:
FCFS:
- Process requests in arrival order
- Simple but may cause excessive head movement
- Can have "wild swings" from one end of disk to other
SSTF:
- Always pick the closest request to current position
- Greedy algorithm - minimizes immediate seek time
- Better average performance than FCFS
- Can cause starvation (far requests may wait indefinitely)
Q2. Differentiate between Dedicated, Shared, and Virtual Devices
Answer:
| Aspect | Dedicated Devices | Shared Devices | Virtual Devices |
|---|---|---|---|
| Definition | Assigned to one process exclusively | Used by multiple processes concurrently | Dedicated devices transformed into shared via spooling |
| Access | Single process at a time | Multiple processes simultaneously or interleaved | Appears dedicated to each process, actually shared |
| Allocation Duration | Entire process lifetime or until release | Short time slices or concurrent access | Process writes to spool and continues |
| Utilization | Poor - device may be idle while allocated | Good - device kept busy | Excellent - processes don't wait |
| Complexity | Simple to manage | Complex - requires synchronization | Moderate - needs spool management |
| Examples | Tape drives, plotters, old printers | Hard disks, USB ports, network cards | Print spooling, batch processing |
| Waiting | Other processes must wait even if device idle | Minimal waiting with proper scheduling | No waiting - output goes to spool |
| Deadlock Risk | High if not managed properly | Medium - depends on synchronization | Low - spooling reduces resource contention |
Detailed Comparison:
1. Dedicated Devices:
- Once allocated, the device is exclusively owned by one process
- Process retains control until it voluntarily releases the device
- Suitable for devices that cannot be shared (e.g., tape drives during write operations)
- Drawback: If process doesn't fully utilize device, it sits idle but unavailable to others
- Use case: When device operations cannot be interrupted or mixed with other processes
2. Shared Devices:
- Multiple processes can access the device, managed by OS scheduler
- Time-multiplexing or true concurrent access depending on device
- Hard disks are classic example - OS interleaves requests from different processes
- Advantage: Much better resource utilization, higher throughput
- Challenge: Requires complex scheduling and synchronization mechanisms
3. Virtual Devices:
- Uses spooling technique to create illusion of dedicated device
- Process sends output to high-speed disk (spool), thinks device is dedicated
- Spooler daemon later sends data to actual device
- Example: Print spooling - you can "print" 100 documents instantly (they go to spool), printer processes them one by one
- Benefit: Processes don't wait for slow devices, better CPU utilization
π UNIT V: Information Management & File Systems
File System Concepts
What is a File?
A file is a named collection of related information recorded on secondary storage (disk). It is the smallest allotment of logical secondary storage - the OS doesn't write to disk unless it's going to a file.
File Characteristics:
- Name: Symbolic identifier, human-readable (only info kept in human-readable form)
- Identifier: Unique tag (number) identifying file within file system
- Type: Information about file format (text, binary, executable, etc.)
- Location: Pointer to device and location on device
- Size: Current file size (in bytes, words, or blocks)
- Protection: Access control information (who can read, write, execute)
- Time, Date, User ID: Data for protection, security, usage monitoring
File Types
Files can be classified based on their content and usage:
| Type | Extension | Description |
|---|---|---|
| Executable | .exe, .com, .bin | Ready-to-run machine code |
| Object | .obj, .o | Compiled but not linked code |
| Source Code | .c, .cpp, .java, .py | Human-readable program code |
| Text | .txt, .doc, .pdf | Textual data, documents |
| Multimedia | .mp3, .mp4, .jpg, .png | Audio, video, images |
| Archive | .zip, .rar, .tar | Compressed files |
| Library | .lib, .dll, .so | Libraries of routines |
File Operations
The operating system provides system calls to perform various file operations:
- Create: Allocate space, make entry in directory
- Write: Write data to file at current position, update write pointer
- Read: Read data from file at current position, advance read pointer
- Reposition (Seek): Move read/write pointer to specific location
- Delete: Remove directory entry, release file space
- Truncate: Erase contents but keep attributes
- Open: Load file attributes and locations into memory
- Close: Free up memory space used by file attributes
File Access Methods
1. Sequential Access
Information in the file is processed in order, one record after another. This is the simplest access method.
How it works:
- Read all bytes/records from the beginning
- Cannot skip around - must read sequentially
- File pointer automatically advances after each read/write
- To access record N, must first access records 1 through N-1
Operations:
read_next()- read next portion and advance pointerwrite_next()- write next portion and advance pointerreset()- go back to beginning
Advantages:
- Simple implementation
- Suitable for tape drives
- Efficient for processing entire file
- Natural for many applications (log files, batch processing)
Disadvantages:
- Slow for accessing specific records
- Inefficient if only small portion of file needed
- Cannot skip or jump to arbitrary positions easily
Examples: Compilers reading source code, text editors, log file processing
2. Direct Access (Random Access)
File is viewed as numbered sequence of blocks or records. Can read/write blocks in any order, no sequential access needed.
How it works:
- Each record/block has a logical address (number)
- Can directly access any record by specifying its number
- No need to search from beginning
- File pointer can be moved to any position
Operations:
read(n)- read block nwrite(n)- write block nseek(n)- position to block n
Advantages:
- Fast access to any part of file
- Efficient for databases and large files
- No need to read unwanted data
- Supports random access patterns
Disadvantages:
- More complex implementation
- Requires disk or random-access storage
- Not suitable for sequential media (tapes)
Examples: Databases, indexed files, virtual memory swap files
3. Indexed Access
Uses an index (like a book's index) to access records. The index contains pointers to various blocks in the file.
How it works:
- Separate index file contains key-pointer pairs
- To find a record, first search the index for the key
- Index gives pointer to actual record location
- Follow pointer to access the record
Structure:
Index File:
Key | Pointer
-------|--------
"John" | Block 5
"Mary" | Block 12
"Sam" | Block 3
Data File:
Block 3: Sam's record
Block 5: John's record
Block 12: Mary's record
Advantages:
- Very fast searching by key
- Efficient for large files
- Can have multiple indexes on same file
- Direct access without scanning entire file
Disadvantages:
- Extra space required for index
- Index must be maintained (overhead on insertions/deletions)
- More complex than direct access
Examples: Database management systems, library catalog systems
Directory Structure
What is a Directory?
A directory is a container that holds information about files. It provides a mapping between file names and their actual file structures. Directories themselves are special files maintained by the OS.
Types of Directory Structures
1. Single-Level Directory:
The simplest directory structure - all files are contained in the same directory.
Root Directory
|
|-- file1.txt
|-- file2.exe
|-- file3.doc
|-- program.c
Characteristics:
- All files in one directory
- Simple naming scheme
- Easy to implement and understand
Advantages: Simple, easy to support, fast file access
Disadvantages:
- Naming problem - all files must have unique names
- Difficult to remember file names as number grows
- No grouping capability
- Not suitable for multiple users
2. Two-Level Directory:
Separate directory for each user. Each user has their own User File Directory (UFD).
Master File Directory (MFD)
|
|-- User1 (UFD)
| |-- file1.txt
| |-- file2.c
|
|-- User2 (UFD)
| |-- file1.txt (different from User1's file1.txt)
| |-- prog.exe
|
|-- User3 (UFD)
|-- data.dat
|-- report.doc
Characteristics:
- MFD (Master File Directory) at top level
- Each entry in MFD points to a UFD for a user
- Files are addressed: user-name/file-name
- Different users can have same filename
Advantages:
- Solves naming problem - each user has separate namespace
- Efficient searching (search only user's directory)
- Isolates users from each other
Disadvantages:
- No grouping within user directory
- Difficult to share files between users
- Still limits organizational capability
3. Tree-Structured Directory (Hierarchical):
Most common structure. Allows arbitrary tree structure - directories can contain files and subdirectories.
Root (/)
|
|-- home/
| |-- user1/
| | |-- documents/
| | | |-- report.doc
| | | |-- letter.txt
| | |-- programs/
| | |-- test.c
| | |-- a.out
| |
| |-- user2/
| |-- data.txt
|
|-- bin/
| |-- ls
| |-- cat
|
|-- etc/
|-- config.sys
Characteristics:
- Unlimited depth of directories
- Directories are special files
- Each process has current (working) directory
- Paths can be absolute (from root) or relative (from current)
Path Names:
- Absolute Path: /home/user1/documents/report.doc (starts from root)
- Relative Path: documents/report.doc (from current directory)
Advantages:
- Efficient file organization and grouping
- Easy navigation
- Natural hierarchical structure
- Current directory concept simplifies naming
Disadvantages:
- More complex implementation
- File sharing still limited
- Path names can become long
4. Acyclic Graph Directory:
Extension of tree structure that allows shared subdirectories and files. Same file can appear in multiple directories via links.
Root
|
|-- user1/
| |-- doc/
| | |-- file1.txt
| | |-- shared --> (link to user2/data)
| |
| |-- programs/
|
|-- user2/
|-- data/ <-- shared by user1 via link
|-- common.dat
|-- info.txt
Implementation Methods:
- Hard Links: Multiple directory entries point to same file (same inode in UNIX)
- Symbolic Links: Special file that contains path to another file
Advantages:
- Flexible file sharing
- Avoids file duplication
- Consistency (one copy of shared file)
Disadvantages:
- Complex to implement
- Deletion becomes complicated (reference counting needed)
- Possible dangling pointers with symbolic links
5. General Graph Directory:
Most flexible - allows cycles in directory structure. Can have links that create loops.
Problem: Cycles can cause issues:
- File search may loop infinitely
- Traversal algorithms become complex
- Deletion is very complicated
Solutions:
- Garbage collection to detect cycles
- Restrict to acyclic graph (most common)
- Use reference counting
File Protection and Access Control
Why Protection?
Files need protection because:
- Multi-user systems have many users accessing files simultaneously
- Need to prevent unauthorized access
- Maintain data integrity and confidentiality
- Control what operations different users can perform
Types of Access
Different operations that can be performed on files:
- Read: View file contents
- Write: Modify file contents
- Execute: Load and run program file
- Append: Add data to end (but not modify existing)
- Delete: Remove file from system
- List/Attributes: View file name and attributes
- Rename: Change file name
- Copy: Make duplicate of file
Access Control Methods
1. Access Control List (ACL):
Each file has a list of users and their permitted access types.
File: report.doc
ACL:
User: John - Read, Write, Execute
User: Mary - Read
User: Admin - Read, Write, Execute, Delete
User: Guest - Read
Advantages:
- Fine-grained control
- Flexible - can specify exact permissions per user
- Easy to determine who has access to a file
Disadvantages:
- Can become very long for many users
- Difficult to determine all files a user can access
- More space required
2. Group-Based Access (UNIX Model):
Users are divided into groups. Each file has three sets of permissions:
- Owner: User who created the file
- Group: Set of users who share the file
- Others/World: All other users
Permissions (3 bits each):
- r (read) = 4
- w (write) = 2
- x (execute) = 1
Example: -rwxr-xr--
- : Regular file (d for directory)
rwx : Owner can read, write, execute (7)
r-x : Group can read and execute (5)
r-- : Others can only read (4)
Numeric representation: 754
Advantages:
- Compact representation (9 bits)
- Simple and efficient
- Easy to understand and manage
- Widely used (UNIX, Linux)
Disadvantages:
- Less flexible than ACL
- Only three categories of users
- Cannot specify individual user permissions easily
3. Password Protection:
Each file has a password. Users must provide password to access file.
Advantages: Simple, compact
Disadvantages:
- Password must be remembered
- Same password for all users (or many passwords needed)
- Difficult to revoke access
- Security risk if password shared or written down
File System Models
1. Simple File System Model
Basic file system with minimal features. Files are stored sequentially on disk with simple directory structure.
Characteristics: Single-level directory, sequential allocation, simple naming
2. General File System Model
More sophisticated, supports multiple levels of abstraction and various access methods.
Layers:
- Application programs
- Logical file system (manages metadata)
- File organization module (translates logical to physical blocks)
- Basic file system (issues generic commands to device driver)
- I/O control (device drivers and interrupt handlers)
- Devices (physical hardware)
3. Symbolic File System Model
Uses symbolic names and provides abstraction from physical storage. Focus on logical organization.
4. Logical File System
Manages file metadata (name, type, location, size, protection). Maintains directory structure. Provides file operations interface to users.
5. Physical File System
Manages physical blocks on storage device. Handles allocation, deallocation, and free space management. Translates logical block numbers to physical block addresses.
File Allocation Methods
1. Contiguous Allocation
Each file occupies a set of contiguous blocks on disk.
Directory Entry: Contains start block and length
Example:
File: test.txt
Start Block: 5
Length: 3 blocks
Disk: [...][test][test][test][...]
5 6 7
Advantages:
- Simple implementation
- Excellent read performance (sequential and direct access)
- Minimal seek time for sequential access
- Only starting location needed
Disadvantages:
- External fragmentation
- Difficult to grow files (need to find larger contiguous space)
- Must know file size at creation time
- Compaction needed to reclaim space
2. Linked Allocation
Each file is a linked list of disk blocks. Blocks can be scattered anywhere on disk.
Directory entry points to first block
Each block contains pointer to next block
File: example.txt
Start: Block 5
Block 5: [data][ptrβ9]
Block 9: [data][ptrβ2]
Block 2: [data][ptrβ11]
Block 11: [data][NULL]
Advantages:
- No external fragmentation
- Easy to grow files (just allocate new block and update last pointer)
- Simple free space management
- No need to know file size in advance
Disadvantages:
- Poor random access (must follow chain from beginning)
- Space overhead for pointers
- Reliability - broken link loses rest of file
- Slow access time
FAT (File Allocation Table) - Variation:
- Links stored in separate table (FAT) instead of blocks
- FAT kept in memory for faster access
- Used by MS-DOS, Windows 95/98
3. Indexed Allocation
Each file has an index block containing pointers to all blocks of the file.
Index Block (Block 10):
[ptrβ5][ptrβ9][ptrβ2][ptrβ11]
Data Blocks:
Block 5: [file data]
Block 9: [file data]
Block 2: [file data]
Block 11: [file data]
Advantages:
- Supports direct access (no sequential scan needed)
- No external fragmentation
- Easy to grow files (update index block)
- Fast random access
Disadvantages:
- Overhead of index block (wasted space for small files)
- Index block size limits file size
- Extra disk access to read index block
Solutions for Large Files:
- Linked Scheme: Index block points to other index blocks
- Multilevel Index: Index of index blocks (like paging)
- Combined Scheme (UNIX inode): Direct blocks + indirect blocks + double indirect + triple indirect
β Unit V: Solved Questions
Q1. Explain different file access methods with advantages and disadvantages
Answer:
File access methods define how records in a file are accessed and read. There are three main file access methods, each suitable for different applications. Let me explain each in detail:
1. Sequential Access Method:
In sequential access, information in the file is processed in order, one record after another. This is the simplest and most common access method.
Detailed Explanation:
- File is read from beginning to end sequentially
- Records are accessed in the order they are stored
- Cannot skip records - must read all previous records to reach a specific one
- File pointer automatically advances after each read/write operation
- Similar to reading a book page by page
Operations Supported:
read_next() - Read next record and advance pointer
write_next() - Write next record and advance pointer
reset() - Move pointer back to beginning
skip_forward(n) - Skip n records (by reading and discarding)
Advantages:
- Simple Implementation: Easy to understand and code
- Efficient for Full Processing: Best when entire file needs to be processed
- Works on Any Medium: Suitable for tape drives and disk drives
- Low Overhead: Minimal bookkeeping required
- Predictable Performance: Consistent access times
Disadvantages:
- Slow Random Access: To access record N, must read records 1 to N-1
- Inefficient for Selective Access: Wasteful if only few records needed
- Cannot Skip Records: Must process in order
- Time-Consuming: Poor performance for large files when accessing specific records
Applications:
- Text file processing
- Log file analysis
- Compiler reading source code
- Backup and archiving systems
- Batch processing applications
2. Direct Access (Random Access) Method:
Direct access allows reading or writing records in any order. The file is viewed as a numbered sequence of fixed-length records.
Detailed Explanation:
- Each record has a unique record number (relative address)
- Can jump directly to any record without reading previous ones
- Requires disk storage (not suitable for tapes)
- File seen as array of records
- Similar to accessing array elements by index
Operations Supported:
read(n) - Read record number n
write(n) - Write to record number n
position(n) - Move file pointer to record n
Advantages:
- Fast Random Access: Can access any record immediately
- Efficient: No need to read unwanted data
- Flexible: Can read records in any order
- Good for Databases: Ideal for database applications
- No Sequential Constraint: Freedom to access as needed
Disadvantages:
- Complex Implementation: More sophisticated than sequential
- Requires Disk: Cannot work with sequential media like tapes
- Fixed Record Size: Typically requires fixed-length records
- More Overhead: Need to maintain record addressing
Applications:
- Database management systems
- Virtual memory systems (page files)
- Airline reservation systems
- Banking systems
- Any application requiring quick access to specific records
3. Indexed Sequential Access Method:
Combines benefits of both sequential and direct access. Uses an index to enable direct access while maintaining sequential organization.
Detailed Explanation:
- Records stored sequentially based on a key field
- Separate index file contains key-pointer pairs
- To find a record: search index for key, then use pointer to access record
- Supports both sequential traversal and direct access
- Similar to book with both sequential pages and an index
Structure:
Index File:
Key | Pointer
--------|--------
1001 | Block 5
1002 | Block 5
2001 | Block 12
2002 | Block 12
3001 | Block 20
Data File (Sequential):
Block 5: Records with keys 1001, 1002
Block 12: Records with keys 2001, 2002
Block 20: Records with key 3001
Advantages:
- Fast Searching: Index enables quick key-based searches
- Dual Access: Supports both sequential and direct access
- Efficient for Large Files: Index much smaller than full data
- Multiple Indexes: Can have multiple indexes on different keys
- Best of Both Worlds: Flexibility of direct + efficiency of sequential
Disadvantages:
- Extra Space: Index requires additional storage
- Maintenance Overhead: Index must be updated on insertions/deletions
- Complex Implementation: More sophisticated than other methods
- Index Overhead: Must read index before accessing data
- Performance Degradation: Index can become fragmented over time
Applications:
- Relational database systems
- Library catalog systems
- Employee record systems
- Inventory management
- Any system requiring both sequential reports and random queries
Comparison Summary:
| Feature | Sequential | Direct | Indexed |
|---|---|---|---|
| Access Speed | Slow for random | Fast | Very Fast |
| Storage Medium | Any | Disk only | Disk only |
| Complexity | Simple | Moderate | Complex |
| Overhead | Low | Medium | High |
| Best For | Full file processing | Random access | Both sequential & random |
Q2. Draw and explain different types of directory structures
Answer:
A directory structure is the way files are organized and named in a file system. Different directory structures provide different levels of organization and sharing capabilities. Let me explain each type:
1. Single-Level Directory:
The simplest directory structure where all files are contained in the same directory.
βββββββββββββββββββββββββββββββ
β Root Directory β
βββββββββββββββββββββββββββββββ€
β file1.txt β
β program.exe β
β data.dat β
β report.doc β
β photo.jpg β
β music.mp3 β
βββββββββββββββββββββββββββββββ
All files in one flat list
Characteristics:
- All files at same level
- No subdirectories allowed
- Simple naming scheme
- Used in early simple systems
Advantages:
- Very simple to implement and understand
- Fast file operations (simple search)
- Easy file management
Disadvantages:
- Naming conflicts - all files must have unique names
- No grouping or organization
- Difficult to remember names as number of files grows
- Not suitable for multiple users
- Poor scalability
2. Two-Level Directory:
Separate directory for each user. Solves naming conflicts between users.
ββββββββββββββββββββββββββββββββββ
β Master File Directory (MFD) β
ββββββββββββββ¬ββββββββββββββββββββ
β
ββββββββββΌβββββββββ¬βββββββββ
β β β β
ββββΌβββ ββββΌβββ ββββΌβββ ββββΌβββ
βUser1β βUser2β βUser3β βUser4β
β UFD β β UFD β β UFD β β UFD β
ββββ¬βββ ββββ¬βββ ββββ¬βββ ββββ¬βββ
β β β β
ββββΌβββ ββββΌβββ ββββΌβββ ββββΌβββ
βfile1β βfile1β βdata β βprog β
βprog β βdoc β βfile1β βtext β
βββββββ βββββββ βββββββ βββββββ
Path: user-name/file-name
Example: user1/file1.txt
Characteristics:
- MFD contains entry for each user
- Each UFD (User File Directory) lists that user's files
- Files accessed as: username/filename
- Different users can have files with same name
Advantages:
- Solves naming conflicts between users
- Efficient searching (only search user's directory)
- Provides user isolation
- Simple security (each user has own space)
Disadvantages:
- No grouping within user's directory
- Difficult to share files between users
- No support for subdirectories
- Limited organizational capability
3. Tree-Structured (Hierarchical) Directory:
Most commonly used structure. Allows arbitrary tree of directories.
Root (/)
β
βββββββββββββββΌββββββββββββββ
β β β
home/ bin/ etc/
β β β
βββββ΄ββββ β config.sys
β β β
user1/ user2/ ls
β β cat
β β
βββββ΄ββββ data.txt
β β
docs/ programs/
β β
β test.c
β a.out
β
report.doc
letter.txt
Paths:
Absolute: /home/user1/docs/report.doc
Relative (from /home/user1): docs/report.doc
Characteristics:
- Arbitrary depth of directories
- Directories are special files containing file entries
- Each directory can contain files and subdirectories
- Current working directory concept
Advantages:
- Excellent organization and grouping
- Natural hierarchical structure
- Easy navigation
- Efficient searching (within subtrees)
- Supports both absolute and relative paths
Disadvantages:
- More complex implementation
- Path names can become very long
- File sharing between users still limited
4. Acyclic Graph Directory:
Extends tree structure to allow sharing. Same file can appear in multiple directories via links.
Root
β
βββββββΌββββββ
β β
user1/ user2/
β β
ββββ΄βββ βββ΄ββ
β β β β
docs/ pgm/ data/ work/
β β β β
β test.c shared β (link to user2/data)
β β
file1 common.dat
info.txt
Links allow shared access:
user1/docs/shared β user2/data
Both users see same actual directory/file
Link Types:
- Hard Link: Multiple directory entries point to same inode (same physical file)
- Symbolic Link (Soft Link): Special file containing path to target file
Advantages:
- Flexible file sharing between users
- No file duplication (saves space)
- Maintains consistency (one copy of shared file)
- Supports collaboration
Disadvantages:
- Complex to implement
- Deletion becomes complicated (reference counting needed)
- Dangling pointers possible with symbolic links
- Harder to guarantee acyclic property
5. General Graph Directory:
Most flexible - allows cycles in directory structure.
dir1
/ \
/ \
dir2 file1
| |
| |
dir3 ββββ
|
ββββ dir1 (creates cycle!)
Problems with Cycles:
- File search can loop infinitely
- Difficult to compute total disk usage
- Reference counting doesn't work (cycle never reaches 0)
- Deletion is very complex
Solutions:
- Use garbage collection to detect unreachable cycles
- Restrict to acyclic graph (most common approach)
- Use special algorithms for traversal that detect cycles
Q3. Explain file protection mechanisms and access control methods
Answer:
File protection is essential in multi-user systems to control access to files and prevent unauthorized access, modification, or deletion. Let me explain the various protection mechanisms:
Need for File Protection:
- Multiple users accessing the system simultaneously
- Prevent unauthorized access to sensitive data
- Maintain data integrity and confidentiality
- Control operations different users can perform
- Implement security policies
Types of Access Operations:
- Read (R): View and copy file contents
- Write (W): Modify file contents
- Execute (X): Load and run program file
- Append (A): Add data to end without modifying existing content
- Delete (D): Remove file from file system
- List (L): View file name and attributes
Access Control Methods:
1. Access Control Lists (ACL):
Each file/directory has a list specifying which users have which types of access.
File: ProjectReport.doc
Access Control List:
βββββββββββββββ¬ββββββββββββββββββββββββββ
β User β Permissions β
βββββββββββββββΌββββββββββββββββββββββββββ€
β Alice β Read, Write, Delete β
β Bob β Read, Write β
β Charlie β Read β
β Admin β Full Control β
β Guest β None β
βββββββββββββββ΄ββββββββββββββββββββββββββ
Advantages of ACL:
- Very fine-grained control - specify exact permissions for each user
- Flexible - can give different permissions to different users
- Easy to see who has access to a specific file
- Can implement complex access policies
Disadvantages of ACL:
- Can become very long with many users
- Difficult to find all files a user can access
- Requires more storage space
- More overhead in managing and checking
2. UNIX-Style Group Protection (Triplet Model):
Most widely used method. Classifies users into three categories:
- Owner (User): Creator of the file
- Group: Set of users who share the file
- Others (World): All other users in the system
Permission Bits:
Each category has 3 bits: r w x
r = read (4)
w = write (2)
x = execute (1)
Example: -rwxr-xr--
βββββββββ
ββββββββββ Others: read (4)
ββββββββββ Others: no write (0)
ββββββββββ Others: no execute (0)
ββββββββββ Group: read (4)
ββββββββββ Group: no write (0)
ββββββββββ Group: execute (1)
ββββββββββ Owner: read (4)
ββββββββββ Owner: write (2)
ββββββββββ Owner: execute (1)
Numeric: 754
Owner: 7 (rwx)
Group: 5 (r-x)
Others: 4 (r--)
Common Permission Combinations:
777 (rwxrwxrwx) - Everyone has full access
755 (rwxr-xr-x) - Owner full, others read+execute
644 (rw-r--r--) - Owner read+write, others read only
600 (rw-------) - Only owner can read+write
700 (rwx------) - Only owner has full access
Advantages of Group Model:
- Simple and efficient (only 9 bits)
- Easy to understand and manage
- Fast to check permissions
- Minimal storage overhead
- Works well for most scenarios
Disadvantages of Group Model:
- Less flexible than ACL
- Only three categories - cannot specify individual users easily
- Limited granularity
- User can only be in one group per file
3. Password Protection:
Each file is assigned a password. Users must provide correct password to access.
Variants:
- Single password for all access types
- Different passwords for read, write, execute
Advantages:
- Simple concept
- Minimal storage (just password hash)
- Easy to implement
Disadvantages:
- Users must remember many passwords
- Passwords can be forgotten
- If password shared, hard to revoke access
- Security risk if written down
- No differentiation between users with password
Modern Systems - Hybrid Approach:
Modern operating systems like Linux use combination:
- Basic UNIX permissions (owner, group, others)
- Extended ACLs for fine-grained control when needed
- Special attributes (immutable, append-only, etc.)
- SELinux/AppArmor for mandatory access control