0% found this document useful (0 votes)
2 views18 pages

OS Programs

The document outlines several operating system laboratory programs focusing on memory allocation techniques including Best Fit, First Fit, and Worst Fit, as well as FIFO page replacement algorithms. Each section includes code listings, sample inputs, and expected outputs for the respective memory allocation strategies. The programs demonstrate how to allocate memory blocks to files or processes and manage page faults in a simulated environment.

Uploaded by

aakanshyadav7
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)
2 views18 pages

OS Programs

The document outlines several operating system laboratory programs focusing on memory allocation techniques including Best Fit, First Fit, and Worst Fit, as well as FIFO page replacement algorithms. Each section includes code listings, sample inputs, and expected outputs for the respective memory allocation strategies. The programs demonstrate how to allocate memory blocks to files or processes and manage page faults in a simulated environment.

Uploaded by

aakanshyadav7
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


Operating Systems Laboratory Programs (Experiments 6–10)

1 Experiment 6: Contiguous Memory Allocation Techniques


1.1 (a) Best Fit
Program

Listing 1: Best Fit Memory Allocation


1 # include < stdio .h >
2

3 # define MAX 25
4
5 int main ( void )
6 {
7 int frag [ MAX ] , block [ MAX ] , file [ MAX ];
8 int bf [ MAX ] , ff [ MAX ];
9 int nb , nf ;
10 int i , j , temp , lowest ;
11
12 printf ( " Enter the number of blocks : " ) ;
13 scanf ( " % d " , & nb ) ;
14

15 printf ( " Enter the number of files : " ) ;


16 scanf ( " % d " , & nf ) ;
17
18 if ( nb > MAX || nf > MAX ) {
19 printf ( " Error : nb and nf must each be <= % d \ n " , MAX ) ;
20 return 1;
21 }
22
23 printf ( " Enter the size of the blocks :\ n " ) ;
24 for ( i = 0; i < nb ; i ++) {
25 printf ( " Block % d : " , i + 1) ;
26 scanf ( " % d " , & block [ i ]) ;
27 bf [ i ] = 0; /* 0 = free , 1 = allocated */
28 }
29
30 printf ( " Enter the size of the files :\ n " ) ;
31 for ( i = 0; i < nf ; i ++) {
32 printf ( " File % d : " , i + 1) ;
33 scanf ( " % d " , & file [ i ]) ;
34 ff [ i ] = -1; /* -1 = not allocated */
35 frag [ i ] = 0;
36 }
37

38 /* Best fit allocation */


39 for ( i = 0; i < nf ; i ++) {
40 lowest = 10000;
41 for ( j = 0; j < nb ; j ++) {

2
42 if (! bf [ j ] && block [ j ] >= file [ i ]) {
43 temp = block [ j ] - file [ i ];
44 if ( temp < lowest ) {
45 lowest = temp ;
46 ff [ i ] = j ;
47 }
48 }
49 }
50 if ( ff [ i ] != -1) {
51 frag [ i ] = lowest ;
52 bf [ ff [ i ]] = 1;
53 } else {
54 frag [ i ] = -1; /* allocation failed */
55 }
56 }
57
58 printf ( " \ nFile No \ tFile Size \ tBlock No \ tBlock Size \ tFragment " ) ;
59 for ( i = 0; i < nf ; i ++) {
60 if ( ff [ i ] != -1) {
61 printf ( " \ n % d \ t \ t % d \ t \ t % d \ t \ t % d \ t \ t % d " ,
62 i + 1 , file [ i ] , ff [ i ] + 1 , block [ ff [ i ]] , frag [ i ]) ;
63 } else {
64 printf ( " \ n % d \ t \ t % d \ t \ tNot Allocated " ,
65 i + 1 , file [ i ]) ;
66 }
67 }
68 printf ( " \ n " ) ;
69
70 return 0;
71 }

Sample Input and Expected Output


