0% found this document useful (0 votes)
4 views

Operating System Practical File-1

The document is a practical file for an Operating Systems course, detailing various experiments related to CPU scheduling algorithms, file allocation strategies, multiprogramming, and deadlock management. It includes simulations for algorithms like FCFS, SJF, Round Robin, and Banker's Algorithm, along with examples and calculations for each method. The file serves as a comprehensive guide for students to understand and implement key operating system concepts.

Uploaded by

Chandrpal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Operating System Practical File-1

The document is a practical file for an Operating Systems course, detailing various experiments related to CPU scheduling algorithms, file allocation strategies, multiprogramming, and deadlock management. It includes simulations for algorithms like FCFS, SJF, Round Robin, and Banker's Algorithm, along with examples and calculations for each method. The file serves as a comprehensive guide for students to understand and implement key operating system concepts.

Uploaded by

Chandrpal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PRACTICAL FILE

OF
OPERATING SYSTEM
(TIT 243)

Name: Paritosh Bahuguna.


ID: 60631

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

3 Simulate linked file allocation strategy. 8–9


Simulate Multiprogramming with Variable number of Tasks and
4 Multiprogramming with Fixed number of Tasks 10 – 11
12 – 14
5 Simulate Bankers Algorithm for DeadLock Avoidance

6 Simulate Bankers Algorithm for DeadLock Prevention 15 – 17


Simulate the following page replacement algorithms 18 – 20
a. FIFO
7 b. LRU
c. LFU
21 – 22
8 Simulate Paging technique for memory management.

9 Simulate disk scheduling algorithms-Scan,C-Scan. 23 – 24

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.

 FCFS (First-Come First-Serve)


Processes are scheduled in the order they arrive.
Gantt Chart:
| P1 | P2 | P3 | P4 |
0 5 8 16 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
P3 2 8 16 16 – 2 = 14 14 – 8 = 6
P4 3 6 22 22 – 3 = 19 19 – 6 = 13

 SJF (Shortest Job First) – Non-pre-emptive


At each scheduling decision, choose the process with the shortest burst time among
available processes.
Step-by-Step:
1. At t=0, only P1 is available → run P1.

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

 ROUND ROBIN (quantum = 4)


Let’s use time quantum = 4 units.
Step-by-Step:
Queue initially: [P1, P2, P3, P4]
Start at t=0
1. P1 runs 4 units → remaining 1 → t=4
2. P2 runs 3 units (finishes) → t=7
3. P3 runs 4 units → remaining 4 → t=11
4. P4 runs 4 units → remaining 2 → t=15
5. P1 runs 1 unit (finishes) → t=16
6. P3 runs 4 units (finishes) → t=20
7. P4 runs 2 units (finishes) → t=22
Gantt Chart:
| P1 | P2 | P3 | P4 | P1 | P3 | P4 |
0 4 7 11 15 16 20 22
Completion Times:

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

Block Allocation Example


Let’s assign random blocks:
Fil Allocated Blocks (Linked List)
e
F1 4 → 7 → 9 → 11 → 15
F2 2 → 5 → 12
F3 8 → 10 → 13 → 16
Each block contains:
1. Data
2. Pointer to next block
The last block’s pointer is null (end of file).

Directory Table:
We just need to store starting block for each file:

8|Page
File Starting Block
F1 4
F2 2
F3 8

How to access data?


 To access a block of a file,
start from starting block and follow pointers till desired block.
Example:
To access 3rd block of F1:
Start at 4 → next = 7 → next = 9 (3rd block)

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

 Multiprogramming with Fixed number of Tasks (MFT)


In MFT:
 Memory is divided into fixed-size partitions
 One job per partition
 If a job is smaller than partition → internal fragmentation
Let’s divide memory into 4 fixed partitions of 225 KB each (since 900/4 = 225):
Partition Size (KB) Allocated Job Wasted Space
P1 225 J1 225-150=75
P2 225 J3 225-200=25
P3 225 J5 225-100=125
P4 225 J2 225-300=Cannot fit → Skipped
 J2 (300 KB) and J4 (350 KB) → do not fit into 225 KB partition
 Internal Fragmentation Total = 75 + 25 + 125 = 225 KB
MFT Summary:
 J1, J3, J5 are loaded
 J2, J4 cannot be loaded

