Operating System Practical File-1
Operating System Practical File-1
OF
OPERATING SYSTEM
(TIT 243)
1|Page
S.No List of Experiments in OS Page.no
Simulate the following CPU scheduling algorithms 3–5
a. FCFS
1. b. SJF
c. ROUND ROBIN
Simulate all file allocation strategies 6–7
a. Sequential
2
b. Indexed
2|Page
Practical 1
Practical: Simulate the following CPU scheduling algorithms
d. FCFS
e. SJF
f. ROUND ROBIN
Solution:
We’ll assume the following process set as an example:
Process Arrival Time Burst Time
P1 0 5
P2 1 3
P3 2 8
P4 3 6
We’ll calculate Completion Time (CT), Turnaround Time (TAT), and Waiting Time
(WT) for each process.
3|Page
2. After P1 completes at t=5, processes P2, P3, P4 have arrived.
3. Choose the shortest burst: P2 (3).
4. After P2 completes at t=8, choose P4 (6) (since it’s shorter than P3's 8).
5. Finally P3 runs.
Gantt Chart:
| P1 | P2 | P4 | P3 |
0 5 8 14 22
Now compute Turnaround Time and Waiting Time:
Process Arrival Burst Completion Turnaround Time Waiting Time
Time Time Time (CT - AT) (TAT - BT)
P1 0 5 5 5–0=5 5–5=0
P2 1 3 8 8–1=7 7–3=4
P4 3 6 14 14 – 3 = 11 11 – 6 = 5
P3 2 8 22 22 – 2 = 20 20 – 8 = 12
4|Page
Process Completion Time
P1 16
P2 7
P3 20
P4 22
Now compute Turnaround Time and Waiting Time:
Process Arrival Burst Completion Turnaround Time Waiting Time
Time Time Time (CT - AT) (TAT - BT)
P1 0 5 16 16 – 0 = 16 16 – 5 = 11
P2 1 3 7 7–1=6 6–3=3
P3 2 8 20 20 – 2 = 18 18 – 8 = 10
P4 3 6 22 22 – 3 = 19 19 – 6 = 13
5|Page
Practical 2
Practical: Simulate all file allocation strategies
c. Sequential
d. Indexed
Solution:
We’ll assume a simple disk with 20 blocks (0 to 19).
Let’s say we want to allocate the following files:
Fil Length (Blocks)
e
F1 5
F2 3
F3 4
We will manually allocate blocks for each strategy.
Sequential Allocation
In sequential allocation, each file is allocated contiguous blocks on the disk.
Let’s assume we start allocating from block 0:
File Start Block Allocated Blocks
F1 0 0,1,2,3,4
F2 5 5,6,7
F3 8 8,9,10,11
Files occupy continuous memory.
Easy access since we just need the start block and length.
Directory Table:
File Start Length
Block
F1 0 5
F2 5 3
F3 8 4
Example: To access the 3rd block of F1 → block = 0 + (3-1) = 2.
6|Page
Indexed Allocation
In indexed allocation, each file has an index block that stores the addresses of all its data
blocks.
Let’s assign index blocks arbitrarily:
Fil Index Block Allocated Data Blocks
e
F1 0 2, 4, 5, 9, 12
F2 1 3, 6, 11
F3 7 8, 10, 13, 15
The index block contains pointers to the actual data blocks.
The data blocks need not be contiguous or ordered.
Directory Table:
Fil Index Block
e
F1 0
F2 1
F3 7
The content of index blocks:
Index Block 0: [2, 4, 5, 9, 12]
Index Block 1: [3, 6, 11]
Index Block 7: [8, 10, 13, 15]
Example: To access 4th block of F1 → follow index block 0 → get block 9.
7|Page
Practical 3
Practical: Simulate linked file allocation strategy.
Solution:
Linked Allocation
In linked allocation,
Each file is a linked list of disk blocks.
Each block contains data + pointer to the next block.
Blocks are scattered anywhere on disk, no need to be contiguous.
We’ll assume a disk with 20 blocks (0 to 19).
We want to allocate:
Fil Length (Blocks)
e
F1 5
F2 3
F3 4
Directory Table:
We just need to store starting block for each file:
8|Page
File Starting Block
F1 4
F2 2
F3 8
Visualization:
File F1:
4 --> 7 --> 9 --> 11 --> 15 --> NULL
File F2:
2 --> 5 --> 12 --> NULL
File F3:
8 --> 10 --> 13 --> 16 --> NULL
9|Page
Practical 4
Practical: Simulate Multiprogramming with Variable number of Tasks and Multiprogramming
with Fixed number of Tasks
Solution:
Assumptions:
Total main memory = 1000 KB
OS occupies 100 KB
→ Available memory = 900 KB
We have the following jobs:
Job Size (KB)
J1 150
J2 300
J3 200
J4 350
J5 100
10 | P a g e
225 KB wasted as internal fragmentation
11 | P a g e
Practical 5
Practical: Simulate Bankers Algorithm for Deadlock Avoidance
Solution:
Step 1: Problem Setup
Let’s assume:
5 processes: P0, P1, P2, P3, P4
3 resource types: A, B, C
Given Matrices:
Process Allocation (A,B,C) Max (A,B,C)
P0 010 753
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Available resources (A,B,C) = (3, 3, 2)
12 | P a g e
Check P0:
Need (7,4,3) > Work (3,3,2) → ❌ cannot proceed
Check P1:
Need (1,2,2) ≤ Work (3,3,2) → ✅ can proceed
→ Allocate P1:
→ New Work = Work + Allocation = (3+2, 3+0, 2+0) = (5,3,2)
→ Finish[P1] = True
Check P0 again:
Need (7,4,3) > Work (5,3,2) → ❌ cannot proceed
Check P2:
Need (6,0,0) > Work (5,3,2) → ❌ cannot proceed
Check P3:
Need (0,1,1) ≤ Work (5,3,2) → ✅ can proceed
→ Allocate P3:
→ New Work = (5+2, 3+1, 2+1) = (7,4,3)
→ Finish[P3] = True
Check P0 again:
Need (7,4,3) ≤ Work (7,4,3) → ✅ can proceed
→ Allocate P0:
→ New Work = (7+0, 4+1, 3+0) = (7,5,3)
→ Finish[P0] = True
Check P2:
Need (6,0,0) ≤ Work (7,5,3) → ✅ can proceed
→ Allocate P2:
→ New Work = (7+3, 5+0, 3+2) = (10,5,5)
→ Finish[P2] = True
Check P4:
Need (4,3,1) ≤ Work (10,5,5) → ✅ can proceed
13 | P a g e
→ Allocate P4:
→ New Work = (10+0,5+0,5+2) = (10,5,7)
→ Finish[P4] = True
14 | P a g e
Practical 6
Practical: Simulate Bankers Algorithm for Deadlock Prevention
Solution:
Banker’s Algorithm is actually a Deadlock Avoidance algorithm, not prevention (deadlock
prevention uses different methods like pre-emption, hold & wait avoidance, etc.).
Problem Setup
We have:
3 resource types: A, B, C
5 processes: P0, P1, P2, P3, P4
Initial Matrices:
Process Allocation (A,B,C) Max (A,B,C)
P0 010 753
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Available resources: (A,B,C) = (3,3,2)
15 | P a g e
Finish = [False, False, False, False, False]
Check P0:
Need (7,4,3) > Work (3,3,2) → ❌ cannot proceed
Check P1:
Need (1,2,2) ≤ Work (3,3,2) → ✅ can proceed
→ Allocate P1 →
New Work = Work + Allocation = (3+2, 3+0, 2+0) = (5,3,2)
→ Finish[P1] = True
Check P0 again:
Need (7,4,3) > Work (5,3,2) → ❌
Check P2:
Need (6,0,0) > Work (5,3,2) → ❌
Check P3:
Need (0,1,1) ≤ Work (5,3,2) → ✅
→ Allocate P3 →
New Work = (5+2, 3+1, 2+1) = (7,4,3)
→ Finish[P3] = True
Check P0 now:
Need (7,4,3) ≤ Work (7,4,3) → ✅
→ Allocate P0 →
New Work = (7+0, 4+1, 3+0) = (7,5,3)
→ Finish[P0] = True
Check P2:
Need (6,0,0) ≤ Work (7,5,3) → ✅
→ Allocate P2 →
New Work = (7+3, 5+0, 3+2) = (10,5,5)
→ Finish[P2] = True
Check P4:
16 | P a g e
Need (4,3,1) ≤ Work (10,5,5) → ✅
→ Allocate P4 →
New Work = (10+0, 5+0, 5+2) = (10,5,7)
→ Finish[P4] = True
17 | P a g e
Practical 7
Practical: Simulate the following page replacement algorithms
a. FIFO
b. LRU
c. LFU
Solution:
Given:
Reference String:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
Number of frames = 3
FIFO (First-In-First-Out)
We replace the oldest page first.
Let’s process:
Step Ref Frame Content Page Fault?
1 7 7 Fault
2 0 70 Fault
3 1 701 Fault
4 2 012 Fault (7 out)
5 0 012 -
6 3 123 Fault (0 out)
7 0 230 Fault (1 out)
8 4 304 Fault (2 out)
9 2 042 Fault (3 out)
10 3 423 Fault (0 out)
11 0 230 Fault (4 out)
12 3 230 -
13 2 230 -
Total Page Faults (FIFO) = 9
18 | P a g e
We replace the page that was least recently used.
Step Ref Frame Content Page Fault?
1 7 7 Fault
2 0 70 Fault
3 1 701 Fault
4 2 012 Fault (7 out)
5 0 012 -
6 3 123 Fault (0 out)
7 0 230 Fault (1 out)
8 4 304 Fault (2 out)
9 2 042 Fault (3 out)
10 3 423 Fault (0 out)
11 0 230 Fault (4 out)
12 3 230 -
13 2 230 -
Total Page Faults (LRU) = 10
(NOTE: identical to FIFO in this example — sometimes possible)
19 | P a g e
10 3 023 0→3, 2→1, 3→1 Fault (4 out: least freq 1)
11 0 023 0→4, 2→1, 3→1 -
12 3 023 0→4, 2→1, 3→2 -
13 2 023 0→4, 2→2, 3→2 -
Total Page Faults (LFU) = 9
20 | P a g e
Practical 8
Practical: Simulate Paging technique for memory management.
Solution:
What is Paging?
In paging, physical memory is divided into fixed-size frames and logical memory into pages
of the same size.
Each page is mapped to a frame via a page table.
Example Setup:
Logical Address Space = 8 pages (0-7)
Physical Memory = 32 frames (0-31)
Page size = 1 KB (each page/frame = 1 KB)
Suppose the following page table:
Page No Frame No
0 5
1 9
2 17
3 1
4 8
5 20
6 4
7 12
Virtual Address:
Suppose a process generates the logical address:
Logical Address = 3 KB + 300 bytes
Page size = 1 KB →
→ Page Number = 3 (since 3 KB falls in page 3)
→ Offset = 300 bytes
21 | P a g e
Page 3 is in Frame 1
Physical Address formula:
Physical Address = (Frame Number X Frame Size) + Offset
Substitute:
Physical Address = (1 X 1024) + 300
Physical Address = 1024 + 300 = 1324
Physical Address = 1324 bytes
Explanation:
→ Logical address 3 KB + 300 bytes → mapped to Frame 1 → → Physical Address = 1324
bytes
22 | P a g e
Practical 9
Practical: Simulate disk scheduling algorithms-Scan, C-Scan.
Solution:
Problem Setup:
Disk queue of requests:
82, 170, 43, 140, 24, 16, 190
Initial head position = 50
Disk size = 200 tracks (0–199)
Head moves toward higher track numbers first
Distance travelled:
1. 50 → 82 = 32
2. 82 → 140 = 58
3. 140 → 170 = 30
4. 170 → 190 = 20
5. 190 → 43 = (reverse) → 147
6. 43 → 24 = 19
7. 24 → 16 = 8
Total distance:
23 | P a g e
32 + 58 + 30 + 20 + 147 + 19 + 8 = 314
24 | P a g e