Input
Enter the number of blocks: 5
Enter the number of files: 4
Enter the size of the blocks:
Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600
Enter the size of the files:
File 1: 212
File 2: 417
File 3: 112
File 4: 426
Expected Output
File No File Size Block No Block Size Fragment
1 212 4 300 88
2 417 2 500 83
3 112 3 200 88
4 426 5 600 174

3
1.2 (b) First Fit
Program

Listing 2: First Fit Memory Allocation


1 # include < stdio .h >
2

3 # define MAX 25
4
5 void firstFit ( int blockSize [] , int m , int processSize [] , int n )
6 {
7 int allocation [ MAX ];
8 int availableBlock [ MAX ];
9 int i , j ;
10
11 /* Copy block sizes into availableBlock ( remaining space ) */
12 for ( i = 0; i < m ; i ++) {
13 availableBlock [ i ] = blockSize [ i ];
14 }
15
16 /* Initially no block is assigned to any process */
17 for ( i = 0; i < n ; i ++) {
18 allocation [ i ] = -1;
19 }
20

21 /* First fit allocation */


22 for ( i = 0; i < n ; i ++) { /* for each process */
23 for ( j = 0; j < m ; j ++) { /* search blocks */
24 if ( availableBlock [ j ] >= processSize [ i ]) {
25 allocation [ i ] = j ;
26 /* Reduce available memory in this block ( bug fix ) */
27 availableBlock [ j ] -= processSize [ i ];
28 break ;
29 }
30 }
31 }
32

33 printf ( " \ nProcess No .\ tProcess Size \ tBlock No .\ n " ) ;


34 for ( i = 0; i < n ; i ++) {
35 printf ( " % d \ t \ t % d KB \ t \ t " , i + 1 , processSize [ i ]) ;
36 if ( allocation [ i ] != -1) {
37 printf ( " % d \ n " , allocation [ i ] + 1) ;
38 } else {
39 printf ( " Not Allocated \ n " ) ;
40 }
41 }
42 }
43
44 int main ( void )
45 {
46 int nb , np ;
47 int blockSize [ MAX ];
48 int processSize [ MAX ];
49 int i ;
50
51 printf ( " Enter number of Processes : " ) ;
52 scanf ( " % d " , & np ) ;
53 printf ( " Enter number of Blocks : " ) ;

4
54 scanf ( " % d " , & nb ) ;
55
56 if ( np > MAX || nb > MAX ) {
57 printf ( " Error : np and nb must each be <= % d \ n " , MAX ) ;
58 return 1;
59 }
60
61 for ( i = 0; i < np ; i ++) {
62 printf ( " Enter size of Process % d : " , i + 1) ;
63 scanf ( " % d " , & processSize [ i ]) ;
64 }
65
66 for ( i = 0; i < nb ; i ++) {
67 printf ( " Enter size of Block % d : " , i + 1) ;
68 scanf ( " % d " , & blockSize [ i ]) ;
69 }
70
71 firstFit ( blockSize , nb , processSize , np ) ;
72
73 return 0;
74 }

Sample Input and Expected Output


Input

Enter number of Processes: 4


Enter number of Blocks: 3
Enter size of Process 1: 212
Enter size of Process 2: 417
Enter size of Process 3: 112
Enter size of Process 4: 426
Enter size of Block 1: 100
Enter size of Block 2: 500
Enter size of Block 3: 200

Expected Output

Process No. Process Size Block No.


1 212 KB 2
2 417 KB Not Allocated
3 112 KB 2
4 426 KB Not Allocated

1.3 (c) Worst Fit


Program

Listing 3: Worst Fit Memory Allocation


1 # include < stdio .h >
2

