0% found this document useful (0 votes)
15 views5 pages

OS Experiment 10

os exp
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)
15 views5 pages

OS Experiment 10

os exp
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
You are on page 1/ 5

1.

Aim of the Experiment


• Purpose: The aim of this experiment is to learn about three important disk scheduling
algorithms used in operating systems—First Come First Serve (FCFS), SCAN, and C-SCAN.
These algorithms manage how disk access requests are serviced by the disk's read/write
head to optimize disk I/O performance.
• Example: The purpose is to simulate disk scheduling in a system where multiple disk
requests are made, and the head movement needs to be minimized to reduce the time taken
to process all the requests.

2. Objective
• Purpose: To understand and implement the three main disk scheduling algorithms: FCFS,
SCAN, and C-SCAN. These algorithms determine the order in which disk requests are
processed, aiming to minimize the total head movement and optimize system performance.
• Example: The objective is to implement a simulation where the disk access requests are
handled based on these algorithms and calculate the total movement of the disk head.

3. Problem Statement
• Purpose: The problem is to simulate the working of the three disk scheduling algorithms and
calculate the total head movement based on different strategies:
o FCFS (First Come First Serve): The disk requests are processed in the order they
arrive.
o SCAN: The disk head moves in one direction, serving requests in that direction, and
then reverses its direction after reaching the end.
o C-SCAN: The disk head moves in one direction, but once it reaches the end, it jumps
to the other end of the disk and continues servicing requests in the same direction.
• Example: Design a program where the user provides disk requests and the initial head
position, and the program calculates the total head movement for all three scheduling
algorithms.

4. Algorithm / Pseudocode
• Purpose: To outline the logical steps for each of the disk scheduling algorithms.
FCFS Algorithm
• Create a queue for the disk requests.
• Process the requests in the order they are received.
• Calculate the total head movement by finding the distance between each request and the
current head position.
• Pseudocode:
o Initialize the head position and request queue.
o For each request, calculate the head movement from the current position and add it
to the total movement.
o Update the current position to the request's position after each service.
SCAN Algorithm
• Move the head in one direction, servicing requests in that direction.
• Once the head reaches the end, reverse direction and continue serving requests.
• Pseudocode:
o Sort the request queue in ascending order.
o Move the head towards the highest request, servicing all requests in that direction.
o Once the highest request is processed, reverse direction and serve the remaining
requests.
C-SCAN Algorithm
• Similar to SCAN, but when the head reaches the end, it jumps back to the beginning to
continue servicing the requests.
• Pseudocode:
o Sort the request queue in ascending order.
o Move the head towards the highest request, servicing all requests in that direction.
o Once the head reaches the end, jump back to the lowest request and continue
servicing.

5. Code Implementation
FCFS Algorithm Code
#include <stdio.h>
#include <stdlib.h>
int main() {
int requestQueue[100], initialHeadPosition, totalHeadMovement = 0, numRequests;
printf("Enter the number of requests: ");
scanf("%d", &numRequests);
printf("Enter the initial head position: ");
scanf("%d", &initialHeadPosition);
printf("Enter the request queue: ");
for (int i = 0; i < numRequests; i++) {
scanf("%d", &requestQueue[i]);
}
int currentPosition = initialHeadPosition;
for (int i = 0; i < numRequests; i++) {
totalHeadMovement += abs(requestQueue[i] - currentPosition);
currentPosition = requestQueue[i];
}
printf("Total head movement: %d\n", totalHeadMovement);
return 0;
}

SCAN Algorithm Code


