---
# System prepended metadata

title: OS Final Exam

---


## Summary of Chapter 6: Synchronization Tools

**Background**:
- Processes can execute concurrently and may be interrupted at any time, leading to partial execution.
- Concurrent access to shared data can result in data inconsistency, known as race conditions.
- The chapter addresses mechanisms to maintain data consistency and orderly execution of processes.

**Race Condition:**
- A race condition occurs when the outcome of execution depends on the non-deterministic order of process execution.
- Example: Two processes incrementing and decrementing a counter may leave it in an inconsistent state.
- Another example involves processes creating child processes and assigning duplicate process identifiers (pids).

**Critical Section Problem:**
- Processes have critical sections where they access shared resources.
- Requirements for a solution:
  1. Mutual Exclusion: Only one process can be in the critical section at a time.
  2. Progress: If no process is in the critical section, the selection of the next process cannot be postponed indefinitely.
  3. Bounded Waiting: There must be a limit on the number of times other processes enter their critical sections after a process requests to enter.

**Software Solutions:**
- **Peterson’s Solution**: Uses two variables (`turn` and `flag`) to ensure mutual exclusion, progress, and bounded waiting.
- **Modern Architecture Issues**: Reordering of instructions by processors can break the assumptions of algorithms like Peterson's Solution, necessitating memory barriers.

**Synchronization Hardware:**
- **Memory Barriers**: Ensure all changes in memory are visible to all processors before any subsequent operations.
- **Hardware Instructions**: `test_and_set` and `compare_and_swap` provide atomic operations to modify memory without interruption.

**Mutex Locks:**
- Simplifies critical section protection by providing `acquire` and `release` operations, ensuring mutual exclusion.
- Can lead to busy waiting (spinlocks).

**Semaphores:**
- More sophisticated synchronization tool than mutex locks, consisting of `wait` and `signal` operations.
- **Types**:
  - **Counting Semaphores**: Can range over an unrestricted domain.
  - **Binary Semaphores**: Range between 0 and 1, similar to mutex locks.
- Semaphores can solve various synchronization problems, like ensuring ordered execution of processes.

**Semaphore Implementation Without Busy Waiting:**
- Uses a waiting queue to block processes, reducing busy waiting.
- Operations include `block` (adds a process to the waiting queue) and `wakeup` (removes a process from the waiting queue).

**Problems with Semaphores:**
- Incorrect use, such as misordering `wait` and `signal` operations, can lead to issues.

### Focus on Key Points

**Semaphore:**
- A semaphore is an integer variable used for process synchronization.
- Two main operations:
  - `wait(S)`: Decreases the semaphore value; if the value is less than or equal to zero, the process is blocked.
  - `signal(S)`: Increases the semaphore value; if there are blocked processes, one is unblocked.
- Types: Counting semaphores and binary semaphores.

**Mutex:**
- A mutex (mutual exclusion) lock is a simpler tool to protect critical sections.
- It has two main operations:
  - `acquire()`: Locks the critical section if it is free; otherwise, the process waits.
  - `release()`: Unlocks the critical section, allowing other processes to enter.
- Common issue: Busy waiting.

**Race Condition:**
- A race condition occurs when multiple processes access and manipulate shared data concurrently, leading to inconsistent results.
- Example: Two processes modifying a shared variable like a counter without synchronization can result in unpredictable values.

**Preventing Race Conditions with Semaphore and Mutex:**
- **Using Mutex**: Ensures that only one process can enter the critical section at a time.
  ```c
  acquire(mutex);
  // critical section
  release(mutex);
  ```
- **Using Semaphore**: Can be used similarly to a mutex for mutual exclusion but also supports more complex synchronization scenarios.
  ```c
  wait(semaphore);
  // critical section
  signal(semaphore);
  ```

### Resources for Further Study

1. **Textbook**: "Operating System Concepts" by Silberschatz, Galvin, and Gagne.
2. **Online Tutorials**:
   - GeeksforGeeks: Articles on semaphores and mutexes.
   - Tutorialspoint: Operating System Synchronization.
3. **Videos**: 
   - YouTube lectures on Operating System concepts by educational channels like Neso Academy and MIT OpenCourseWare.

These resources provide detailed explanations, examples, and visual aids to help solidify your understanding of synchronization tools and race conditions.

---

## Deadlocks in Operating Systems**

#### 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. 


