Prac 17
Prac 17
Program
The following C program simulates the First-Come, First-Serve (FCFS) 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 in order, calculates the total head
movement, and writes the results to fcfso utput.txt.T heprogramtakesaninitialheadpositionandcompute
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
1
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 // FCFS processing
51 char output [ MAX_BUFFER ];
52 int totalH eadMov ement = 0;
53 int currentHead = initialHead ;
54 snprintf ( output , sizeof ( output ) , " Seek Sequence : % d " , currentHead ) ;
55
2
93 int sampleRequests [] = {98 , 183 , 37 , 122 , 14 , 124 , 65 , 67};
94 int fd = open ( inputFile , O_WRONLY | O_CREAT | O_TRUNC , 0644) ;
95 if ( fd == -1) {
96 perror ( " Error creating input file " ) ;
97 return 1;
98 }
99 char input [ MAX_BUFFER ] = " " ;
100 for ( int i = 0; i < 8; i ++) {
101 char temp [10];
102 snprintf ( temp , sizeof ( temp ) , " %d , " , sampleRequests [ i ]) ;
103 strcat ( input , temp ) ;
104 }
105 if ( write ( fd , input , strlen ( input ) ) == -1) {
106 perror ( " Error writing to input file " ) ;
107 close ( fd ) ;
108 return 1;
109 }
110 close ( fd ) ;
111
112 printf ( " FCFS Disk Scheduling with initial head at % d :\ n " ,
initialHead ) ;
113 fcf sD iskS ch ed ul in g ( inputFile , outputFile , initialHead ) ;
114
115 return 0;
116 }
Listing 1: C Program for FCFS Disk Scheduling
Output
The output for the FCFS 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 FCFS Disk Scheduling with initial head at 53:
2 Seek Sequence : 53 -> 98 -> 183 -> 37 -> 122 -> 14 -> 124 -> 65 -> 67
3 Total Head Movement : 640
Listing 2: Sample Output for FCFS Disk Scheduling