0% found this document useful (0 votes)
4 views4 pages

Prac 18

The document provides a C program that implements the Shortest Seek Time First (SSTF) disk scheduling algorithm using I/O system calls. It reads disk track requests from a file, processes them to determine the closest track to the current head position, and calculates the total head movement, outputting the results to another file. The sample output demonstrates the seek sequence and total head movement for a given initial head position and request queue.

Uploaded by

moccasincaitlin
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)
4 views4 pages

Prac 18

The document provides a C program that implements the Shortest Seek Time First (SSTF) disk scheduling algorithm using I/O system calls. It reads disk track requests from a file, processes them to determine the closest track to the current head position, and calculates the total head movement, outputting the results to another file. The sample output demonstrates the seek sequence and total head movement for a given initial head position and request queue.

Uploaded by

moccasincaitlin
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/ 4

Implement SSTF Disk Scheduling Algorithm Using

I/O System Calls

Program
The following C program simulates the Shortest Seek Time First (SSTF) disk schedul-
ing algorithm using I/O system calls (open, write, read, close). It reads a queue
of disk track requests from requests.txt, processes them by selecting the closest track
to the current head position, calculates the total head movement, and writes the results to
sstfo utput.txt.T heprogramtakesaninitialheadpositionandcomputestheseeksequenceandtotalheadmov
1 # include < stdio .h >
2 # include < fcntl .h >
3 # include < unistd .h >
4 # include < string .h >
5 # include < stdlib .h >
6 # include < errno .h >
7
8 # define MAX_BUFFER 1024
9 # define MAX_REQUESTS 100
10
11 void sstfD is kS ch ed ul in g ( char * inputFile , char * outputFile , int
initialHead ) {
12 int fd = open ( inputFile , O_RDONLY ) ;
13 if ( fd == -1) {
14 perror ( " Error opening input file " ) ;
15 return ;
16 }
17
18 // Read request queue
19 char buffer [ MAX_BUFFER ];
20 ssize_t bytes_read = read ( fd , buffer , MAX_BUFFER - 1) ;
21 if ( bytes_read == -1) {
22 perror ( " Error reading input file " ) ;
23 close ( fd ) ;
24 return ;
25 }
26 buffer [ bytes_read ] = ’ \0 ’;
27 close ( fd ) ;
28
29 // Parse requests
30 int requests [ MAX_REQUESTS ];
31 int requestCount = 0;
32 char * token = strtok ( buffer , " ," ) ;
33 while ( token != NULL && requestCount < MAX_REQUESTS ) {
34 requests [ requestCount ++] = atoi ( token ) ;

1
35 token = strtok ( NULL , " ," ) ;
36 }
37
38 if ( requestCount == 0) {
39 printf ( " No requests found in input file .\ n " ) ;
40 return ;
41 }
42

43 // Open output file


44 int out_fd = open ( outputFile , O_WRONLY | O_CREAT | O_TRUNC , 0644) ;
45 if ( out_fd == -1) {
46 perror ( " Error opening output file " ) ;
47 return ;
48 }
49
50 // SSTF processing
51 char output [ MAX_BUFFER ];
52 int totalH eadMov ement = 0;
53 int currentHead = initialHead ;
54 int processed [ MAX_REQUESTS ] = {0}; // Track processed requests
55 snprintf ( output , sizeof ( output ) , " Seek Sequence : % d " , currentHead ) ;
56
57 for ( int i = 0; i < requestCount ; i ++) {
58 int minDistance = 9999;
59 int closestIndex = -1;
60

61 // Find the closest unprocessed request


62 for ( int j = 0; j < requestCount ; j ++) {
63 if (! processed [ j ]) {
64 int distance = abs ( currentHead - requests [ j ]) ;
65 if ( distance < minDistance ) {
66 minDistance = distance ;
67 closestIndex = j ;
68 }
69 }
70 }
71
72 if ( closestIndex == -1) break ; // No more requests
73
74 totalH eadMov ement += minDistance ;
75 currentHead = requests [ closestIndex ];
76 processed [ closestIndex ] = 1;
77
78 char temp [20];
79 snprintf ( temp , sizeof ( temp ) , " -> % d " , currentHead ) ;
80 strcat ( output , temp ) ;
81 }
82
83 // Write seek sequence to output file
84 if ( write ( out_fd , output , strlen ( output ) ) == -1) {
85 perror ( " Error writing to output file " ) ;
86 close ( out_fd ) ;
87 return ;
88 }
89
90 // Write total head movement
91 char total_str [100];

2
92 snprintf ( total_str , sizeof ( total_str ) , " \ nTotal Head Movement : % d \ n
" , total HeadMo vemen t ) ;
93 if ( write ( out_fd , total_str , strlen ( total_str ) ) == -1) {
94 perror ( " Error writing to output file " ) ;
95 close ( out_fd ) ;
96 return ;
97 }
98 close ( out_fd ) ;
99
100 // Display results
101 printf ( " % s " , output ) ;
102 printf ( " % s " , total_str ) ;
103 }
104

105 int main () {


106 char * inputFile = " requests . txt " ;
107 char * outputFile = " sstf_output . txt " ;
108 int initialHead = 53;
109
110 // Create sample input file with requests
111 int sampleRequests [] = {98 , 183 , 37 , 122 , 14 , 124 , 65 , 67};
112 int fd = open ( inputFile , O_WRONLY | O_CREAT | O_TRUNC , 0644) ;
113 if ( fd == -1) {
114 perror ( " Error creating input file " ) ;
115 return 1;
116 }
117 char input [ MAX_BUFFER ] = " " ;
118 for ( int i = 0; i < 8; i ++) {
119 char temp [10];
120 snprintf ( temp , sizeof ( temp ) , " %d , " , sampleRequests [ i ]) ;
121 strcat ( input , temp ) ;
122 }
123 if ( write ( fd , input , strlen ( input ) ) == -1) {
124 perror ( " Error writing to input file " ) ;
125 close ( fd ) ;
126 return 1;
127 }
128 close ( fd ) ;
129
130 printf ( " SSTF Disk Scheduling with initial head at % d :\ n " ,
initialHead ) ;
131 sst fD iskS ch ed ul in g ( inputFile , outputFile , initialHead ) ;
132
133 return 0;
134 }
Listing 1: C Program for SSTF Disk Scheduling

Output
The output for the SSTF disk scheduling simulation with an initial head position
of 53 and a request queue {98, 183, 37, 122, 14, 124, 65, 67} stored in requests.txt
is:
1 SSTF Disk Scheduling with initial head at 53:
2 Seek Sequence : 53 -> 65 -> 67 -> 37 -> 14 -> 98 -> 122 -> 124 -> 183

3
3 Total Head Movement : 236
Listing 2: Sample Output for SSTF Disk Scheduling

You might also like