1. **Identification of Deadlocks**
    - **Conditions for Deadlock**:
        - **Mutual Exclusion**: Only one process can use a resource at a time.
        - **Hold and Wait**: A process holding resources can request additional resources held by other processes.
        - **No Preemption**: Resources cannot be forcibly taken from processes holding them.
        - **Circular Wait**: A circular chain of processes exists, where each process holds resources that the next process in the chain is waiting for【11:0†source】【11:4†source】.

    - **Detection**:
        - **Resource Allocation Graph**: A graph with processes and resources as nodes. A cycle in the graph indicates the potential for deadlock.
        - **Wait-for Graph**: If there is a cycle in this graph, deadlock exists. This graph is derived from the Resource Allocation Graph by collapsing resource nodes【11:2†source】.

2. **Banker's Algorithm**
    - **Purpose**: To avoid deadlock by ensuring that the system is always in a safe state.
    - **Concept**:
        - Processes declare the maximum number of resources they may need.
        - The system checks if it can allocate resources without entering an unsafe state, ensuring no circular wait conditions.
    - **Implementation**:
        - **Data Structures**: `Available`, `Maximum`, `Allocation`, `Need`.
        - **Safety Algorithm**:
            - Check if the system can allocate resources in such a way that all processes can complete.
        - **Resource Request Algorithm**:
            - When a process requests resources, simulate allocation and check if the state remains safe【11:4†source】.

3. **Example Exercise: Deadlock Snapshot Exercise (Page 8.21)**
    - **Scenario**: You have several processes and resources with given allocations and requests.
    - **Steps to Solve**:
        - **Identify Available Resources**: The total number of resources minus the resources currently allocated.
        - **Calculate Need Matrix**: Maximum resources needed minus currently allocated resources for each process.
        - **Check System State**:
            - Determine if the system is in a safe state by attempting to find a sequence of process completions.
        - **Detect Deadlock**:
            - If no sequence can be found that allows all processes to complete, a deadlock is present【11:2†source】.

### Focus on Key Points

1. **How to Identify Deadlocks**
    - Look for cycles in the Resource Allocation Graph.
    - Use the Wait-for Graph for more complex systems with multiple instances of resources.
    - Understand the four necessary conditions for deadlock: Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait.

2. **Banker’s Algorithm**
    - **Key Components**:
        - **Available**: Vector of available resources.
        - **Maximum**: Matrix of maximum resources each process may request.
        - **Allocation**: Matrix of resources currently allocated to each process.
        - **Need**: Matrix of remaining resource needs of each process (calculated as Maximum - Allocation).
    - **Procedure**:
        - Simulate resource allocation.
        - Verify if the system remains in a safe state after allocation.
        - If the system can allocate resources to meet the maximum demand of all processes without leading to a deadlock, it is in a safe state.

3. **Example Exercise Explanation (Page 8.21)**
    - **Given Data**: Initial allocation of resources, requests, and available resources.
    - **Process**:
        - Calculate the `Need` matrix.
        - Use the safety algorithm to determine if the system is in a safe state.
        - For each request, check if it can be granted while keeping the system in a safe state.
        - Example calculations show how to determine if a deadlock exists or if the system can proceed safely.

### Resources for Further Study
- **Textbook**: "Operating System Concepts" by Silberschatz, Galvin, and Gagne.
- **Online Tutorials**: TutorialsPoint and GeeksforGeeks have comprehensive articles and examples on deadlocks and Banker’s Algorithm.
- **Lecture Notes and Examples**: Review your class notes and any provided example problems, especially focusing on those similar to the exercises in the textbook.

By focusing on these key concepts and utilizing the resources mentioned, you can prepare effectively for your final exam on deadlocks and resource allocation in operating systems.

---
## Summary of Chapter 9: Main Memory

#### Background
- **Memory Access**: The CPU can directly access only main memory and registers. Registers are accessed in one CPU clock cycle, while accessing main memory can take many cycles, causing stalls. A cache helps mitigate this delay by sitting between the CPU registers and main memory.
  
#### Protection
- **Base and Limit Registers**: These ensure that a process can access only addresses within its address space, protecting user processes from each other and the operating system.

#### Address Binding
- **Stages**:
  - **Compile Time**: Absolute code generated if memory location is known.
  - **Load Time**: Relocatable code is generated if the memory location is not known.
  - **Execution Time**: Binding delayed until run time if the process can be moved during its execution.