3 # define MAX 25
4
5 int main ( void )
6 {

5
7 int frag [ MAX ] , b [ MAX ] , f [ MAX ];
8 int bf [ MAX ] , ff [ MAX ];
9 int i , j , nb , nf , temp , highest ;
10 int wo r s t_ f it _ b lo c k_ i dx ;
11
12 printf ( " \ n \ tMemory Management Scheme - Worst Fit \ n " ) ;
13
14 printf ( " Enter the number of blocks : " ) ;
15 scanf ( " % d " , & nb ) ;
16 printf ( " Enter the number of files : " ) ;
17 scanf ( " % d " , & nf ) ;
18
19 if ( nb > MAX || nf > MAX ) {
20 printf ( " Error : nb and nf must each be <= % d \ n " , MAX ) ;
21 return 1;
22 }
23
24 printf ( " \ nEnter the size of the blocks :\ n " ) ;
25 for ( i = 0; i < nb ; i ++) {
26 printf ( " Block % d : " , i + 1) ;
27 scanf ( " % d " , & b [ i ]) ;
28 bf [ i ] = 0; /* 0 = free , 1 = allocated */
29 }
30
31 printf ( " Enter the size of the files :\ n " ) ;
32 for ( i = 0; i < nf ; i ++) {
33 printf ( " File % d : " , i + 1) ;
34 scanf ( " % d " , & f [ i ]) ;
35 ff [ i ] = -1; /* -1 = not allocated */
36 frag [ i ] = 0;
37 }
38

39 /* Worst fit allocation */


40 for ( i = 0; i < nf ; i ++) {
41 highest = -1;
42 w o r st _ fi t _ bl o ck _ id x = -1;
43
44 for ( j = 0; j < nb ; j ++) {
45 if ( bf [ j ] == 0) {
46 temp = b [ j ] - f [ i ];
47 if ( temp >= 0 && temp > highest ) {
48 highest = temp ;
49 wo r s t_ f it _ bl o c k_ i dx = j ;
50 }
51 }
52 }
53
54 if ( w o rs t _f i t _b l oc k _i d x != -1) {
55 ff [ i ] = w o rs t _ fi t _b l o ck _ id x ;
56 frag [ i ] = highest ;
57 bf [ wo r s t_ f it _ b lo c k_ i dx ] = 1;
58 } else {
59 frag [ i ] = -1; /* could not allocate */
60 }
61 }
62

63 printf ( " \ nFile_no :\ tFile_size :\ tBlock_no :\ tBlock_size :\ tFragment " ) ;


64 for ( i = 0; i < nf ; i ++) {

6
65 if ( ff [ i ] != -1) {
66 printf ( " \ n % d \ t \ t % d \ t \ t % d \ t \ t % d \ t \ t % d " ,
67 i + 1 , f [ i ] , ff [ i ] + 1 , b [ ff [ i ]] , frag [ i ]) ;
68 } else {
69 printf ( " \ n % d \ t \ t % d \ t \ tNot Allocated \t - -\ t \t - - " ,
70 i + 1 , f [ i ]) ;
71 }
72 }
73 printf ( " \ n " ) ;
74
75 return 0;
76 }

Sample Input and Expected Output


Input (same as Best Fit example)

Enter the number of blocks: 5


Enter the number of files: 4
Enter the size of the blocks:
Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600
Enter the size of the files:
File 1: 212
File 2: 417
File 3: 112
File 4: 426

Expected Output

File_no: File_size: Block_no: Block_size: Fragment


1 212 5 600 388
2 417 2 500 83
3 112 4 300 188
4 426 Not Allocated -- --

2 Experiment 7: Page Replacement Algorithms


2.1 (a) FIFO
Program

Listing 4: FIFO Page Replacement


