0% found this document useful (0 votes)
8 views9 pages

454066OS exp10

Uploaded by

vedantshimpi3114
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)
8 views9 pages

454066OS exp10

Uploaded by

vedantshimpi3114
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/ 9

Experiment No-10

PART A
(PART A: TO BE REFFERED BY STUDENTS)

A.1 File Management & I/O Management


a. Write a C program to simulate File allocation strategies typically sequential, indexed and
linked files.
b. Write a program in C to do disk scheduling - FCFS, SCAN, C-SCAN.

A-2 Prerequisite
Knowledge about C/C++ programming language.
A.3 Outcome
A.4 Theory:
SEQUENTIAL FILE ALLOCATION
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations to each in sequential order.
a). Randomly select a location from available location s1= random(100);
b). Check whether the required locations are free from the selected location.
c). Allocate and set flag=1 to the allocated locations.
Step 5: Print the results fileno, length , Blocks allocated.
Step 6: Stop the program.
LINKED FILE ALLOCATION
ALGORITHM:
Step 1: Start the Program
Step 2: Get the number of files.
Step 3: Allocate the required locations by selecting a location randomly
Step 4: Check whether the selected location is free.
Step 5: If the location is free allocate and set flag =1 to the allocated locations.
Step 6: Print the results file no, length, blocks allocated.
Step 7: Stop the execution

INDEXED FILE ALLOCATION


ALGORITHM:
Step 1: Start the Program
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly.
Step 5: Print the results file no,length, blocks allocated.
Step 6: Stop the execution.

Disk Scheduling:

DESCRIPTION
One of the responsibilities of the operating system is to use the hardware efficiently. For the disk
drives, meeting this responsibility entails having fast access time and large disk bandwidth. Both
the access time and the bandwidth can be improved by managing the order in which disk I/O
requests are serviced which is called as disk scheduling. The simplest form of disk scheduling is,
of course, the first-come, first served (FCFS) algorithm. This algorithm is intrinsically fair, but it
generally does not provide the fastest service. In the SCAN algorithm, the disk arm starts at one
end, and moves towards the other end, servicing requests as it reaches each cylinder, until it gets
to the other end of the disk. At the other end, the direction of head movement is reversed, and
servicing continues. The head continuously scans back and forth across the disk. C-SCAN is a
variant of SCAN designed to provide a more uniform wait time. Like SCAN, C-SCAN moves the
head from one end of the disk to the other, servicing requests along the way. When the head reaches
the other end, however, it immediately returns to the beginning of the disk without servicing any
requests on the return trip.

Disk Scheduling Algorithms

1. FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS,
the requests are addressed in the order they arrive in the disk queue.Let us
understand this with the help of an example.

Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50

So, total seek time:


=(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16)
=642
2. SCAN: In SCAN algorithm the disk arm moves into a particular direction and
services the requests coming in its path and after reaching the end of disk, it
reverses its direction and again services the request arriving in its path. So, this
algorithm works as an elevator and hence also known as elevator
algorithm. As a result, the requests at the midrange are serviced more and
those arriving behind the disk arm will have to wait.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the
Read/Write arm is at 50, and it is also given that the disk arm should
move “towards the larger value”.

Therefore, the seek time is calculated as:


=(199-50)+(199-16)
=332
3. CSCAN: In SCAN algorithm, the disk arm again scans the path that has been
scanned, after reversing its direction. So, it may be possible that too many
requests are waiting at the other end or there may be zero or few requests
pending at the scanned area.
These situations are avoided in CSCAN algorithm in which the disk arm instead
of reversing its direction goes to the other end of the disk and starts servicing the
requests from there. So, the disk arm moves in a circular fashion and this
algorithm is also similar to SCAN algorithm and hence it is known as C-SCAN
(Circular SCAN).
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the
Read/Write arm is at 50, and it is also given that the disk arm should
move “towards the larger value”.

Seek time is calculated as:


=(199-50)+(199-0)+(43-0)
=391
PART B
(PART B: TO BE COMPLETED BY STUDENTS)

Roll. No: B01 Name: Ujwal Uttam Bagul

Class:SE Comps B Batch:B1

Date of Experiment: 30/03/24 Date of Submission: 30/03/24

