0% found this document useful (0 votes)
18 views8 pages

Aim and Algo

The document outlines various scheduling algorithms including Shortest Job First (SJF), Round Robin, and Priority Scheduling, detailing their aims, algorithms, and outputs for calculating waiting and turnaround times. It also covers the implementation of mutual exclusion using semaphores, Banker's Algorithm for system safety, and various page replacement algorithms like FIFO, LRU, and Optimal. Additionally, it describes file allocation strategies and disk scheduling algorithms such as FCFS, SSTF, and SCAN, providing example outputs for each.

Uploaded by

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

Aim and Algo

The document outlines various scheduling algorithms including Shortest Job First (SJF), Round Robin, and Priority Scheduling, detailing their aims, algorithms, and outputs for calculating waiting and turnaround times. It also covers the implementation of mutual exclusion using semaphores, Banker's Algorithm for system safety, and various page replacement algorithms like FIFO, LRU, and Optimal. Additionally, it describes file allocation strategies and disk scheduling algorithms such as FCFS, SSTF, and SCAN, providing example outputs for each.

Uploaded by

SARDAR HUSSAIN S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SJF Scheduling algorithm

Aim: Implement a Shortest Job First (SJF) scheduling algorithm to calculate the waiting time and turnaround time for a
set of processes.

Algorithm:

1. Sort: Arrange the processes in increasing order of burst times.

2. Calculate Waiting Time:

o Set the waiting time of the first process to 0.

o For each subsequent process, waiting time = previous process's burst time + previous process's waiting
time.

3. Calculate Turnaround Time:

o For each process, turnaround time = burst time + waiting time.

4. Output: Print the process details along with average waiting and turnaround times.

Output:

For the input processes with burst times [6, 8, 7, 3], the output will look like:

Process Burst Time Waiting Time Turnaround Time


4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24

Average waiting time: 7.0


Average turnaround time: 13.0

Round Robin Scheduling Algorithm

Aim

To implement the Round Robin Scheduling Algorithm to calculate the average waiting time and turnaround time for
processes with given burst times and a fixed time quantum.

Algorithm

1. Input: Burst times, process IDs, and quantum.

2. Initialize: Remaining burst times, waiting times, and turnaround times.

3. Process Execution: Execute each process cyclically based on the quantum until all are completed:

o Update remaining time.

o Calculate waiting time.

4. Turnaround Time: Compute turnaround time for each process as the sum of waiting time and burst time.

5. Output: Display process details and averages for waiting and turnaround times.
Output :
Process Burst Time Waiting Time Turnaround Time
1 6 11 17
2 8 13 21
3 7 17 24
4 3 12 15

Average waiting time: 13.25


Average turnaround time: 19.25

Priority Scheduling (Non-Preemptive) Algorithm

Aim

Sort processes by priority and calculate/display their waiting and turnaround times plus averages.

Algorithm

1. Sort: Arrange processes (with burst times and priorities) in ascending order of priority.

2. Waiting Time: Set first waiting time to 0; for others, sum previous burst times.

3. Turnaround Time: For each process, add burst time and waiting time.

4. Output: Display each process's details and compute averages.

Output :
Process Burst Time Priority Waiting Time Turnaround Time
2 8 1 0 8
1 6 2 8 14
3 7 3 14 21
4 3 4 21 24

Average waiting time: 10.25


Average turnaround time: 16.25

5. Implement Mutual Exclusion by Using Semaphore

Aim

Implement the Producer-Consumer problem using POSIX threads, semaphores, and a mutex.

Algorithm

1. Init: Set up a shared buffer, semaphores (empty, full), and a mutex.

2. Produce: Wait for empty, lock mutex, add item, unlock mutex, signal full.

3. Consume: Wait for full, lock mutex, remove item, unlock mutex, signal empty.

4. Main: Create and join producer/consumer threads, then clean up.

Output :
Produced: 0
Produced: 1
Consumed: 0
Produced: 2
Consumed: 1
Produced: 3
Consumed: 2
Produced: 4
Consumed: 3
Produced: 5
Consumed: 4
...
Exp 6 Banker's Algorithm
Aim