#### Logical vs. Physical Address Space
- **Logical Address**: Generated by the CPU, also known as a virtual address.
- **Physical Address**: Address seen by the memory unit.
- **Memory-Management Unit (MMU)**: A hardware device that maps logical addresses to physical addresses at run time.

#### Dynamic Loading and Linking
- **Dynamic Loading**: Routines are not loaded until they are called, improving memory-space utilization.
- **Dynamic Linking**: Linking of libraries is postponed until execution time. A stub locates the appropriate memory-resident library routine.

#### Memory Allocation
- **Contiguous Allocation**: Memory is divided into fixed-sized partitions. Each process is contained within a single contiguous section of memory.
- **Variable Partitioning**: Multiple partitions where the degree of multiprogramming is limited by the number of partitions. Holes of various sizes are scattered throughout memory.

#### Dynamic Storage-Allocation Problem
- **Strategies**:
  - **First-Fit**: Allocates the first hole that is big enough.
  - **Best-Fit**: Allocates the smallest hole that is big enough.
  - **Worst-Fit**: Allocates the largest hole.
  
#### Fragmentation
- **External Fragmentation**: Free memory space exists outside the process address space.
- **Internal Fragmentation**: Allocated memory may be slightly larger than requested, causing wasted space within the partition.
- **Compaction**: Reduces external fragmentation by shuffling memory contents to place all free memory together.

#### Paging
- **Frames and Pages**: Physical memory is divided into fixed-sized blocks called frames, and logical memory is divided into pages of the same size.
- **Page Table**: Translates logical addresses to physical addresses. Each data/instruction access typically requires two memory accesses, which can be optimized using Translation Look-aside Buffers (TLBs).

#### Swapping
- **Swapping Process**: Processes can be temporarily swapped out of memory to a backing store and brought back as needed. This allows the total physical memory space of processes to exceed the physical memory.

---

#### Translate Physical Memory to Virtual (Slide 9.19)

**Paging Model of Logical and Physical Memory**:
- **Logical Memory**: Divided into blocks called pages.
- **Physical Memory**: Divided into fixed-sized blocks called frames.
- **Page Table**: Used to translate logical addresses to physical addresses.
- **Internal Fragmentation**: Occurs when allocated memory may be slightly larger than the requested memory.

**Address Translation Scheme**:
- Logical address from the CPU is divided into a page number and a page offset.
- The page number is used as an index into the page table.
- The page offset is combined with the base address from the page table to define the physical memory address.

#### Translation (Slide 9.20)

**Paging Hardware**:
- **Page Table**: Stores the base address of each page in physical memory.
- **Translation**: Uses the page number to find the base address and adds the page offset to get the physical address.

**Paging Example (Slide 9.21)**:
- **Logical Address**: Example with n=2 and m=4, page size of 4 bytes, and physical memory of 32 bytes (8 pages).

#### Page Table Exercise (Slide 9.22)

**Implementation of Page Table**:
- **Page-Table Base Register (PTBR)**: Points to the page table in main memory.
- **Page-Table Length Register (PTLR)**: Indicates the size of the page table.
- **Memory Access**: Requires two accesses; one for the page table and one for the data/instruction.

**Translation Look-aside Buffers (TLBs)**:
- **TLB**: A fast-lookup hardware cache that stores recent translations of logical to physical addresses to speed up the process.

**Example Exercise**:
- Given a logical address, convert it to a physical address using the page table.
- **Problem**: Logical address given, use paging hardware to find the physical address.

#### Paging System

**Concepts**:
- **Paging**: Divides physical and logical memory into fixed-size blocks.
- **Frames**: Fixed-size blocks in physical memory.
- **Pages**: Fixed-size blocks in logical memory.
- **Page Table**: Maps pages to frames.
- **Internal Fragmentation**: Possible within allocated frames.

**Hardware**:
- **Page Table**: Stored in main memory.
- **TLB**: Cache to store frequently accessed page-table entries.

#### Domain Paging

**Protection**:
- **Logical Address Space**: The set of all logical addresses generated by a program.
- **Physical Address Space**: The set of all physical memory addresses.
- **Address Binding**: The process of mapping one address space to another.

**Domain Paging**:
- **Base and Limit Registers**: Used to protect processes from accessing each other's memory.
- **Memory-Management Unit (MMU)**: Maps logical addresses to physical addresses at runtime.

#### From Logical Address Space to Physical