1 # include < stdio .h >
2
3 int fr [3];
4
5 void display ( void )
6 {
7 int i ;
8 printf ( " \ n " ) ;

7
9 for ( i = 0; i < 3; i ++) {
10 printf ( " % d \ t " , fr [ i ]) ;
11 }
12 }
13
14 int main ( void )
15 {
16 int i , j , n , page [50];
17 int flag1 = 0 , flag2 = 0 , pf = 0;
18 int frsize = 3 , top = 0;
19
20 printf ( " Enter number of pages ( max 50) : " ) ;
21 scanf ( " % d " , & n ) ;
22
23 if ( n > 50) {
24 printf ( " Error : n must be <= 50\ n " ) ;
25 return 1;
26 }
27
28 printf ( " Enter % d page numbers : " , n ) ;
29 for ( i = 0; i < n ; i ++) {
30 scanf ( " % d " , & page [ i ]) ;
31 }
32
33 for ( i = 0; i < 3; i ++) {
34 fr [ i ] = -1;
35 }
36
37 for ( j = 0; j < n ; j ++) {
38 flag1 = 0;
39 flag2 = 0;
40

41 /* Check if page is already in a frame */


42 for ( i = 0; i < 3; i ++) {
43 if ( fr [ i ] == page [ j ]) {
44 flag1 = 1;
45 flag2 = 1;
46 break ;
47 }
48 }
49
50 /* If not found and there is an empty frame */
51 if ( flag1 == 0) {
52 for ( i = 0; i < frsize ; i ++) {
53 if ( fr [ i ] == -1) {
54 fr [ i ] = page [ j ];
55 flag2 = 1;
56 pf ++; /* page fault for first load */
57 break ;
58 }
59 }
60 }
61
62 /* If no empty frame , replace using FIFO */
63 if ( flag2 == 0) {
64 fr [ top ] = page [ j ];
65 top = ( top + 1) % frsize ;
66 pf ++;

8
67 }
68
69 display () ;
70 }
71
72 printf ( " \ nNumber of page faults : % d \ n " , pf ) ;
73
74 return 0;
75 }

Sample Input and Expected Output


Input

Enter number of pages (max 50): 12


Enter 12 page numbers: 2 3 2 1 5 2 4 5 3 2 5 2

Expected Output (one line per reference)

-1 -1 -1
2 -1 -1
2 3 -1
2 3 -1
2 3 1
5 3 1
5 2 1
5 2 4
5 2 4
3 2 4
3 2 4
3 5 4
3 5 2

Number of page faults: 9

2.2 (b) LRU


Program

Listing 5: LRU Page Replacement


1 # include < stdio .h >
2
3 # define FRAMES 3
4 # define PAGES 12
5
6 int fr [ FRAMES ];
7
8 void display ( void )
9 {
10 int i ;
11 printf ( " \ n " ) ;
12 for ( i = 0; i < FRAMES ; i ++) {
13 printf ( " % d \ t " , fr [ i ]) ;
14 }
15 }

9
16
17 int main ( void )
18 {
19 int page [ PAGES ] = {2 , 3 , 2 , 1 , 5 , 2 , 4 , 5 , 3 , 2 , 5 , 2};
20 int last_used [ FRAMES ];
21 int time = 0;
22 int pf = 0;
23 int i , j ;
24

25 /* initialize frames as empty */


26 for ( i = 0; i < FRAMES ; i ++) {
27 fr [ i ] = -1;
28 last_used [ i ] = -1;
29 }
30

31 for ( j = 0; j < PAGES ; j ++) {


32 int current = page [ j ];
33 int hit = 0;
34
35 time ++;
36

37 /* Check for hit */


38 for ( i = 0; i < FRAMES ; i ++) {
39 if ( fr [ i ] == current ) {
40 hit = 1;
41 last_used [ i ] = time ;
42 break ;
43 }
44 }
45
46 if (! hit ) {
47 int placed = 0;
48
49 /* Place in empty frame if available */
50 for ( i = 0; i < FRAMES ; i ++) {
51 if ( fr [ i ] == -1) {
52 fr [ i ] = current ;
53 last_used [ i ] = time ;
54 pf ++;
55 placed = 1;
56 break ;
57 }
58 }
59

60 /* Replace least recently used frame */


61 if (! placed ) {
62 int lru_index = 0;
63 int min_time = last_used [0];
64
65 for ( i = 1; i < FRAMES ; i ++) {
66 if ( last_used [ i ] < min_time ) {
67 min_time = last_used [ i ];
68 lru_index = i ;
69 }
70 }
71

72 fr [ lru_index ] = current ;
73 last_used [ lru_index ] = time ;

10
74 pf ++;
75 }
76 }
77
78 display () ;
79 }
80
81 printf ( " \ nNumber of page faults ( LRU ) : % d \ n " , pf ) ;
82

83 return 0;
84 }