To verify system safety with Banker's Algorithm by finding a safe sequence.

Algorithm

1. Init: Set work = available; mark all processes unfinished.

2. Loop:

o Find an unfinished process whose need ≤ work.

o Mark it finished, add its allocation to work, and append it to the safe sequence.

3. Result:

o If all processes are finished, the system is safe; otherwise, unsafe.

Output :
Safe Sequence: P1 -> p3 -> p4 -> p0 -> p2
System is in a safe state

Exp 7

Aim

To simulate the FIFO page replacement algorithm, replacing pages in a fixed frame buffer and counting page faults.

Algorithm

1. Input:

o A sequence of page references and a fixed number of frames.

2. For Each Page:

o Check if the page is already present in the frames.

o If not, insert the page into the frame using FIFO order (via modulo indexing) and increment the page
fault count.

3. Output:

o Display the current state of the frames after each access and print the total number of page faults.

Output :
Page: 0 | Frames: 0 -1 -1 -1
Page: 1 | Frames: 0 1 -1 -1
Page: 2 | Frames: 0 1 2 -1
Page: 1 | Frames: 0 1 2 -1
Page: 3 | Frames: 0 1 2 -1
Page: 0 | Frames: 0 1 2 0
Page: 3 | Frames: 0 1 2 0
Total Page Faults: 4

8. Write a Program to Implement Various Page Replacement Algorithms

Aim
To implement the FIFO page replacement algorithm to manage memory frames, replace pages, and calculate the total
number of page faults.

Algorithm

1. Initialize:

o Set the frames to empty (-1).

o Initialize the page fault counter and frame pointer.

2. For Each Page Reference:

o Check if the page is already in the frames.

o If not, replace the oldest page using FIFO and increment the page fault counter.

3. Output Frames:

o Display the current state of the frames after each page reference.

4. Final Output:

o Print the total number of page faults.

Output :
Page: 0 | Frames: 0 -1 -1
Page: 1 | Frames: 0 1 -1
Page: 2 | Frames: 0 1 2
Page: 1 | Frames: 0 1 2
Page: 3 | Frames: 3 1 2
Page: 0 | Frames: 3 0 2
Page: 3 | Frames: 3 0 2
Total Page Faults: 5

Code for LRU Page Replacement:

Aim

Simulate Least Recently Used (LRU) page replacement in a fixed number of frames and count page faults.

Algorithm

1. Iterate through each page:

o Check if the page is already in the frames.

2. If the page is not present:

o Increment the page fault count.

o Look for an empty frame; if found, use it.

o Otherwise, select a frame (here, a fallback to index 0 is used) to replace with the new page.

3. Output:

o Print the frame status after each page reference.

o Display the total number of page faults once all pages have been processed.

Output :
Page: 0 | Frames: 0 -1 -1
Page: 1 | Frames: 0 1 -1
Page: 2 | Frames: 0 1 2
Page: 1 | Frames: 0 1 2
Page: 3 | Frames: 3 1 2
Page: 0 | Frames: 3 0 2
Page: 3 | Frames: 3 0 2
Total Page Faults: 4

Page Replacement Algorithm: Optimal (OPT)


Aim

Simulate the optimal page replacement algorithm to minimize page faults.

Algorithm

1. For each page in the reference string, check if it is in frames.

2. If not, if an empty frame exists, load the page; otherwise, replace the page that is not used for the longest future
duration.

3. Print the frame status and total page faults.

Output :
Optimal Page Replacement Algorithm
Frames: 7 Empty Empty Empty
Frames: 7 0 Empty Empty
Frames: 7 0 1 Empty
Frames: 7 0 1 2
Frames: 7 1 2 0
Frames: 3 2 1 2
Frames: 3 0 1 2
Frames: 3 0 4 2
Frames: 3 0 4 2
Frames: 3 0 4 2
Frames: 3 0 4 2
Frames: 3 0 4 2

Total Page Faults: 9

9. Implement File Allocation Strategies Using C Programs