**Address Binding**:
- **Compile Time**: Absolute code if memory location known.
- **Load Time**: Generates relocatable code if memory location is not known at compile time.
- **Execution Time**: Binding delayed until runtime for processes that can move during execution.

**Address Translation**:
- **MMU**: Adds the value in the relocation register to every address generated by a user process.
- **Dynamic Loading**: Load routines only when called.
- **Dynamic Linking**: Postpones linking until execution time, useful for shared libraries.

### Summary

**Main Concepts**:
- **Paging**: Divides memory into pages and frames to manage memory efficiently.
- **Page Table**: Keeps track of where each page is stored in physical memory.
- **Address Translation**: Converts logical addresses from the CPU to physical addresses in memory.

**Exercises**:
- Practice converting logical addresses to physical addresses using a page table.
- Understand how TLBs improve the efficiency of address translation.


---

Here are the definitions and relevant information extracted from the provided documents about Chapter 10: Virtual Memory:

### FIFO (First-In-First-Out) Algorithm
- **Definition**: FIFO is a page replacement algorithm where the oldest page in memory is replaced first. This is managed by maintaining a queue of pages in memory, with the oldest page at the front.
- **Reference String Example**: For a reference string of 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1 and 3 frames, FIFO would result in 15 page faults【7:0†source】.
- **Page Faults**: A page fault occurs when a referenced page is not present in memory, causing the system to fetch the page from disk. The number of page faults using FIFO can be high, especially with frequent replacements of pages that might be reused soon.

### Trashing
- **Definition**: Trashing occurs when a process spends more time swapping pages in and out of memory than executing. This is typically due to insufficient memory for the working set of the process.
- **Problem Understanding**: When the total demand for frames (D) exceeds the number of available physical frames (m), thrashing can occur. This significantly degrades performance as the system is overwhelmed with page faults【7:5†source】.
- **Solution with Working Set**: The working set model can help reduce thrashing by keeping track of the set of pages a process needs during execution. If the demand exceeds the available frames, some processes may need to be suspended or swapped out to manage memory better【7:5†source】.

### Working Set
- **Definition**: The working set is the set of pages currently needed by a process for efficient execution. It changes over time as the process progresses.
- **Using Working Set to Reduce Trashing**: By monitoring the working set size, the system can ensure that enough frames are allocated to each process to avoid frequent page faults. If the working set size of all processes exceeds the available frames, the system may need to suspend or swap out some processes【7:5†source】.

### Page Replacement
- **Basic Concepts**: Page replacement algorithms are used to decide which memory pages to swap out when new pages need to be loaded. The goal is to minimize page faults and ensure efficient memory utilization.
- **Types of Algorithms**:
  - **FIFO**: Replaces the oldest page.
  - **Optimal**: Replaces the page that will not be used for the longest period. This is theoretical and used for comparison as future references are not known.
  - **LRU (Least Recently Used)**: Replaces the page that has not been used for the longest time. This uses past knowledge rather than predicting the future【7:1†source】【7:4†source】.

### Summary
The provided documents cover various aspects of virtual memory management, including FIFO and LRU page replacement algorithms, the concept of the working set, and the problem of thrashing. Understanding these concepts and their applications can help manage memory more effectively and reduce performance issues related to paging.

For more detailed explanations and examples, refer to the slides and content in the provided PDFs【7:0†source】【7:1†source】【7:4†source】【7:5†source】.

--- 

# Chapter 9 
Sure, here are the answers to the review questions from the sections specified:

### Section 9.1
#### 9.1 What two registers can be used to provide a simple form of memory protection?
- **Base register** and **Limit register**. The base register holds the smallest legal physical memory address, and the limit register specifies the size of the range.

#### 9.2 List the three different times at which address binding may occur.
- **Compile time**, **Load time**, and **Execution time**.

#### 9.3 True or False? An address generated by the CPU is also referred to as a physical address.
- **False**. An address generated by the CPU is referred to as a logical address (or virtual address).

#### 9.4 What is the hardware device that maps virtual to physical addresses?
- The **Memory Management Unit (MMU)**.

### Section 9.2
#### 9.5 What are the three strategies for selecting a free hole from the set of available holes?
- **First-fit**, **Best-fit**, and **Worst-fit**.

#### 9.6 What are the two forms of fragmentation?
- **External fragmentation** and **Internal fragmentation**.

### Section 9.3
#### 9.7 What are the two parts of an address generated by the CPU?
- **Page number** and **Page offset**.