Grade:

B.1 Software Code written by student:

File Allocation Strategies :


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILES 10
struct File {
char name[20];
int startBlock;
int length;
};
int main() {
struct File files[MAX_FILES];
int n, i, j, blocks[100] = {0};
printf("Enter the number of files: ");scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter file %d name: ", i + 1);
scanf("%s", files[i].name);
printf("Enter the starting block of file %s: ", files[i].name);
scanf("%d", &files[i].startBlock);
printf("Enter the length of file %s: ", files[i].name);
scanf("%d", &files[i].length);
for (j = files[i].startBlock; j < files[i].startBlock + files[i].length; j++) {
if (blocks[j] == 1) {
printf("Block already in use. Please enter a different block.\n");
exit(0);
}
blocks[j] = 1;
}
}
printf("\nFilename\tStart block\tLength\n");
for (i = 0; i < n; i++) {
printf("%s\t\t%d\t\t%d\n", files[i].name, files[i].startBlock, files[i].length);
}
return 0;
}
Disk Scheduling Algorithms :
#include <stdio.h>
#include <stdlib.h>
#define MAX_REQUESTS 100
void fcfs(int requests[], int n, int head) {
int seek_time = 0;
printf("\nFCFS Disk Scheduling\n");
printf("Initial head position: %d\n", head);
printf("Sequence of requests: ");
for (int i = 0; i < n; i++) {
printf("%d ", requests[i]);
}
printf("\nHead movement:\n");
printf("%d ", head);
for (int i = 0; i < n; i++) {
seek_time += abs(head - requests[i]);
head = requests[i];
printf("-> %d ", head);
}
printf("\nTotal seek time: %d\n", seek_time);
}void scan(int requests[], int n, int head, int disk_size) {
int seek_time = 0, direction, i, j;
int left[MAX_REQUESTS], right[MAX_REQUESTS];
int left_count = 0, right_count = 0;
printf("\nSCAN Disk Scheduling\n");
printf("Initial head position: %d\n", head);
printf("Sequence of requests: ");
for (i = 0; i < n; i++) {
printf("%d ", requests[i]);
}
for (i = 0; i < n; i++) {
if (requests[i] < head) {
left[left_count++] = requests[i];
} else {
right[right_count++] = requests[i];
}
}
printf("\nHead movement:\n");
printf("%d ", head);
direction = (right_count > 0) ? 1 : -1;
if (direction == 1) {
for (i = 0; i < right_count; i++) {
seek_time += abs(head - right[i]);head = right[i];
printf("-> %d ", head);
}
head = disk_size - 1;
printf("-> %d ", head);
seek_time += disk_size - 1;
} else {
for (i = left_count - 1; i >= 0; i--) {
seek_time += abs(head - left[i]);
head = left[i];
printf("-> %d ", head);
}
head = 0;
printf("-> %d ", head);
seek_time += head;
}
printf("\nTotal seek time: %d\n", seek_time);
}
void c_scan(int requests[], int n, int head, int disk_size) {
int seek_time = 0, direction, i, j;
int left[MAX_REQUESTS], right[MAX_REQUESTS];
int left_count = 0, right_count = 0;
printf("\nC-SCAN Disk Scheduling\n");
printf("Initial head position: %d\n", head);printf("Sequence of requests: ");
for (i = 0; i < n; i++) {
printf("%d ", requests[i]);
}
for (i = 0; i < n; i++) {
if (requests[i] < head) {
left[left_count++] = requests[i];
} else {
right[right_count++] = requests[i];
}
}
printf("\nHead movement:\n");
printf("%d ", head);
direction = (right_count > 0) ? 1 : -1;
if (direction == 1) {
for (i = 0; i < right_count; i++) {
seek_time += abs(head - right[i]);
head = right[i];
printf("-> %d ", head);
}
head = 0;
printf("-> %d ", head);
seek_time += head;
for (i = 0; i < left_count; i++) {seek_time += abs(head - left[i]);
head = left[i];
printf("-> %d ", head);
}
} else {
for (i = left_count - 1; i >= 0; i--) {
seek_time += abs(head - left[i]);
head = left[i];
printf("-> %d ", head);
}
head = disk_size - 1;
printf("-> %d ", head);
seek_time += disk_size - 1;
for (i = right_count - 1; i >= 0; i--) {
seek_time += abs(head - right[i]);
head = right[i];
printf("-> %d ", head);
}
}
printf("\nTotal seek time: %d\n", seek_time);
}
int main() {
int requests[MAX_REQUESTS], n, head, disk_size;
printf("Enter the number of disk requests: ");
scanf("%d", &n);
printf("Enter the disk requests: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
printf("Enter the disk size: ");
scanf("%d", &disk_size);
fcfs(requests, n, head);
scan(requests, n, head, disk_size);
c_scan(requests, n, head, disk_size);
return 0;
}

B.2 Input and Output:


Allocation Strategies:
Disk scheduling algorithms:

B 3. Conclusion:
In conclusion, the FCFS, SCAN, and C-SCAN disk scheduling algorithms offer different
approaches to optimizing disk access and seek times. FCFS serves requests in the order they
arrive, while SCAN and C-SCAN prioritize efficient movement of the disk arm to reduce seek
times. SCAN moves the arm back and forth, while C-SCAN restricts movement to one direction,
enhancing performance in certain scenarios. Understanding these algorithms helps in selecting the
most suitable disk scheduling strategy based on specific system requirements and disk access
patterns.

B 4. Question of Curiosity
Q 1. Write advantages and disadvantages of FCFS, SCAN and C-SCAN algorithms.
Advantages and Disadvantages of FCFS, SCAN, and C-SCAN Algorithms
First Come First Serve (FCFS) Algorithm:
Advantages:
1. Simplicity: FCFS is easy to implement and understand, making it user-friendly for all levels of
experience.
2. Ease of Implementation: It can be quickly integrated into existing systems with minimal effort
and cost.
3. Sequence Integrity: Orders are processed in the exact order they are received, ensuring
fairness.
Disadvantages:
1. Not Suitable for Time-Sharing Systems: FCFS is not ideal for time-sharing systems where
processes need fixed access to the CPU at regular intervals.
2. Inefficiency for Short Processes: Short processes may have to wait for long processes to finish,
reducing throughput efficiency.
3. Limited Input-Output Efficiency: FCFS is more compatible with CPU-centric systems than
input-output systems.
SCAN Algorithm:
Advantages:
1. Simple Implementation: SCAN is straightforward to implement and understand.
2. Avoids Starvation: Ensures that all requests are eventually serviced, preventing starvation.
3. Low Variance in Waiting and Response Time: Provides consistent performance in terms of
waiting and response times.
Disadvantages:
1. Long Waiting Time: Cylinders recently visited by the head may experience extended waiting
times.2. Inefficient Head Movement: The head moves to the end of the disk even when no
requests are present, impacting efficiency.
C-SCAN Algorithm:
Advantages:
1. Improved Version of SCAN: C-SCAN enhances the SCAN algorithm by reducing waiting
times for recently visited cylinders.
2. Uniform Waiting Time: Provides consistent waiting times for all requests.
3. Better Response Time: Offers improved response times compared to SCAN.
Disadvantages:
1. Increased Seek Movements: C-SCAN may cause more seek movements compared to SCAN.
2. Unnecessary Head Travel: The head travels to the end of the disk even when no requests are
left to be serviced.

Q 2. Which file allocation method is best? Why?


The best file allocation method depends on specific system requirements and considerations. Each
method has its own strengths and weaknesses, making them suitable for different scenarios:
- Contiguous File Allocation:
- Advantages: Fast access for large files, efficient for sequential and direct access.
- Disadvantages: Fragmentation issues with file deletions and creations.
- Linked File Allocation:
- Advantages: Flexibility in file storage, no fragmentation issues.
- Disadvantages: Overhead for maintaining pointers, slower access due to block traversal.
- Indexed File Allocation:
- Advantages: Quick access, efficient storage management.
- Disadvantages: Overhead for index blocks, more suitable for varied file sizes.
The best method depends on system needs. Contiguous allocation is ideal for fast access to large
files, linked allocation offers flexibility, and indexed allocation balances quick access and
efficient storage. System-specific factors like file sizes, access patterns, and storage media
characteristics determine the most suitable allocation method.

You might also like