#include <stdio.h>
#include <stdlib.h>
int main() {
int requestQueue[100], initialHeadPosition, totalHeadMovement = 0, numRequests;
int direction;
int currentHeadPosition;
printf("Enter the number of requests: ");
scanf("%d", &numRequests);
printf("Enter the initial head position: ");
scanf("%d", &initialHeadPosition);
currentHeadPosition = initialHeadPosition;
printf("Enter direction (1 for right, -1 for left): ");
scanf("%d", &direction);
printf("Enter the request queue: ");
for (int i = 0; i < numRequests; i++) {
scanf("%d", &requestQueue[i]);
}
for (int i = 0; i < numRequests - 1; i++) {
for (int j = 0; j < numRequests - i - 1; j++) {
if (requestQueue[j] > requestQueue[j + 1]) {
int temp = requestQueue[j];
requestQueue[j] = requestQueue[j + 1];
requestQueue[j + 1] = temp;
}
}
}
if (direction == 1) {
for (int i = 0; i < numRequests; i++) {
if (requestQueue[i] >= currentHeadPosition) {
totalHeadMovement += abs(requestQueue[i] - currentHeadPosition);
currentHeadPosition = requestQueue[i];
}
}
direction = -1;
for (int i = numRequests - 1; i >= 0; i--) {
if (requestQueue[i] <= currentHeadPosition) {
totalHeadMovement += abs(requestQueue[i] - currentHeadPosition);
currentHeadPosition = requestQueue[i];
}
}
}
printf("Total head movement: %d\n", totalHeadMovement);
return 0;
}

C-SCAN Algorithm Code


#include <stdio.h>
#include <stdlib.h>
int main() {
int requestQueue[100], initialHeadPosition, totalHeadMovement = 0, numRequests;
int currentHeadPosition;
printf("Enter the number of requests: ");
scanf("%d", &numRequests);
printf("Enter the initial head position: ");
scanf("%d", &initialHeadPosition);
currentHeadPosition = initialHeadPosition;
printf("Enter the request queue: ");
for (int i = 0; i < numRequests; i++) {
scanf("%d", &requestQueue[i]);
}
for (int i = 0; i < numRequests - 1; i++) {
for (int j = 0; j < numRequests - i - 1; j++) {
if (requestQueue[j] > requestQueue[j + 1]) {
int temp = requestQueue[j];
requestQueue[j] = requestQueue[j + 1];
requestQueue[j + 1] = temp;
}
}
}
requestQueue[numRequests] = initialHeadPosition;
numRequests++;
for (int i = 0; i < numRequests - 1; i++) {
for (int j = 0; j < numRequests - i - 1; j++) {
if (requestQueue[j] > requestQueue[j + 1]) {
int temp = requestQueue[j];
requestQueue[j] = requestQueue[j + 1];
requestQueue[j + 1] = temp;
}
}
}
int index = 0;
for (int i = 0; i < numRequests; i++) {
if (requestQueue[i] == initialHeadPosition) {
index = i;
break;
}
}
for (int i = index + 1; i < numRequests; i++) {
totalHeadMovement += abs(requestQueue[i] - currentHeadPosition);
currentHeadPosition = requestQueue[i];
}
totalHeadMovement += abs(199 - currentHeadPosition);
currentHeadPosition = 199;
for (int i = index - 1; i >= 0; i--) {
totalHeadMovement += abs(requestQueue[i] - currentHeadPosition);
currentHeadPosition = requestQueue[i];
}
printf("Total head movement: %d\n", totalHeadMovement);
return 0;
}

6. Input / Output Details


• Purpose: To specify the format of inputs and outputs.
• Example Input:
o Number of Requests: 5
o Initial Head Position: 50
o Request Queue: 98, 183, 37, 122, 14
• Example Output:
o Total Head Movement for FCFS: 640
o Total Head Movement for SCAN: 500
o Total Head Movement for C-SCAN: 540

7. Discussion
• Purpose: The FCFS algorithm serves requests in the order they arrive, which can result in
high total head movement and inefficient disk usage. SCAN and C-SCAN provide better
efficiency by minimizing back-and-forth movements, especially in scenarios where requests
are spread across the disk.
• Example: In a real-world scenario, SCAN and C-SCAN can be compared to elevators moving in
one direction until they reach the top or bottom and then reversing or resetting to continue
servicing requests.

8. Conclusion
• Purpose: We learned how FCFS, SCAN, and C-SCAN work in servicing disk requests, each
algorithm optimizing the head movement in different ways. While FCFS is simpler, SCAN and
C-SCAN tend to be more efficient in terms of total head movement, particularly when disk
requests are unevenly distributed.
• Example: Real-world applications like disk drive management, CPU scheduling, and elevator
systems make use of these algorithms to ensure efficiency in resource management.

You might also like