Sample Input and Expected Output


Input (same reference string)

Page reference string (fixed in program):


2 3 2 1 5 2 4 5 3 2 5 2

Expected Output (one line per reference)

-1 -1 -1
2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 5 1
2 5 4
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
3 5 2

Number of page faults (LRU): 7

3 Experiment 8: File Organization Techniques


3.1 (a) Single Level Directory
Program

Listing 6: Single Level Directory Organization


1 # include < stdio .h >
2

3 struct Directory {
4 char name [20];
5 int fileCount ;
6 char files [20][20];
7 };
8

9 int main ( void )


10 {

11
11 struct Directory directories [20];
12 int master ;
13 int i , j ;
14
15 printf ( " Enter number of directories : " ) ;
16 scanf ( " % d " , & master ) ;
17
18 /* Input directory information */
19 for ( i = 0; i < master ; i ++) {
20 printf ( " \ nDirectory % d :\ n " , i + 1) ;
21
22 printf ( " Enter directory name : " ) ;
23 scanf ( " % s " , directories [ i ]. name ) ;
24
25 printf ( " Enter number of files : " ) ;
26 scanf ( " % d " , & directories [ i ]. fileCount ) ;
27
28 printf ( " Enter file names :\ n " ) ;
29 for ( j = 0; j < directories [ i ]. fileCount ; j ++) {
30 printf ( " File % d : " , j + 1) ;
31 scanf ( " % s " , directories [ i ]. files [ j ]) ;
32 }
33 }
34
35 /* Display directory structure */
36 printf ( " \ n ====\ n " ) ;
37 printf ( " Directory \ t \ tSize \ tFilenames \ n " ) ;
38 printf ( " ====\ n " ) ;
39
40 for ( i = 0; i < master ; i ++) {
41 printf ( " % -20 s \ t %2 d \ t " , directories [ i ]. name ,
42 directories [ i ]. fileCount ) ;
43
44 for ( j = 0; j < directories [ i ]. fileCount ; j ++) {
45 if ( j == 0) {
46 printf ( " % s \ n " , directories [ i ]. files [ j ]) ;
47 } else {
48 printf ( " \ t \ t \ t \ t % s \ n " , directories [ i ]. files [ j ]) ;
49 }
50 }
51 printf ( " \ n " ) ;
52 }
53
54 printf ( " ====\ n " ) ;
55
56 return 0;
57 }

Sample Input and Expected Output


Input

Enter number of directories: 2

Directory 1:
Enter directory name: DOCS
Enter number of files: 2

12
Enter file names:
File 1: [Link]
File 2: [Link]

Directory 2:
Enter directory name: IMAGES
Enter number of files: 1
Enter file names:
File 1: [Link]
Expected Output
====
Directory Size Filenames
====
DOCS 2 [Link]
[Link]

IMAGES 1 [Link]

====
(Note: actual spacing may vary slightly depending on console.)

3.2 (b) Two Level Directory


Program

Listing 7: Two Level Directory Organization