#### 9.8 What does each entry in the page table contain?
- Each entry in the page table typically contains a **frame number** (the corresponding physical memory address) and **control bits** (such as valid/invalid bits, protection bits, and other attributes).

#### 9.9 True or False? Fragmentation can still occur in paging systems.
- **True**. While paging eliminates external fragmentation, it can still suffer from internal fragmentation.

#### 9.10 What is the term that describes when a page number is not present in the TLB?
- **TLB miss**.

### Section 9.4
#### 9.11 If a page offset is 13 bits, how large (in bytes) is the page?
- A page is \( 2^{13} \) bytes, which is **8192 bytes (8 KB)**.

#### 9.12 How many entries are in a two-level page table with a 20-bit page number?
- Assuming each level of the page table uses 10 bits for indexing, there would be \( 2^{10} \) entries in each level, resulting in \( 1024 \) entries per level.

#### 9.13 What is an alternative to hierarchical paging for large (> 32 bits) address sizes?
- **Hashed page tables** and **Inverted page tables**.

### Section 9.6
#### 9.14 True or False? IA-32 address translation involves both paging and segmentation.
- **True**.

#### 9.15 True or False? In practice, all 64 bits are used with IA-64 addressing.
- **False**. In practice, not all 64 bits are used; the actual implemented address space is typically less than 64 bits.

### Section 9.7
#### 9.16 What are the three components of a 32-bit ARM address?
- **Segment number**, **Page number**, and **Offset**.

# Chapter 10
Sure, here are the answers to the review questions from the specified sections:

### Section 10.1
#### 10.1 True or False? A program does not need to be stored in memory in its entirety.
- **True**. Virtual memory allows programs to be executed without being fully loaded into physical memory.

#### 10.2 True or False? A physical address space is at least as large as a virtual address space.
- **False**. The virtual address space is typically larger than the physical address space, allowing for the use of virtual memory.

### Section 10.2
#### 10.3 When does a page fault occur?
- A page fault occurs when a program tries to access a page that is not currently in physical memory.

#### 10.4 True or False? In a pure demand paged system a page is never brought into memory until it is needed.
- **True**.

### Section 10.3
#### 10.5 What system call initiates copy on write?
- **fork()**.

#### 10.6 True or False? The vfork() system call does not use copy on write.
- **True**.

### Section 10.4
#### 10.7 What is the simplest page replacement algorithm?
- **First-In-First-Out (FIFO)**.

#### 10.8 What is the name of the page replacement algorithm that operates by replacing the page that will not be used for the longest period of time?
- **Optimal page replacement algorithm**.

#### 10.9 What page replacement algorithm could be implemented using a stack or counters?
- **Least Recently Used (LRU)**.

#### 10.10 True or False? Approximation algorithms are almost always used when implementing LRU.
- **True**.

### Section 10.5
#### 10.11 What is the fundamental difference between global and local page replacement?
- **Global page replacement** allows a process to select a replacement frame from the set of all frames, while **local page replacement** restricts a process to selecting from its own allocated frames.

### Section 10.6
#### 10.12 What term is used to describe the situation where a process spends more time paging than executing?
- **Thrashing**.

#### 10.13 What term is used to describe the set of pages a process is currently referencing?
- **Working set**.

#### 10.14 True or False? With pure demand paging, the page fault rate is initially very high.
- **True**.

### Section 13.5
#### 10.15 True or False? Shared memory is typically not implemented using memory mapping.
- **False**.

### Section 10.8
#### 10.16 Using the buddy system, if a request for 200 KB of kernel memory is made, how much is actually allocated?
- **256 KB**. The buddy system allocates memory in powers of two.

#### 10.17 What is one benefit of using slab allocation?
- **Reduced fragmentation** and **fast memory allocation**.

### Section 10.9
#### 10.18 What is the TLB reach of a system with 4 KB page sizes and 32 entries in the TLB?
- \( 4 \text{ KB} \times 32 = 128 \text{ KB} \).

#### 10.19 True or False? 4 KB is a typical page size.
- **True**.

#### 10.20 True or False? Some systems support page sizes up to 4 MB.
- **True**.

### Section 10.10
#### 10.21 What page replacement algorithm is used by Windows?
- **Clock algorithm**, also known as **Enhanced Second-Chance algorithm**.

#### 10.22 Solaris uses the clock algorithm variation of LRU. How many hands does this algorithm employ?
- **Two hands**.