Aim

Simulate sequential file allocation by determining the starting block for each file based on its size.

Algorithm

1. Initialize: Set the starting block to 0.

2. Iterate: For each file:

o Print the file number with the current starting block.

o Increment the starting block by the size of the file.

3. End: All files are allocated sequentially.

Output :
Sequential File Allocation:
File 1 starts at block 0
File 2 starts at block 5
File 3 starts at block 15
File 4 starts at block 30
Code for Indexed Allocation:

Aim

To simulate indexed file allocation by printing the allocated block numbers for each file based on a given number of
blocks.

Algorithm

1. Input:

o An array representing the number of blocks allocated to each file.

2. For Each File:

o Print the file number.

o Iterate over the number of blocks for that file and print simulated block numbers.

3. Output:

o Display the block allocation for each file.

Output :
Indexed File Allocation:
File 1 is allocated to blocks: 0 (1 2
File 2 is allocated to blocks: 0 1 2 3 4
File 3 is allocated to blocks: 0 1)—optional

Code for Linked Allocation:


Aim

Simulate linked file allocation by dynamically creating a linked list for each file, where each node represents a block
allocated to that file.

Algorithm

1. For each file:

o Initialize an empty linked list (set head to NULL).

o For the number of blocks required by the file, dynamically allocate a node, set its data (block number),
and insert it at the beginning of the list.

2. Display:

o Traverse the list for each file and print its blocks.

3. Output:

o Show the allocated blocks for each file.

Output :
Linked File Allocation:
File 1 blocks: 0 1 2
File 2 blocks: 0 1 2 3

10. Write a Program for the Implementation of Various Disk Scheduling Algorithms

Aim

Implement FCFS disk scheduling to calculate total head movements.

Algorithm
1. Start at the initial head position.

2. Move to each request sequentially, adding the distance moved to the total.

3. Print total head movements.

Output :
FCFS Disk Scheduling:
Move from 53 to 98
Move from 98 to 183
Move from 183 to 41
Move from 41 to 122
Move from 122 to 14
Move from 14 to 124
Move from 124 to 65
Move from 65 to 67
Total Head Movements: 632

Algorithm for SSTF (Shortest Seek Time First):

Aim

To implement SSTF (Shortest Seek Time First) disk scheduling to minimize the total head movements by
processing the closest disk request first.

Algorithm

1. Initialize:
o Set the initial head position and total head movements to zero.
o Mark all requests as unvisited.
2. Repeat Until All Requests Are Processed:
o Find the closest unvisited request to the current head position.
o Move to the closest request, add the distance to the total head movements, and mark it as visited.
3. Output:
o Print the movement for each request and the total head movements.

Output :

SSTF Disk Scheduling:


Move from 53 to 41
Move from 41 to 65
Move from 65 to 67
Move from 67 to 98
Move from 98 to 122
Move from 122 to 124
Move from 124 to 183
Move from 183 to 14
Total Head Movements: 323

Code for SCAN Disk Scheduling:

Aim

To implement the SCAN disk scheduling algorithm to minimize total head movements by moving the disk arm
in one direction until the end is reached and then reversing.

Algorithm

1. Sort the Requests: Arrange the requests in ascending order.


2. Move Toward Zero:
o Process and service requests less than the initial head position, moving toward track 0.
o Add the distance moved to the total head movements.
3. Reverse Direction:
o Continue processing requests in reverse order toward the highest track.
o Add the distance moved to the total head movements.
4. Output:
o Display the sequence of head movements and the total number of movements.

Output :

( Lab manual output )

SCAN Disk Scheduling:


Move from 53 to 41
Move from 41 to 14
Move from 14 to 0
Move from 0 to 65
Move from 65 to 67
Move from 67 to 98
Move from 98 to 122
Move from 122 to 124
Move from 124 to 183
Move from 183 to 200
Total Head Movements: 398

( Observation )
SCAN Disk Scheduling:
Move from 53 to 14
Move from 0 to 0
Move from 0 to 200
Total Head Movements: 253

You might also like