1 # include < stdio .h >
2
3 struct Directory {
4 char dname [10];
5 int ds ; /* number of subdirectories */
6 char sdname [10][10]; /* subdirectory names */
7 int sds [10]; /* file count in each subdirectory */
8 char fname [10][10][10]; /* file names */
9 } dir [10];
10
11 int main ( void )
12 {
13 int i , j , k , n ;
14
15 printf ( " Enter number of directories : " ) ;
16 scanf ( " % d " , & n ) ;
17
18 for ( i = 0; i < n ; i ++) {
19 printf ( " Enter directory % d name : " , i + 1) ;
20 scanf ( " % s " , dir [ i ]. dname ) ;
21
22 printf ( " Enter number of subdirectories : " ) ;
23 scanf ( " % d " , & dir [ i ]. ds ) ;
24
25 for ( j = 0; j < dir [ i ]. ds ; j ++) {
26 printf ( " Enter subdirectory % d name : " , j + 1) ;
27 scanf ( " % s " , dir [ i ]. sdname [ j ]) ;

13
28
29 printf ( " Enter number of files in subdirectory % d : " , j + 1)
;
30 scanf ( " % d " , & dir [ i ]. sds [ j ]) ;
31
32 for ( k = 0; k < dir [ i ]. sds [ j ]; k ++) {
33 printf ( " Enter file % d name : " , k + 1) ;
34 scanf ( " % s " , dir [ i ]. fname [ j ][ k ]) ;
35 }
36 }
37 }
38
39 printf ( " \ ndirname \ t \ tsize \ tsubdirname \ tsize \ tfiles " ) ;
40 printf ( " \ n ****\ n " ) ;
41

42 for ( i = 0; i < n ; i ++) {


43 printf ( " % s \ t \ t % d " , dir [ i ]. dname , dir [ i ]. ds ) ;
44
45 for ( j = 0; j < dir [ i ]. ds ; j ++) {
46 printf ( " \ t % s \ t \ t % d \ t " , dir [ i ]. sdname [ j ] , dir [ i ]. sds [ j ]) ;
47

48 for ( k = 0; k < dir [ i ]. sds [ j ]; k ++) {


49 printf ( " % s \ t " , dir [ i ]. fname [ j ][ k ]) ;
50 }
51
52 printf ( " \ n \ t \ t " ) ;
53 }
54 printf ( " \ n " ) ;
55 }
56
57 return 0;
58 }

Sample Input and Expected Output


Input

Enter number of directories: 1


Enter directory 1 name: USER
Enter number of subdirectories: 2
Enter subdirectory 1 name: CS
Enter number of files in subdirectory 1: 2
Enter file 1 name: [Link]
Enter file 2 name: [Link]
Enter subdirectory 2 name: ECE
Enter number of files in subdirectory 2: 1
Enter file 1 name: [Link]

Expected Output

dirname size subdirname size files


****
USER 2 CS 2 [Link] [Link]
ECE 1 [Link]

14
4 Experiment 9: Linked File Allocation
Program

Listing 8: Linked File Allocation


1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main ( void )
5 {
6 int f [50];
7 int p , i , j , a , st , len , k , c ;
8
9 for ( i = 0; i < 50; i ++) {
10 f [ i ] = 0;
11 }
12
13 printf ( " Enter how many blocks are already allocated : " ) ;
14 scanf ( " % d " , & p ) ;
15
16 printf ( " Enter the block numbers that are already allocated :\ n " ) ;
17 for ( i = 0; i < p ; i ++) {
18 scanf ( " % d " , & a ) ;
19 if ( a >= 0 && a < 50) {
20 f [ a ] = 1;
21 }
22 }
23
24 while (1) {
25 printf ( " \ nEnter the starting index block and length : " ) ;
26 scanf ( " % d % d " , & st , & len ) ;
27
28 if ( st < 0 || st + len > 50) {
29 printf ( " Invalid range (0 - -49) . Try again .\ n " ) ;
30 continue ;
31 }
32
33 k = len ;
34 for ( j = st ; j < st + k ; j ++) {
35 if ( f [ j ] == 0) {
36 f [ j ] = 1;
37 printf ( " \ n % d -> % d " , j , f [ j ]) ;
38 } else {
39 printf ( " \ n % d -> file is already allocated " , j ) ;
40 k ++; /* extend length to find more free blocks */
41 if ( st + k > 50) {
42 printf ( " \ nOut of disk space while extending .\ n " ) ;
43 break ;
44 }
45 }
46 }
47
48 printf ( " \ nDo you want to enter one more file ? ( yes -1/ no -0) : " ) ;
49 scanf ( " % d " , & c ) ;
50 if ( c != 1) {
51 break ;
52 }

15
53 }
54
55 return 0;
56 }

