**Operating System Assignment Solution**
---
### **Assignment Question 1: Banker's Algorithm**
**Given:**
- Processes: P0, P1, P2, P3
- Resources: R1 (9 instances), R2 (3 instances), R3 (6 instances)
**Allocation Matrix:**
| Process | R1 | R2 | R3 |
|---------|----|----|----|
| P0 | 1 | 0 | 1 |
| P1 | 2 | 1 | 1 |
| P2 | 3 | 1 | 1 |
| P3 | 1 | 1 | 0 |
**Max Matrix:**
| Process | R1 | R2 | R3 |
|---------|----|----|----|
| P0 | 3 | 2 | 2 |
| P1 | 4 | 2 | 2 |
| P2 | 6 | 2 | 3 |
| P3 | 2 | 2 | 1 |
**Available Resources:**
`(R1, R2, R3) = (2, 0, 2)`
**Need Matrix (Max - Allocation):**
| Process | R1 | R2 | R3 |
|---------|----|----|----|
| P0 | 2 | 2 | 1 |
| P1 | 2 | 1 | 1 |
| P2 | 3 | 1 | 2 |
| P3 | 1 | 1 | 1 |
**Safe Sequence:** ✅ `P1 → P3 → P0 → P2`
---
### **Assignment Question 2: Fragmentation and Memory Allocation**
**(a) Fragmentation:**
- **Internal Fragmentation**: Unused space *within* allocated blocks.
- **External Fragmentation**: Small free spaces *between* blocks.
**(b) Memory Block Sizes (in KB):**
`[200, 100, 300, 400, 150, 200, 100, 300]`
**Process Sizes (in KB):**
`[90, 200, 350, 50, 200, 100, 150]`
#### **First Fit (Non-Reuse)**
| Process | Block Allocated | Fragmentation |
|---------|------------------|----------------|
| 90 | 200 | 110 |
| 200 | 300 | 100 |
| 350 | 400 | 50 |
| 50 | 150 | 100 |
| 200 | 200 |0 |
| 100 | 100 |0 |
| 150 | 300 | 150 |
#### **Best Fit**
| Process | Block Allocated | Fragmentation |
|---------|------------------|----------------|
| 90 | 100 | 10 |
| 200 | 200 |0 |
| 350 | 400 | 50 |
| 50 | 100 | 50 |
| 200 | 200 |0 |
| 100 | 150 | 50 |
| 150 | 300 | 150 |
#### **Worst Fit**
| Process | Block Allocated | Fragmentation |
|---------|------------------|----------------|
| 90 | 400 | 310 |
| 200 | 300 | 100 |
| 350 | Not Allocated | - |
| 50 | 200 | 150 |
| 200 | 200 |0 |
| 100 | 150 | 50 |
| 150 | 300 | 150 |
---
### **Assignment Question 3: Paging and Address Translation**
**Address Space:**
- 16-bit logical address → 2^16 = 65536 bytes
- Page size = 2KB = 2048 bytes = 2^11
- Offset = 11 bits
- Page Number = 5 bits
- Total pages = 2^5 = 32 pages
**Page Table:** `[5, 2, 7, 0, 3, 1, 4, 6]`
#### **Address Translation:**
| Logical Address | Page | Offset | Frame | Physical Address |
|-----------------|------|--------|--------|------------------|
| 4100 | 2 | 4 | 7 | 14340 |
| 8191 | 3 | 2047 | 0 | 2047 |
| 16383 | 7 | 2047 | 6 | 14335 |
---
### **Assignment Question 4: File Allocation Methods**
**(a) Directory Structure and Free Space Management:**
- Directory maintains file information.
- Free space tracks available disk blocks.
- **Tree structure**: organizes directories hierarchically.
- **Grouping**: keeps a list of free blocks in a group for efficient tracking.
**(b) Disk = 300 Blocks**
- File starts at block 120
- File needs 12 blocks → Allocated: 120 to 131
**If Block 123 Fails:**
#### **Contiguous Allocation:**
| Block Mapping | Issue |
|---------------|--------------------|
| 120 → 121 → ... → 123 → ... | File becomes unreadable at 123 |
#### **Linked Allocation:**
| Block Mapping | Resolution |
|---------------|---------------------|
| 120 → 121 → 122 → 124 → ... | Skip block 123 with pointer |
#### **Indexed Allocation:**
| Index Block | Data Blocks |
|-------------|----------------------|
| 500 | [120,121,122,124,...]| Skips 123 directly |
---
### **Assignment Question 5: Linux Shell Script**
```bash
#!/bin/bash
mkdir -p archive/students_data
cd archive/students_data || exit
touch [Link] [Link] [Link]
for file in [Link] [Link] [Link]
do
echo "Exam data for Fall Term - $(date)" >> "$file"
done
chmod 444 [Link] [Link] [Link]
cd ../..
ls -l archive/students_data
```