Prac 18
Prac 18
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
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
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