10 | P a g e
 225 KB wasted as internal fragmentation

 Multiprogramming with Variable number of Tasks (MVT)


In MVT:
 Memory allocated dynamically based on job size
 No fixed partitions → no internal fragmentation
Let’s allocate jobs one by one:
1. Available = 900 KB
2. Allocate J1 (150 KB) → Remaining = 750 KB
3. Allocate J2 (300 KB) → Remaining = 450 KB
4. Allocate J3 (200 KB) → Remaining = 250 KB
5. Allocate J4 (350 KB) → Cannot fit → needs 350, only 250 left
6. Allocate J5 (100 KB) → Remaining = 150 KB
Job Allocated? Memory Remaining
J1 Yes 750 KB
J2 Yes 450 KB
J3 Yes 250 KB
J4 No -
J5 Yes 150 KB
 No internal fragmentation!
 External fragmentation possible (since 150 KB free but J4 needs 350 KB)

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)

Step 2: Calculate Need Matrix


Need=Max−Allocation Need = Max - Allocation Need=Max−Allocation
Process Need (A,B,C)
P0 743
P1 122
P2 600
P3 011
P4 431

Step 3: Check for Safe Sequence


We start with:
→ Work = Available = (3,3,2)
→ Finish = [False, False, False, False, False]
Let’s check if a process can finish:

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

All processes can finish → SYSTEM IS IN SAFE STATE


 Safe Sequence = P1 → P3 → P0 → P2 → P4

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)

Step 1: Compute Need Matrix


Need=Max−allocation Need = Max - allocation Need=Max−Allocation
Process Need (A,B,C)
P0 743
P1 122
P2 600
P3 011
P4 431

Step 2: Safe Sequence Check


We start with:
 Work = Available = (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

All processes finished → SYSTEM IS IN SAFE STATE


 Safe Sequence: P1 → P3 → P0 → P2 → P4
 No deadlock will occur if requests stay within maximum declared resources.

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

 LRU (Least Recently Used)

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)

 LFU (Least Frequently Used)


We replace the least frequently used page (if tie → FIFO or LRU policy resolves).
Step Ref Frame Content Frequency Page Fault?
1 7 7 7→1 Fault
2 0 70 7→1, 0→1 Fault
3 1 701 7→1, 0→1, 1→1 Fault
4 2 012 0→1, 1→1, 2→1 Fault (7 out)
5 0 012 0→2, 1→1, 2→1 -
6 3 023 0→2, 2→1, 3→1 Fault (1 out: least freq 1)
7 0 023 0→3, 2→1, 3→1 -
8 4 034 0→3, 3→1, 4→1 Fault (2 out: least freq 1)
9 2 042 0→3, 4→1, 2→1 Fault (3 out: least freq 1)

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

Find Physical Address:


→ From page table:

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

A. SCAN (Elevator Algorithm):


Head moves in one direction → serves requests → reverses at end → serves in opposite
direction.
Step 1: Sort requests
Requests: 16, 24, 43, 82, 140, 170, 190
Head at 50, moving toward higher tracks first:
 Serve in ascending order until end:
→ 82 → 140 → 170 → 190 → then reverse → 43 → 24 → 16
(Also, end boundary is 199 if you go to end)
But in SCAN → reverses only when no more requests → no need to go till 199
Order of service:
50 → 82 → 140 → 170 → 190 → reverse → 43 → 24 → 16

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

B. C-SCAN (Circular SCAN):


 Head moves in one direction → serves requests → reaches end → jumps to beginning
→ continues in same direction
Order of service:
50 → 82 → 140 → 170 → 190 → (go to end 199) → jump to 0 → 16 → 24 → 43
 After 190, move to end (199) → jump to 0 → continue
Distance travelled:
1. 50 → 82 = 32
2. 82 → 140 = 58
3. 140 → 170 = 30
4. 170 → 190 = 20
5. 190 → 199 = 9 (move to end)
6. 199 → 0 = 199 (jump)
7. 0 → 16 = 16
8. 16 → 24 = 8
9. 24 → 43 = 19
 Total distance:
32 + 58 + 30 + 20 + 9 + 199 + 16 + 8 + 19 = 391

24 | P a g e

You might also like