Sample Input and Expected Output


Input

Enter how many blocks are already allocated: 3


Enter the block numbers that are already allocated:
2 3 7

Enter the starting index block and length: 5 4

Do you want to enter one more file? (yes-1/no-0): 0

Expected Output

5 -> 1
6 -> 1
7 -> file is already allocated
8 -> 1
9 -> 1

(Exact sequence may vary slightly depending on chosen start and length.)

5 Experiment 10: SCAN Disk Scheduling Algorithm


Program

Listing 9: SCAN Disk Scheduling


1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4
5 # define SIZE 10
6 # define DISK_SIZE 200
7
8 int comp ( const void *l , const void * n )
9 {
10 return (*( const int *) l - *( const int *) n ) ;
11 }
12
13 void SCAN ( int arr [] , int head , char * dn )
14 {
15 int seek_num = 0;
16 int dt , cur_track ;
17 int leftside [ SIZE + 1] = {0};
18 int rightside [ SIZE + 1] = {0};
19 int seek_seq [ SIZE + 2];
20 int m_scan = 0 , s_scan = 0;
21 int go = 2;
22 int ind = 0;
23 int i ;

16
24
25 /* Depending on initial direction , include 0 or DISK_SIZE -1 as
boundary */
26 if ( strcmp ( dn , " leftside " ) == 0) {
27 leftside [ m_scan ++] = 0;
28 } else if ( strcmp ( dn , " rightside " ) == 0) {
29 rightside [ s_scan ++] = DISK_SIZE - 1;
30 }
31

32 /* Divide requests to left or right of head */


33 for ( i = 0; i < SIZE ; i ++) {
34 if ( arr [ i ] < head ) {
35 leftside [ m_scan ++] = arr [ i ];
36 } else if ( arr [ i ] > head ) {
37 rightside [ s_scan ++] = arr [ i ];
38 }
39 }
40
41 qsort ( leftside , m_scan , sizeof ( int ) , comp ) ;
42 qsort ( rightside , s_scan , sizeof ( int ) , comp ) ;
43

44 while ( go - -) {
45 if ( strcmp ( dn , " leftside " ) == 0) {
46 /* Move left */
47 for ( i = m_scan - 1; i >= 0; i - -) {
48 cur_track = leftside [ i ];
49 seek_seq [ ind ++] = cur_track ;
50 dt = abs ( cur_track - head ) ;
51 seek_num += dt ;
52 head = cur_track ;
53 }
54 dn = " rightside " ;
55 } else if ( strcmp ( dn , " rightside " ) == 0) {
56 /* Move right */
57 for ( i = 0; i < s_scan ; i ++) {
58 cur_track = rightside [ i ];
59 seek_seq [ ind ++] = cur_track ;
60 dt = abs ( cur_track - head ) ;
61 seek_num += dt ;
62 head = cur_track ;
63 }
64 dn = " leftside " ;
65 }
66 }
67
68 printf ( " Number of seek operations = % d \ n " , seek_num ) ;
69 printf ( " Seek sequence is :\ n " ) ;
70 for ( i = 0; i < ind ; i ++) {
71 printf ( " % d \ n " , seek_seq [ i ]) ;
72 }
73 }
74
75 int main ( void )
76 {
77 int arr [ SIZE ] = {126 , 90 , 14 , 50 , 25 , 42 , 51 , 78 , 102 , 100};
78 int head = 42;
79 char dn [] = " leftside " ;
80

17
81 SCAN ( arr , head , dn ) ;
82
83 return 0;
84 }

Sample Input and Expected Output


Input (fixed in program)

Request queue: 126, 90, 14, 50, 25, 42, 51, 78, 102, 100
Initial head position: 42
Initial direction: leftside
Disk size: 0 to 199

Expected Output

Number of seek operations = 168


Seek sequence is:
25
14
0
50
51
78
90
100
102
126

18

You might also like