0% found this document useful (0 votes)
5 views3 pages

Prac 17

The document describes a C program that implements the First-Come, First-Serve (FCFS) disk scheduling algorithm using I/O system calls. It reads disk track requests from a file, processes them in the order received, calculates the total head movement, and writes the results to an output file. A sample output is provided, demonstrating the seek sequence and total head movement for a given set of requests.

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)
5 views3 pages

Prac 17

The document describes a C program that implements the First-Come, First-Serve (FCFS) disk scheduling algorithm using I/O system calls. It reads disk track requests from a file, processes them in the order received, calculates the total head movement, and writes the results to an output file. A sample output is provided, demonstrating the seek sequence and total head movement for a given set of requests.

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/ 3

Implement FCFS Disk Scheduling Algorithm Using

I/O System Calls

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

8 # define MAX_BUFFER 1024


9 # define MAX_REQUESTS 100
10
11 void fcfsD 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 ) ;
35 token = strtok ( NULL , " ," ) ;

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

56 for ( int i = 0; i < requestCount ; i ++) {


57 int movement = abs ( currentHead - requests [ i ]) ;
58 totalH eadMov ement += movement ;
59 currentHead = requests [ i ];
60 char temp [20];
61 snprintf ( temp , sizeof ( temp ) , " -> % d " , currentHead ) ;
62 strcat ( output , temp ) ;
63 }
64
65 // Write seek sequence to output file
66 if ( write ( out_fd , output , strlen ( output ) ) == -1) {
67 perror ( " Error writing to output file " ) ;
68 close ( out_fd ) ;
69 return ;
70 }
71
72 // Write total head movement
73 char total_str [100];
74 snprintf ( total_str , sizeof ( total_str ) , " \ nTotal Head Movement : % d \ n
" , total HeadMo vemen t ) ;
75 if ( write ( out_fd , total_str , strlen ( total_str ) ) == -1) {
76 perror ( " Error writing to output file " ) ;
77 close ( out_fd ) ;
78 return ;
79 }
80 close ( out_fd ) ;
81
82 // Display results
83 printf ( " % s " , output ) ;
84 printf ( " % s " , total_str ) ;
85 }
86
87 int main () {
88 char * inputFile = " requests . txt " ;
89 char * outputFile = " fcfs_output . txt " ;
90 int initialHead = 53;
91
92 // Create sample input file with requests

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy