Step-by-Step Approach to Solve Algorithm Problems
Step-by-Step Approach to Solve Algorithm Problems
Example Calculation:
P1 0 3 3 3-0 = 3 3-3 = 0
P2 2 3 6 6-2 = 4 4-3 = 1
P3 3 2 8 8-3 = 5 5-2 = 3
P4 5 5 13 13-5 = 8 8-5 = 3
P5 9 3 16 16-9 = 7 7-3 = 4
3. Shortest Job First (SJF) - Non-Preemptive Scheduling
Step-by-Step Approach:
Key Considerations:
● If two processes have the same burst time, select the process that arrived first.
● SJF can minimize waiting time but may lead to starvation.
1. Sort carefully: Whether based on arrival time or other parameters, sorting is crucial.
2. Be accurate with calculations: Minor mistakes can lead to incorrect final results.
3. Use tables for better clarity: Keeping columns for all calculations helps avoid confusion.
4. Draw the Gantt chart for better visualization.
5. Practice different time quantum values in Round Robin for better understanding.
6. Understand advantages and limitations of each algorithm:
○ FCFS: Simple but causes long waiting times (convoy effect).
○ SJF: Minimizes waiting time but may cause starvation.
○ Round Robin: Ensures fairness but may increase overhead.
○ Priority Scheduling: Efficient but prone to starvation.
For each process and each resource type, subtract the allocation matrix from the maximum matrix.
Example Calculation:
○ Compare each row of the Need matrix with the Work vector.
○ If the Need of a process is ≤ Available, allocate the resources and mark the process as finished.
3. Update Work:
Work=Work+Allocation of the completed process\text{Work} = \text{Work} + \text{Allocation} \text{ of the
completed process}
4. Repeat Steps 2-3 until all processes are marked as finished (safe state) or no more processes can satisfy the
condition (unsafe state).
● If all processes are marked Finished = True, the system is in a safe state, and the order in which processes
finished gives the safe sequence.
● If not all processes can finish, the system is in an unsafe state, meaning deadlock is possible.
Available Resources: (A = 3, B = 3, C = 2)
● If a safe sequence is found, print the sequence and conclude the system is in a safe state.
● If no safe sequence exists, declare the system is in an unsafe state and potentially prone to deadlock.
○ Example: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
2. Number of Page Frames: The number of slots available in memory.
○ Example: 3 frames
3. Replacement Policy: The page replacement algorithm to be applied (e.g., FIFO, LRU, Optimal).
1. Page Faults: When a requested page is not found in memory and must be loaded.
2. Page Hits: When a requested page is already in memory, avoiding a fault.
3. Hit Ratio: Hit Ratio=Number of Page HitsTotal Page Requests\text{Hit Ratio} = \frac{\text{Number of Page
Hits}}{\text{Total Page Requests}}
4. Miss Ratio: Miss Ratio=Number of Page FaultsTotal Page Requests\text{Miss Ratio} = \frac{\text{Number
of Page Faults}}{\text{Total Page Requests}}
Concept:
● The page that entered first will be removed first when a new page needs to be loaded.
● Managed using a queue (FIFO structure).
Steps to Solve:
1. Start loading pages into frames based on their arrival sequence.
2. When the frames are full and a new page arrives, remove the oldest page.
3. Track page faults by checking if a page is in memory before loading.
1 7 [7] Yes
2 0 [7, 0] Yes
3 1 [7, 0, Yes
1]
4 2 [0, 1, Yes (7
2] replaced)
5 0 [1, 2, Yes (0
0] reloaded)
6 3 [2, 0, Yes
3]
7 0 [2, 0, No
3]
8 4 [0, 3, Yes
4]
Pros:
Concept:
● The page that has been least recently used is replaced when new pages arrive.
● Managed using a stack or a tracking mechanism.
Steps to Solve:
2 0 [7, 0] Yes
3 1 [7, 0, Yes
1]
5 0 [1, 2, No (0 recently
0] used)
6 3 [2, 0, Yes
3]
7 0 [2, 0, No
3]
8 4 [0, 3, Yes
4]
Pros:
Concept:
● The page that will be used the farthest in the future is replaced.
● Requires future knowledge of page requests (ideal scenario).
Steps to Solve:
1 7 [7] Yes -
2 0 [7, 0] Yes -
3 1 [7, 0, Yes -
1]
5 0 [1, 2, No -
0]
7 0 [2, 0, No -
3]
Pros:
● FIFO: Simple but may cause "Belady's anomaly" (increase in page frames doesn't always decrease page
faults).
● LRU: Good for real-world applications where past behavior predicts future use.
● Optimal: Theoretically the best but impractical due to the need for future knowledge.
Step-by-Step Approach to Solve Disk Scheduling Problems
Disk scheduling algorithms are used to determine the order in which disk I/O requests are processed to minimize
seek time and improve performance.
● Request Queue (Sequence): 95, 180, 34, 119, 11, 123, 62, 64
● Initial Head Position: 50
● Ending Track (Disk Range): 0 to 199
○ The total number of tracks the disk arm moves to satisfy all requests.
○ Seek distance for each move = |current position - next position|.
2. Total Seek Time:
Concept:
Steps to Solve:
Calculation Example:
Concept:
● Serve the request closest to the current head position first.
Steps to Solve:
Calculation Example:
1. From 50, the closest request is 34 (distance |50 - 34| = 16).
2. From 34, the closest is 62 (distance |34 - 62| = 28).
3. Continue until all requests are processed.
Order of Execution:
50 → 62 → 64 → 34 → 11 → 95 → 119 → 123 → 180
Concept:
● The disk arm moves in one direction (towards the nearest end), serving requests until it reaches the last
request in that direction, then reverses.
Steps to Solve:
1. Sort the requests: 11, 34, 62, 64, 95, 119, 123, 180.
2. Move towards the highest request 180 and serve requests in that direction first.
3. Then move backward to the lowest request.
Concept:
● The disk arm moves in one direction and, upon reaching the last request, jumps back to the lowest request
without reversing.
Steps to Solve:
1. Sort the requests: 11, 34, 62, 64, 95, 119, 123, 180.
2. Move towards the highest request 180, and then jump to the lowest request 11 and continue.
Execution Order:
50 → 62 → 64 → 95 → 119 → 123 → 180 → (jump to) 11 → 34
○ Analyze and compare the results to determine which algorithm is optimal for a given workload.
SSTF When minimizing seek Reduces seek time Can cause starvation of
time is critical distant requests
C-LOOK When a circular pattern Reduces overall seek Jumping can add latency
is needed time