You are on page 1of 58

Ex No: 1 / /08

Problem Statement:

CPU Scheduling Algorithms

To write program to implement the following CPU scheduling algorithms First come first served. Shortest job first Round robin scheduling

Program Description:
Get the process details, process name and burst time. Get the choice of scheduling algorithm FCFS: o Calculate waiting time and turn around time for process in the order of their arrival SJF: o Sort the processes based in their burst time and then calculate waiting time and turn around time for process. Round Robin: o Get the time quantum o Allow the process to run only for the time quantum o If it finishes, calculate total turn around time and waiting time o Else, put it in the back of queue and make it to wait for its chance

Program Code:
#include <stdio.h> #include <conio.h> #include <stdlib.h> int numProc,tq; struct proc { char name[4]; int bt; int wt; int tat; int priority; }job[10],fcfs[10],sjob[10],rr[10]; void getProcDetails() { int i; printf("Enter number of processes\n"); scanf("%d",&numProc); for(i=0;i<numProc;i++) { printf("Process name: "); scanf("%s",job[i].name); printf("Process Burst time: "); scanf("%d",&job[i].bt); job[i].wt = job[i].tat = job[i].priority = 0; fcfs[i] = sjob[i] = rr[i] = job[i]; } } void displayProcDetails(struct proc pr[]) { int i; float awt=0.0, atat=0.0; for(i=0;i<numProc;i++) { awt +=pr[i].wt; atat +=pr[i].tat; } awt /= numProc; atat /= numProc; printf("Name BTime WTime TATime\n"); for(i=0;i<numProc;i++) printf("%3s%9d%9d%9d\n",pr[i].name,pr[i].bt,pr[i].wt,pr[i].tat); printf("\nAverage Waiting Time %f\n",awt); printf("\nAverage Turn Around Time %f\n",atat); getch(); } 2

void swap(int i, int j) { struct proc temp; temp = sjob[i]; sjob[i] = sjob[j]; sjob[j] = temp; } void fcfs_proc(struct proc p[]) { int i, tmp=0; for(i=0;i<numProc;i++) { p[i].wt = tmp; tmp += p[i].bt; p[i].tat = tmp; } displayProcDetails(p); } void sjf_proc() { int i,j; for(i=0;i<numProc;i++) for(j=i+1;j<numProc;j++) if(sjob[i].bt > sjob[j].bt) swap(i,j); fcfs_proc(sjob); } void rr_proc() { int flag=0, i=0, c=0; struct proc temp[10]; printf("Enter time quantum\n"); scanf("%d",&tq); for(i=0;i<numProc;i++) temp[i].bt = rr[i].bt; i=0; while(1) { if(temp[i].bt != 0) { if( tq <= temp[i].bt) { temp[i].bt-=tq; c+=tq; if(temp[i].bt <= 0)

{ flag++; rr[i].tat = c; rr[i].wt = rr[i].tat - rr[i].bt; } } else { c+=temp[i].bt; flag++; rr[i].tat = c; rr[i].wt = rr[i].tat - rr[i].bt; } } i++; if(i == numProc) i=0; if (flag == numProc) break; } /* while loop */ displayProcDetails(rr); } void main() { int choice; clrscr(); getProcDetails(); while(1) { printf("Enter Your Choice\n"); printf("1. FCFS 2. SJF 3. RoundRobin 4. Exit\n"); scanf("%d",&choice); switch(choice) { case 1: fcfs_proc(fcfs); break; case 2: sjf_proc(); break; case 3: rr_proc(); break; case 4: exit(0); } /* switch */ } /* while loop */ } /* main function */

Sample Output:
Enter Process Details Enter Number of Processes: 4 Process name: p1 Process Burst time: 5 Process name: p2 Process Burst time: 6 Process name: p3 Process Burst time: 12 Process name: p4 Process Burst time: 7 Process name: p5 Process Burst time: 9 Enter Your Choice 1. FCFS 2. SJF 3. RoundRobin 4. Exit 1 Name p1 p2 p3 p4 p5 BTime 5 6 12 7 9 WTime 0 5 11 23 30 TATime 5 11 23 30 39

Average Waiting Time 13.800000 Average Turn Around Time 21.600000 Enter Your Choice 1. FCFS 2. SJF 3. RoundRobin 4. Exit 2 Name p1 p2 p4 p5 p3 BTime 5 6 7 9 12 WTime 0 5 11 18 27 TATime 5 11 18 27 39

Average Waiting Time 12.200000 Average Turn Around Time 20.000000 Enter Your Choice 1. FCFS 2. SJF 3. RoundRobin 4. Exit 3 Enter time quantum 1 5

Name p1 p2 p3 p4 p5

BTime 5 6 12 7 9

WTime 16 20 27 24 27

TATime 21 26 39 31 36

Average Waiting Time 22.799999 Average Turn Around Time 30.600000 Enter Your Choice 1. FCFS 2. SJF 3. RoundRobin 4. Exit 4

Result:
Thus CPU Scheduling Algortighms FCS, SJF and RR were implemented, tested and verified.

Ex No: 2 / /08
Program Statement:

Dekkers Algorithm

To write a program to implement Dekkers algorithm to solve critical section problem.

Program Description:
It is the first software solution to critical section problem Have two processes: P0, P1. Create Shared memory for both the processes Let P0 reads the number of integers and stores the values in the shared memory P0 sets the flag to TRUE and goes to critical section code (step 5) P0 adds the first half of the numbers and stores the result in shared memory. P0 sets the flag to FALSE says that it has completed its CS code P1 retrieves the number of elements by accessing the first locatiob of shared memory. It sits in in a loop until the flag becomes FALSE. Now, P1 enters into CS by setting the flag to TRUE. P1 adds the second half of the numbers to the result of process P0 and stores the result in shared memory. P1 sets the flag to FALSE and come out of CS. Finally P1 prints the total sum and P0 deletes the shared memory

Program Code:
/* dekker.c */ #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<stdio.h> #define true 1 #define false 0 main() { int flag1 = true, flag2 = false; int *a, i, j, n1, n2, sum1=0, sum2=0; int shmid, pid; shmid=shmget((key_t)999,sizeof(int)*20,IPC_CREAT|IPC_EXCL|0777); a=(int *)shmat(shmid,NULL,0777); if(a==(int *)-1) { printf("shmat() error\n"); exit(1); } printf("enter the no of elements\n"); scanf("%d",&n1); printf("enter the nos\n"); for(i=1;i<=n1;i++) scanf("%d",&a[i]); a[0]= n2 = n1; if((pid=fork()) == 0) /* child process */ { a[n1+1] = flag1; for(i=1;i<=n1/2;i++) /* CS code begins */ sum1+=a[i]; a[n1+2] = sum1; a[n1+1] = flag2; /* CS code ends */ while(a[n1+1] == flag1); printf(" Child process, Sum of elements = %d\n",sum1); } else { while(a[n2+1] == flag1); 8

a[n2+1] = flag1; /* CS code begins */ for(j=n2/2+1;j<=n2;j++) sum2 += a[j]; a[n2+2] += sum2; a[n2+1] = flag2; /* CS code ends */ printf(" Parent process, Sum of elements = %d\n",sum2); printf(" Sum of %d elements = %d\n",n1,a[n2+2]); } shmdt(a); shmctl(shmid,IPC_RMID,0); } /* end main */

Sample Output:
Compilation $ cc o dekker dekker.c Execution $./dekker enter the no of elements 10 enter the nos 1 2 3 4 5 6 7 8 9 10 Child Prcoess, Sum of elements = 15 Parent Prcoess, Sum of elements = 40 Sum of all elements = 55

Result:
Thus the Dekkers Algorithm was implemented, tested and verified for Mutual Exclusion problem in the UNIX System.

Ex.no: 3 / /08
Problem statement:

Semaphores

To implement Producer-Consumer problem using semaphores in UNIX system.

Program Description:
Create semaphore using semget() system call Set its value using semctl() system call If the process is to request for any resource, set sem_op field to -1 and call semop() sys call which does necessary update, such that any other process cannot make requests for the same resource. If the process is to release for the availed resource, set sem_op field to 1 and call semop() sys call which does necessary update, such that new or waited processes can avail that resource. If the resource is held by any other process, the requested process waits in a loop and checks the semaphore value, whether it is released from the process which holds it. delete() function removes the created semaphore at the exit of the program.

10

Program Code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <signal.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> #define SHMDATASIZE 1000 #define BUFFERSIZE (SHMDATASIZE - sizeof(int)) #define SN_EMPTY 0 #define SN_FULL 1 #define SN_LOCK 2 int DeleteSemId = 0, DeleteShmId=0; void consumer(int); void producer(int); int masterinit(void); char *standardinit(int,int *); void delete(void); void sigdelete(int); void locksem(int,int); void unlocksem(int,int); void producerwrite(int,int, char*); int main(int argc,char *argv[]) { char selection[3]; int shmid; /* No arguments: Server */ if(argc < 2) shmid = masterinit(); else shmid = atoi(argv[1]); printf("Shall I be a [C]onsumer or [P]roducer process\n"); fgets(selection,sizeof(selection),stdin); switch(selection[0]) { case 'p': case 'P': producer(shmid); break; case 'c': 11

case 'C': consumer(shmid); break; default: printf("Invalid choice, exiting\n"); exit(0); } /* end switch */ return 0; } void producer(int shmid) { int semid; char *buffer; buffer = standardinit(shmid, &semid); printf("Produce operational: shm id is %d, sem id is %d\n", shmid,semid); while(1) { char input[3]; printf("\t\t\tMenu\n1. Send a message\n 2. Exit\n"); fgets(input, sizeof(input),stdin); switch(input[0]) { { case '1': producerwrite(shmid,semid,buffer); break; case '2': exit(0); } } /* end while */ } void consumer(int shmid) { int semid; char *buffer; buffer = standardinit(shmid, &semid); printf("Consumer operation: shmid %d, semid %d\n",shmid,semid); while(1) { printf("Waiting until full ... \n"); fflush(stdout); locksem(semid,SN_FULL); printf("done\n"); printf("Waiting for lock ... \n"); fflush(stdout); locksem(semid,SN_LOCK); printf("done\n");

12

printf("Message received %s\n",buffer); unlocksem(semid,SN_LOCK); unlocksem(semid,SN_EMPTY); } /* end while */ } char *standardinit(int shmid, int *semid) { void *shmdata; char *buffer; shmdata = shmat(shmid,0,0); if(shmdata == (int *)-1 ) { printf("shmat() error\n"); exit(0); } *semid = *(int *)shmdata; buffer = shmdata + sizeof(int); return buffer; } int masterinit(void) { int semid, shmid; void *shmdata; semid = semget(IPC_PRIVATE, 3, SHM_R|SHM_W); if(semid == -1 ) { printf("semget() error\n"); exit(0); } DeleteSemId = semid; atexit(&delete); signal(SIGINT, &sigdelete); /* Initially empty should be available and full should not be. The lock will also be available initially */ if(semctl(semid,SN_EMPTY, SETVAL, 1) == -1) { printf("semctl() SN_EMPTY error\n"); exit(0); } if(semctl(semid,SN_LOCK, SETVAL, 1) == -1) { printf("semctl() SN_LOCK error\n");

13

exit(0); } if(semctl(semid,SN_FULL, SETVAL, 0) == -1) { printf("semctl() SN_FULL error\n"); exit(0); } /* Allocate shared memory segment */ shmid = shmget(IPC_PRIVATE, SHMDATASIZE, IPC_CREAT|SHM_R|SHM_W); if(shmid == -1) { printf("shmget() error\n"); exit(0); } DeleteShmId = shmid; /* Map it to memory */ shmdata = shmat(shmid,0,0); if(shmdata == (int *)-1) { printf("semctl() error\n"); exit(0); } /* Write the semaphore id to the beginning */ *(int *)shmdata = semid; printf("*** The system is running with SHM id %d",shmid); return shmid; } void delete(void) { printf("Master exiting; deleting semaphore %d\n", DeleteSemId); if(semctl(DeleteSemId,0,IPC_RMID,0) == -1) printf("Error in releasing semaphore\n Use the command `ipcrm sem <id>` at the command prompt\n"); /* Mark it to delete automatically when the last holding process exits */ if(shmctl(DeleteShmId, IPC_RMID, NULL) == -1) { printf("semctl() IPC_RMID error\n"); exit(0); } } void sigdelete(int signum) { exit(0); }

14

void locksem(int semid, int semnum) { struct sembuf sb; sb.sem_num = semnum; sb.sem_op = -1; sb.sem_flg = SEM_UNDO; if(semop(semid, &sb, 1) == -1) { printf("semop() locksem error\n"); exit(0); } } void unlocksem(int semid, int semnum) { struct sembuf sb; sb.sem_num = semnum; sb.sem_op = 1; sb.sem_flg = SEM_UNDO; if(semop(semid, &sb, 1) == -1) { printf("semop() unlocksem error\n"); exit(0); } } void producerwrite(int shmid, int semid, char *buffer) { printf("Waiting until empty ...\n"); fflush(stdout); locksem(semid,SN_EMPTY); printf("done. waiting for lock ...\n"); fflush(stdout); locksem(semid,SN_LOCK); printf("Enter message\n"); fgets(buffer, BUFFERSIZE, stdin); unlocksem(semid,SN_LOCK); unlocksem(semid,SN_FULL); }

15

Sample Output:
Window 1 (Producer) $ ./prod_con *** The system is running with SHM id $ cc o prod_con prod_con.c 884738 Shall I be a [C]onsumer or [P]roducer process P Produce operational: shm id is 884738, sem id is 327682 Menu 1. Send a message 2. Exit 1 Waiting until empty ... done. waiting for lock ... Enter message Welcome, Good Morning!! Menu 1. Send a message 2. Exit 1 Waiting until empty ... done. waiting for lock ... Enter message Good Luck, All the Best!! Menu 1. Send a message 2. Exit 1 Waiting until empty ... done. waiting for lock ... Enter message Good Luck, All the Best!! Menu 1. Send a message 16 done Waiting for lock ... done Message received Welcome, Good Morning!! Waiting until full ... done Waiting for lock ... done Message received Bye, Good Night!! Waiting until full ... done Waiting for lock ... done Message received Welcome, Good Morning!! Waiting until full ... Window 2 (Consumer 1) $ ./prod_con 884738 Shall I be a [C]onsumer or [P]roducer process C Consumer operation: shmid 884738, semid 327682 Waiting until full ... Window 3 (Consumer 3) $ ./prod_con 884738 Shall I be a [C]onsumer or [P]roducer process C Consumer operation: shmid 884738, semid 327682 Waiting until full ...

2. Exit 2 $

Result:
Thus inter process communication using semaphores in UNIX was implemented, tested and verified.

17

Ex No: 4 / /08

Memory Management Techniques

Problem Statement:
To write program to implement frist fir, best fit and worst fit strategies.

Program Description:
Get the partition details - number of partitions, partition size. Get the process details - number of processes, process size. For First Fit algorithm, for each process o Start searching from beginning and continue searching as long as we find a free hole that is large enough. o Allocate that partition to hold the process For best fit algorithm, for each process o Search entire free partition list and find a partition that causes the least amount of potential fragmentation o Allocate that partition to hold the process For worst fit algorithm, for each process o Search the entire free partition list and find a partition that is larger. o Allocate that partition to hold the process For each of the algorithms, calculate the amount of left over holes in the partition. Compare them and print which uses efficient use of memory (one which has small leftover hole)

18

Program Code:
#include <stdio.h> #include <conio.h> #include <stdlib.h> struct memPart { int partNo; int size; int flag; int freeSpace; }partition[10]; struct memProc { int procNo; int size; int flag; int partNo; }process[10]; void bestfit(int numProc, int numPart) { int i,j, lowkey, lowindex; for(i=0;i<numProc;i++) { if(process[i].flag == 0) { for(j=0;j<numPart;j++) { if(partition[j].flag == 0) { lowkey = partition[j].size; lowindex = j; for(int k=j+1;k<numPart;k++) { if (partition[k].flag == 0) { if (partition[k].size < lowkey) { lowkey = partition[k].size; lowindex=k; } } } /* k for loop */ if( process[i].size <= lowkey ) 19

{ partition[lowindex].flag = process[i].flag = 1; process[i].partNo = lowindex; partition[lowindex].freeSpace = partition[lowindex].size - process[i].size; break; } else continue; } /* if partition[j].flag */ } /* j for loop */ } /* if process[i].flag */ if (process[i].flag == 0) process[i].partNo = 99; } /* i for loop */ } void firstfit(int numProc, int numPart) { int i,j; for(i=0;i<numProc;i++) { if (process[i].flag == 0 ) { for(j=0;j<numPart;j++) { if (partition[j].flag == 0 ) { if( process[i].size <= partition[j].size ) { partition[j].flag = process[i].flag = 1; process[i].partNo = j; partition[j].freeSpace = partition[j].size - process[i].size; break; } } /* if */ } /* j loop */ } /* if */ if (process[i].flag == 0) process[i].partNo = 99; } /* i loop */ } void worstfit(int numProc, int numPart) { int i,j, maxkey, maxindex; for(i=0;i<numProc;i++) { if(process[i].flag == 0) { for(j=0;j<numPart;j++)

20

{ if(partition[j].flag == 0) { maxkey = partition[j].size; maxindex = j; for(int k=j+1;k<numPart;k++) { if (partition[k].flag == 0) { if (partition[k].size > maxkey) { maxkey = partition[k].size; maxindex=k; } } } /* k for loop */ if( process[i].size <= maxkey ) { partition[maxindex].flag = process[i].flag = 1; process[i].partNo = maxindex; partition[maxindex].freeSpace = partition[maxindex].size - process[i].size; break; } else continue; } /* if partition[j].flag */ } /* j for loop */ } /* if process[i].flag */ if (process[i].flag == 0) process[i].partNo = 99; } /* i for loop */ } void display(int numProc, int numPart) { printf("-------- Allocation Details --------\n\n\n"); printf("ProcNo ProcSize Allocated To\n"); printf(" Partition \n"); for(int i=0;i<numProc;i++) { if(process[i].partNo != 99) printf("%4d%13d%13d\n",process[i].procNo,process[i].size,process[i].partNo); else printf("%4d%13d%20s\n",process[i].procNo,process[i].size,"Not Allocated"); } printf("\n\nPartition Partition Partition\n"); printf(" Number Size Free Space\n\n"); for(i=0;i<numPart;i++) { if(partition[i].flag != 0)

21

printf("%4d%13d%13d\n",partition[i].partNo,partition[i].size,partition[i].freeSpace); else printf("%4d%13d%13d %18s\n",partition[i].partNo,partition[i].size,partition[i].freeSpace,"Unused"); } getch(); } void main() { int i,choice,numPart,numProc; clrscr(); printf("Enter partition details\n"); printf("Enter number of partitions\n"); scanf("%d",&numPart); printf("Enter partition size\n"); for(i=0;i<numPart;i++) { scanf("%d",&partition[i].size); partition[i].flag = 0; partition[i].partNo = i; partition[i].freeSpace = partition[i].size; } printf("Enter process details\n"); printf("Enter number of processes\n"); scanf("%d",&numProc); printf("Enter process size\n"); for(i=0;i<numProc;i++) { scanf("%d",&process[i].size); process[i].flag = 0; process[i].procNo = i; } while(1) { printf("1. FirstFit 2. Best Fit 3. Worst Fit 4. Exit\n"); scanf("%d",&choice); switch(choice) { case 1: firstfit(numProc,numPart); display(numProc,numPart); break; case 2: bestfit(numProc,numPart); display(numProc,numPart); case 3: worstfit(numProc,numPart); display(numProc,numPart); case 4:

22

exit(0); } /* switch */ } /* while */ } /* main function */

Sample Output:
Enter partition details Enter number of partitions 4 Enter partition size 150 200 250 300 Enter process details Enter number of processes 4 Enter process size 50 300 275 180 1. FirstFit 2. Best Fit 3. Worst Fit 4. Exit 1 -------- Allocation Details -------ProcNo ProcSize Allocated To Partition 0 50 0 1 300 3 2 275 Not Allocated 3 180 1 Partition Partition Partition Number Size Free Space 0 1 2 3 150 200 250 300 100 20 250 0

Unused

23

1. FirstFit 2. Best Fit 3. Worst Fit 4. Exit 2 -------- Allocation Details -------ProcNo ProcSize Allocated To Partition 0 50 0 1 300 3 2 275 Not Allocated 3 180 1 Partition Partition Partition Number Size Free Space 0 1 2 3 150 200 250 300 100 20 250 0

Unused

1. FirstFit 2. Best Fit 3. Worst Fit 4. Exit 3 -------- Allocation Details -------ProcNo ProcSize Allocated To Partition 0 50 3 1 300 Not Allocated 2 275 Not Allocated 3 180 2 Partition Partition Partition Number Size Free Space 0 1 2 3 150 200 250 300 150 200 70 250 Unused Unused

Result:
Thus the various storage allocation techniques like First Fit, Best Fit and Worst Fit were implemented, tested and verified.

24

Ex No: 5 / /08

FIFO Page Replacement Algorithm

Problem Statement:
To write a program to implement the First In First Out Page Replacement Algorithm

Problem Description:
Get the number of pages in page reference string Get the page reference string Get the number of frames FIFO algorithm: o For each page in reference string, o If a page is already in memory, then it is done. o If it is not in memory, increment the page fault. o If there is any free frame, load the required page into the free frame found. o Else, replace the page, which is brought into memory first. Print the total page faults that have been occurred.

25

Program Code:
#include<stdio.h> #include<conio.h> void main() { int ref[25]; int mem[10]; int i,j,k,flag,n,m,p,pf; clrscr(); printf("Enter the number of pages in the reference string \n"); scanf("%d",&n); printf("enter the reference string \n"); for (i=0;i<n;i++) { printf("Enter the %d page reference:",i+1); scanf("%d",&ref[i]); } printf("Enter the number of frames \n"); scanf("%d",&m); for(i=0;i<m;i++) mem[i]=-1; printf("\t FIFO Page Replacement Algorithm \n"); printf("\t-----------\n"); j=0; pf=0; for(i=0;i<n;i++) { p=ref[i]; flag=0; for(k=0;k<m;k++) if (mem[k]==p) flag=1; if(flag!=1) { pf++; for(k=0;k<m;k++) if(mem[k]==-1) { mem[k]=p; goto tem; } mem[j%3]=p; 26

j++; } /* if flag != 1 */ tem: printf("%d-- %d %d %d\n",p,mem[0],mem[1],mem[2]); } /* for i loop */ printf("Number of page faults:%d\n",pf); getch(); }

Sample Output:
Enter the number of pages in the reference string 20 enter the reference string Enter the 1 page reference:7 Enter the 2 page reference:0 Enter the 3 page reference:1 Enter the 4 page reference:2 Enter the 5 page reference:0 Enter the 6 page reference:3 Enter the 7 page reference:0 Enter the 8 page reference:4 Enter the 9 page reference:2 Enter the 10 page reference:3 Enter the 11 page reference:0 Enter the 12 page reference:3 Enter the 13 page reference:2 Enter the 14 page reference:1 Enter the 15 page reference:2 Enter the 16 page reference:0 Enter the 17 page reference:1 Enter the 18 page reference:7 Enter the 19 page reference:0 Enter the 20 page reference:1 Enter the number of frames 3 FIFO Page Replacement Algorithm -------------------------------------------7-- 7 -1 -1 0-- 7 0 -1 1-- 7 0 1 2-- 2 0 1 0-- 2 0 1 3-- 2 3 1 0-- 2 3 0 4-- 4 3 0 2-- 4 2 0 27

3-- 4 0-- 0 3-- 0 2-- 0 1-- 0 2-- 0 0-- 0 1-- 0 7-- 7 0-- 7 1-- 7

2 2 2 2 1 1 1 1 1 0 0

3 3 3 3 3 2 2 2 2 2 1

Number of page faults:15

Result:
Thus the FIFO page replacement was implemented, tested and verified.

28

Ex No: 6 / /08

LRU Page Replacement Algorithm

Problem Statement:
To write a program to implement the Least Recently Used Page Replacement Algorithm

Problem Description:
Get the number of pages in page reference string Get the page reference string Get the number of frames LRU algorithm: o For each page in the reference string, if a page is already in memory, then it is done o If it is not in memory, increment the page fault. o If there is any free frame, load the required page into the free frame found. o Else, replace the page that has not been used for the longest period of time by checking the previous reference strings. Print the page faults for each of the schemes.

29

Program Code:
#include<stdio.h> #include<conio.h> void main() { int ref[25]; int mem[10],memf[]={0,0,0}; int i,j,k,flag,n,m,p,pf,ch; clrscr(); printf("Enter the number of pages in the reference string \n"); scanf("%d",&n); printf("enter the reference string \n"); for (i=0;i<n;i++) { printf("Enter the %d page reference:",i+1); scanf("%d",&ref[i]); } printf("Enter the number of frames \n"); scanf("%d",&m); printf("\t LRU Algorithm \n"); printf("\t-----------\n"); j=0; pf=0; for(i=0;i<m;i++) mem[i]=-1; for(i=0;i<n;i++)

30

{ p=ref[i]; flag=0; for(k=0;k<m;k++) if (mem[k]==p) flag=1; if(flag!=1) { pf++; for(k=0;k<m;k++) if(mem[k] == -1) { mem[k] = p; goto tmp; } memf[0] = memf[1] = memf[2] = 0; for(k=0;k<m;k++) { for(j=i-1;j>=i-m;j--) { if(mem[k] == ref[j]) { memf[k] = 1; break; } } if(memf[k] == 0) mem[k] = p; } /* for k loop */ if(memf[0] == 1 && memf[1] == 1 && memf[2] == 1 ) { for(int l=0;l<m;l++) if(mem[l] == ref[i-m]) mem[l] = p; } } /* if flag != 1 */ tmp: printf("%d-- %d %d %d\n",p,mem[0],mem[1],mem[2]); } /* for i loop */ printf("Number of page faults :%d \n",pf); getch(); } /* end main */

31

Sample Output:
Enter the number of pages in the reference string 20 enter the reference string Enter the 1 page reference:7 Enter the 2 page reference:0 Enter the 3 page reference:1 Enter the 4 page reference:2 Enter the 5 page reference:0 Enter the 6 page reference:3 Enter the 7 page reference:0 Enter the 8 page reference:4 Enter the 9 page reference:2 Enter the 10 page reference:3 Enter the 11 page reference:0 Enter the 12 page reference:3 Enter the 13 page reference:2 Enter the 14 page reference:1 Enter the 15 page reference:2 Enter the 16 page reference:0 Enter the 17 page reference:1 Enter the 18 page reference:7 Enter the 19 page reference:0 Enter the 20 page reference:1 Enter the number of frames 3 LRU Algorithm ----------7-- 7 -1 -1 0-- 7 0 -1 1-- 7 0 1 2-- 2 0 1 0-- 2 0 1 3-- 2 0 3 0-- 2 0 3 4-- 4 0 3 2-- 4 0 2 3-- 4 3 2 0-- 0 3 2 3-- 0 3 2 2-- 0 3 2 1-- 1 3 2 2-- 1 3 2 0-- 1 0 2 1-- 1 0 2 7-- 1 0 7 0-- 1 0 7 32

1-- 1 0 7 Number of page faults :12

Result:
Thus the LRU page replacement algorithms was implemented, tested and verified.

Ex no: 7 / /08
Problem Statement:

Shared Memory

To write a program to implement shared memory between two unrelated processes.

Program Description:
Process P1 Creates shared memory Attach it to shared memory. Get the array of numbers and store them in the shared memory Sum first n/2 numbers and prints the result Detach from shared memory Delete shared memory

Process P2 Attach to shared memory.

33

Gets the number of elements from the first location of shared memory Sum second half numbers and prints the result Also print the sum of n numbers Detach from shared memory

Program Code:

/* sm1.c */
#include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<stdio.h> void main() { int *a,i,sum=0,shmid,n; shmid=shmget((key_t)123,sizeof(int)*20,IPC_CREAT|IPC_EXCL|0777); a=(int *)shmat(shmid,NULL,0777); if(a==(int *)-1) { printf("failure"); exit(1); } printf("enter the no of elements\n"); scanf("%d",&n); printf("enter the nos\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); a[0]=n; for(i=1;i<=n/2;i++)

34

sum = sum +a[i]; a[n+2]=sum; printf(" Process 1, sum of first half elements = %d\n",sum); shmdt(a); shmctl(shmid,IPC_RMID,0); } /* sm2.c */ #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<stdio.h> void main() { int *a,i,sum=0, total = 0, shmid,n; shmid=shmget((key_t)123,sizeof(int)*20,0777); a=(int *)shmat(shmid,NULL,0777); n=a[0]; for(i=n/2+1;i<=n;i++) sum=sum+a[i]; printf(" Process 2 sum of second half elements =%d\n",sum); total = a[n+2]+sum; printf(" Sum of %d elements = %d\n",n,total); } Compilation $ cc o sm1 sm1.c $ cc o sm2 sm2.c Eecution $./sm1(in one window) enter the no of elements 10 enter the nos 1 2 3 4 5 6 7 8 9 10 Prcoess 1 Sum of elements = 15

35

$./sm2 (in another window) Prcoess 2 Sum of elements = 40 Sum of 10 elements = 55

Result:
Thus the inter process communication using shared memory in UNIX was implemented, tested and verified

EX No: 8 / /08
Problem Statement:

FILE LOCKING

To write a program to implement File Locking algorithm in Unix System. Program Description: Initialize struct flock with the type of lock required Open the file with the matching mode Call fcntl() with the initialized lock structure Update the file Clear the lock by setting F_UNLCK field and call fcntl().

File locking is a mechanism that enforces access to a computer file by only one user or process at any specific time. Types of locking mechanisms: Mandatory Locking and Advisory Locking. Mandatory lock systems actually prevent read() and write() to file, but with an advisory lock systems, processes can still read and write from a file while it's locked.

36

There are two types of advisory locks: read locks and write locks (also referred to as shared locks and exclusive locks, respectively.) The way read locks work is that they don't interfere with other read locks. For instance, multiple processes can have a file locked for reading at the same time. However, when a process has write lock on a file, no other process can activate either a read or write lock until it is relinquished. One easy way to think of this is that there can be multiple readers simultaneously, but there can only be one writer at a time. There are many ways to lock files in Unix systems - l ockf(), flock(), fcntl() and
create() with O_EXCL flag.

Setting a lock The fcntl() function does everything, just we need to fill up the struct flock (declared in fcntl.h) that describes the type of lock needed, open() the file with the matching mode, and call fcntl() with the proper arguments. Here are some field definitions: l_type It signifies the type of lock that need to be set. It's either F_RDLCK, F_WRLCK, or F_UNLCK i.e. it sets a read lock, a write lock or clear the lock respectively. l_whence It determines where the l_start field starts from (it's like an offset for the offset). It can be either SEEK_SET, SEEK_CUR, or SEEK_END, for beginning of file, current file position or end of file respectively. l_start This is the starting offset in bytes of the lock, relative to l_whence. l_len This is the length of the lock region in bytes (which starts from l_start which is relative to l_whence. Lock types and corresponding open() modes. l_type mode F_RDLCK O_RDONLY or O_RDWR F_WRLCK O_WRONLY or O_RDWR The second argument to fcntl() tells it what to do with the data passed to it in the struct flock. The following list summarizes what each fcntl() cmd does: F_SETLKW This argument tells fcntl() to attempt to obtain the lock requested in the struct flock structure. If the lock cannot be obtained (since someone else has it locked already), fcntl() will wait (block) until the lock has cleared, then will set it itself. F_SETLK

37

This function is almost identical to F_SETLKW. The only difference is that this one will not wait if it cannot obtain a lock. It will return immediately with -1. This function can be used to clear a lock by setting the l_type field in the struct flock to F_UNLCK. F_GETLK This is used to get the lcok status of any file, but does not set lock. It copies the lock's information into the struct and returns back. If it can't find any conflicting lock, fcntl() returns the struct with l_type field set to F_UNLCK.

Clearing the lock Just change the l_type to F_UNLCK and call fcntl();
File locks are based on inode instead of file name, since UNIX allows multiple names to refer to the same file. All fcntl locks associated with a file for a given process are removed when any file descriptor for that file is closed by that process, even if a lock was never requested for that file descriptor. Also, fcntl locks are not inherited by a child process.

Program Code:
/* file1_lock.c using lockf() sys call */ #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/file.h> #define SEQFILE "test.txt" #define MAXBUFF 100 void my_lock(int fd) { if(lockf(fd,F_LOCK,0L) == -1) printf("Error in file locking\n"); } void my_unlock(int fd) { if(lockf(fd,F_ULOCK,0L) == -1) printf("Error in file locking\n"); } int main() {

38

int fd, i, n, pid, seqno; char buff[MAXBUFF+1]; pid = getpid(); if( (fd=open(SEQFILE, O_RDWR)) == -1) { printf("Error in opening the file\n"); exit(0); } for(i=0;i<20;i++) { my_lock(fd); lseek(fd,0L,0); if( (n=read(fd,buff,MAXBUFF)) <= 0 ) { printf("Error in reading the file\n"); exit(0); } buff[n] = '\0'; sscanf(buff,"%d\n",&seqno); printf("pid= %d, %d\n",pid,seqno); seqno++; sprintf(buff,"%03d\n",seqno); n = strlen(buff); lseek(fd,0L,0); if((write(fd,buff,n)) != n) { printf("Error in writing the file\n"); exit(0); } my_unlock(fd); } } /* file2_lock.c using flock() sys call */ void my_lock(int fd) { if(flock(fd,LOCK_EX) == -1)

39

printf("Error in file locking\n"); } void my_unlock(int fd) { if(flock(fd,LOCK_UN) == -1) printf("Error in file locking\n"); } /* file3_lock.c using fcntl() sys call */ void my_lock(int fd) { struct flock f; f.l_type = F_WRLCK; f.l_whence = SEEK_SET; f.l_start = 0; f.l_len = 0; if(fcntl(fd,F_SETLKW,&f) == -1) printf("Error in file locking\n"); } void my_unlock(int fd) { struct flock f; f.l_type = F_WRLCK; f.l_whence = SEEK_SET; f.l_start = 0; f.l_len = 0; if(fcntl(fd,F_UNLCK,&f) == -1) printf("Error in file locking\n"); }

Sample Output:
$ ./file3_lock (in one window) pid= 5455, 0 pid= 5455, 1 pid= 5455, 2 pid= 5455, 3 pid= 5455, 4 pid= 5455, 5 pid= 5455, 6 pid= 5455, 7 40

pid= 5455, 8 pid= 5455, 9 pid= 5455, 10 pid= 5455, 11 pid= 5455, 12 pid= 5455, 13 pid= 5455, 14 pid= 5455, 15 pid= 5455, 16 pid= 5455, 17 pid= 5455, 18 pid= 5455, 19 $ ./file3_lock & (in second window) [1] 5456 pid= 5456, 20 pid= 5456, 21 pid= 5456, 22 pid= 5456, 23 pid= 5456, 24 pid= 5456, 25 pid= 5456, 26 pid= 5456, 27 pid= 5456, 28 pid= 5456, 29 pid= 5456, 30 pid= 5456, 31 pid= 5456, 32 pid= 5456, 33 pid= 5456, 34 pid= 5456, 35 pid= 5456, 36 pid= 5456, 37 pid= 5456, 38 pid= 5456, 39

41

Result:
Thus the File Locking was implemented, tested and verified in UNIX system.

Ex No: 9 / /08
Problem statement:

Bankers Algorithm

To write program to implement the Bankers Algorithm to avoid deadlock

Program Description:
Get the number of process, n. Get the number of resource types, m. Get the maximum resource availability, totalR Get the maximum needs for each process, max and current allocated information, current Calculate the available vector by subtracting totalR from the column values of current vector and need vector = Max-Current. Safety Algorithm: Checks whether each process is in safe state or not. For each process, for each resource, check if the need vector is greater than avail vector. If so, then the system is unsafe state; else the system is in safe state. If the 42

system is safe, pretend the request can be granted, increase the avail vector, subtract the allocated information and mark the process finished. Get equest from some process some process i. Check the validity of request using resource request algorithm. The request vector should not be greater than the Max[i]-current[i]. If the request vector is less than the avail vector, the process can be granted new request. Then check the safety of the system.

Program Code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #define safe 1 #define unsafe 0 int max[10][10], current[10][10], need[10][10], allocated[10], avail[10], totalR[10]; int flag[10], finish[10]; void check_safe(int i, int n, int r) { int j; //printf("Evaluating availability of resources\n"); if(finish[i] == 0) { for(j=0;j<r;j++) { if((max[i][j]-current[i][j]) <= avail[j]) flag[i] = safe; else { 43

flag[i] = unsafe; break; } } /* for j loop */ if(flag[i] == safe) { printf("Resources can be granted to process %d\n",i); printf("Now the availability is\n"); for(j=0;j<r;j++) { avail[j] += current[i][j]; allocated[j] -= current[i][j]; if (avail[j] >= totalR[j]) avail[j] = totalR[j]; printf("%d\t",avail[j]); } printf("\n"); finish[i] = 1; } /* if flag[i] == safe */ else if(flag[i] == unsafe) printf("Resources cannot be granted to %d\n It may lead to deadlock\n",i); } /* if finish[i] check */ //getch(); } /* check_safe ends */ void request_res(int n, int r) { int p_no, req[10]; int i, j, flag=0; printf("Enter the process for which the resource is needed\n"); scanf("%d",&p_no); printf("Enter the extra resources needed\n"); for(i=0;i<r;i++) scanf("%d",&req[i]); /* check whether valid request */ for(i=0;i<r;i++) { if(need[p_no][i] >= req[i]) flag = 1; else { flag = 0; break; } } /* for loop */ if(flag == 0) {

44

printf("Invalid request\n"); return; } /* if flag == 0 */ /* granting the requested resources, checking safety */ else { for(i=0;i<r;i++) { if(avail[i] < req[i]) { printf("Availability of %d resource type is less, cannot grant extra resources\n",i); break; } else { current[p_no][j] += req[i]; check_safe(p_no,n,r); } } /* for i loop */ } /* else flag == 0 */ // getch(); } /* request_res ends */ void print(int r) { int i,j; printf("\nTotal Resources available "); for(j=0;j<r;j++) printf("%d\t",totalR[j]); printf("\nCurrently allocated resources "); for(j=0;j<r;j++) printf("%d\t",allocated[j]); printf("\nCurrently available resources "); for(j=0;j<r;j++) printf("%d\t",avail[j]); printf("\n"); } void main() { int i,j,n,r,p,choice; clrscr(); printf("Enter number of processes\n"); scanf("%d",&n); printf("Enter number of resource types\n"); scanf("%d",&r);

45

printf("Enter the max availability of resources\n"); for(j=0;j<r;j++) scanf("%d",&totalR[j]); printf("Enter the max need of each process\n"); for(i=0;i<n;i++) for(j=0;j<r;j++) scanf("%d",&max[i][j]); printf("Enter the current allocation of each process\n"); for(i=0;i<n;i++) for(j=0;j<r;j++) scanf("%d",&current[i][j]); printf("Need of each process "); for(i=0;i<n;i++) for(j=0;j<r;j++) { need[i][j] = max[i][j] - current[i][j]; printf("%d\t",need[i][j]); } printf("\n"); for(j=0;j<r;j++) allocated[j] = avail[0] = 0; for(j=0;j<r;j++) { for(i=0;i<n;i++) allocated[j] += current[i][j]; avail[j] = totalR[j] - allocated[j]; } print(r); for(j=0;j<n;j++) finish[j] = flag[j] = 0; do { printf("\nMenu\n1. Check Safe\n2. Request resource\n3. Exit\n Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 1: for(p=0;p<n;p++) check_safe(p,n,r); print(r); break; case 2: request_res(n,r); print(r); break;

46

case 3: exit(0); } }while(choice <=3); } /* end main */

Sample Output:
Enter number of processes 3 Enter number of resource types 3 Enter the max availability of resources 7 7 10 Enter the max need of each process 366 433 344 Enter the current allocation of each process 223 203 124 Need of each process 1 4 3 2 3 0 2 2 0 Total Resources available 7 7 Currently allocated resources 5 4 Currently available resources 2 3 Menu 1. Check Safe 2. Request resource 3. Exit Enter your choice 1 Resources cannot be granted to process 0, It may lead to deadlock Resources can be granted to process 1 Now the availability is 4 3 3 Resources can be granted to process 2 47 10 10 0

Now the availability is

7 10 3 7

Total Resources available 7 7 Currently allocated resources 2 2 Currently available resources 5 5 Menu 1. Check Safe 2. Request resource 3. Exit Enter your choice 2

Enter the process for which the resource is needed 0 Enter the extra resources needed 110 Resources can be granted to process 0 Now the availabiltiy is 7 7 10 Total Resources available 7 7 10 Currently allocated resources 0 0 0 Currently available resources 7 7 10 Menu 1. Check Safe 2. Request resource 3. Exit Enter your choice 2 Enter the process for which the resource is needed 0 Enter the extra resources needed 213 Invalid Request Total Resources available 7 7 Currently allocated resources 2 2 Currently available resources 5 5 Menu 1. Check Safe 2. Request resource 3. Exit 3 10 3 7

48

Result:
Thus the Banker's algorithm to avoid deadload was implemented, tested and verified.

Ex No: 10 / /08
Problem Statement:

Disk Scheduling Algorithms

To write a program to implement the following disk scheduling algorithms FCFS SSTF SCAN C-SCAN LOOK C-LOOK

Problem Description:
Get the number of cylinders, nc and the number of requests, nr Get the current head position, head. Get the requests, treq[] Total head movement = Number of cylinders that head moved to process requests. FCFS scheduling: Process the requests in order of their arrival and calculate total head movement 49

SSTF scheduling: For each request, find the request, which is closest to the current head position and process that request. SCAN scheduling: Get the previous request Find the direction of head movement. Process the requests that are in the way of head movement If the head reaches end of cylinder, then it is made to reverse the direction and continue to process the requests. LOOK scheduling: Get the previous request Find the direction of head movement. Process the requests that are in the way of head movement If the head reaches last request in that direction of movement, then it is made to reverse the direction and continue process the requests. C-SCAN scheduling: Get the previous request Find the direction of head movement. The head moves in only one direction. Process the requests that are in the way of head movement, If the head reaches last request in the direction of head movement, then instead of reversing the direction of head, it is swept back to the location where next request is made from beginning without processing requests on return paths and then continue. C-LOOK scheduling: Get the previous request Find the direction of head movement. The head moves from one end to other end Process the requests that are in the way of head movement

50

If the head reaches end of cylinder, then instead of reversing the direction of head, it is swept back to the starting location without processing requests on return paths and then continue.

Program Code;
#include <stdio.h> #include <stdlib.h> #include <conio.h> int nc, nr, head, prev, treq[20]; int thm=0; void sort_asc(int l[], int ln) { int i,j, temp; for(i=0;i<ln;i++) for(j=i+1;j<ln;j++) if(l[i] > l[j]) { temp = l[i]; l[i] = l[j]; l[j] = temp; } } void sort_desc(int g[], int gn) { int i,j, temp; for(i=0;i<gn;i++) for(j=i+1;j<gn;j++) 51

if(g[i] < g[j]) { temp = g[i]; g[i] = g[j]; g[j] = temp; } } void calc_thm(int g[], int l[], int gn, int ln, int head) { int i; thm = 0; thm += abs(head-g[0]); for(i=0;i<gn-1;i++) { printf("%d -> ",g[i]); thm += abs(g[i]-g[i+1]); } printf("%d -> ",g[gn-1]); thm += abs(g[gn-1]-l[0]); for(i=0;i<ln-1;i++) { printf("%d -> ",l[i]); thm += abs(l[i]-l[i+1]); } printf("%d -> ",l[ln-1]); printf("Total Head Movement %d\n",thm); } int find_min(int req[], int nr, int thead) { int i, index, min=9999; for(i=0;i<nr;i++) { if(abs(thead-req[i]) < min) { min = abs(thead-req[i]); index = i; } } return index; } void fcfs() { int i; thm = 0; printf("%d -> ",head); thm = abs(head-treq[0]); for(i=0;i<nr-1;i++) { printf("%d -> ",treq[i]); thm+=abs(treq[i]-treq[i+1]);

52

} printf("%d\n",treq[i]); printf("Total Head Movement %d\n",thm); } void sstf() { int thead, req[20], i, min_index; thead = head; for(i=0;i<nr;i++) req[i] = treq[i]; thm = 0; printf("%d -> ",thead); for(i=0;i<nr;i++) { min_index = find_min(req,nr,thead); thm += abs(thead-req[min_index]); if(i != nr-1) printf("%d -> ",req[min_index]); else printf("%d\n",req[min_index]); thead = req[min_index]; req[min_index] = 9999; } printf("\nTotal Head Movement %d\n",thm); } void scan() { int i, ln=0, gn=0, l[20], g[20]; thm = 0; printf("%d -> ",head); for(i=0;i<nr;i++) { if(treq[i] > head) g[gn++] = treq[i]; else l[ln++] = treq[i]; } if(head-prev > 0) { g[gn++] = nc-1; sort_asc(g,gn); sort_desc(l,ln); calc_thm(g,l,gn,ln,head); } else { l[ln++] = 0; sort_asc(g,gn);

53

sort_desc(l,ln); calc_thm(l,g,ln,gn,head); } } void look() { int i, ln=0, gn=0, l[20], g[20]; thm = 0; printf("%d -> ",head); for(i=0;i<nr;i++) { if(treq[i] > head) g[gn++] = treq[i]; else l[ln++] = treq[i]; } sort_asc(g,gn); sort_desc(l,ln); if(head-prev > 0) calc_thm(g,l,gn,ln,head); else calc_thm(l,g,ln,gn,head); } void c_scan() { int i, ln=0, gn=0, l[20], g[20]; thm = 0; printf("%d -> ",head); for(i=0;i<nr;i++) { if(treq[i] > head) g[gn++] = treq[i]; else l[ln++] = treq[i]; } g[gn++] = nc-1; l[ln++] = 0; if(head-prev > 0) { sort_asc(g,gn); sort_asc(l,ln); calc_thm(g,l,gn,ln,head); } else { sort_desc(g,gn); sort_desc(l,ln); calc_thm(l,g,ln,gn,head);

54

} } void c_look() { int i, ln=0, gn=0, l[20], g[20]; thm = 0; printf("%d -> ",head); for(i=0;i<nr;i++) { if(treq[i] > head) g[gn++] = treq[i]; else l[ln++] = treq[i]; } if(head-prev > 0) { sort_asc(g,gn); sort_asc(l,ln); calc_thm(g,l,gn,ln,head); } else { sort_desc(g,gn); sort_desc(l,ln); calc_thm(l,g,ln,gn,head); } } void main() { int choice; int i; clrscr(); printf("Enter number of cylinders and requests\n"); scanf("%d%d",&nc,&nr); printf("Enter the current head position\n"); scanf("%d",&head); for(i=0;i<nr;i++) { printf("Enter the request\n"); scanf("%d",&treq[i]); } do { printf("\nMenu\n1.FCFS\t2.SSTF\t3.SCAN\t4.LOOK\t5.C-SCAN\t6.CLOOK\t7.Exit\n"); scanf("%d",&choice);

55

switch(choice) { case 1: printf("=========FCFS Disk Scheduling=========\n"); fcfs(); break; case 2: printf("=========SSTF Disk Scheduling=========\n"); sstf(); break; case 3: printf("=========SCAN Disk Scheduling=========\n"); printf("Enter previous request\n"); scanf("%d",&prev); scan(); break; case 4: printf("=========LOOK Disk Scheduling=========\n"); printf("Enter previous request\n"); scanf("%d",&prev); look(); break; case 5: printf("=========C-SCAN Disk Scheduling=========\n"); printf("Enter previous request\n"); scanf("%d",&prev); c_scan(); break; case 6: printf("=========C-LOOK Disk Scheduling=========\n"); printf("Enter previous request\n"); scanf("%d",&prev); c_look(); break; case 7: exit(0); } /* end switch */ } while(choice <= 7); } /* end main */

Sample Output:
Enter the number of cylinders 100 Enter current head position 21 Enter the number of requests 7 Enter the request 1:35 56

Enter the request 2:17 Enter the request 3:25 Enter the request 4:50 Enter the request 5:2 Enter the request 6:40 Enter the request 7:23 FCFS scheduling 21 ->3 5-> 17->2 S-> 50->2->40->2 3 Total head movement:168 SSTF scheduling 21->23->25->17->2->35->40->50 Total head movement:75 SCAN scheduling Enter the previous request serviced 10 21->23->25->35->40->50->99->17->2 Total head movement:175 C-SCAN scheduling Enter the previous request serviced 10 21->23->25->35->40->50->99->0->2->17 Total head movement.194 LOOK scheduling Enter the previous request serviced 10 21->23->25->35^40->50->17->2 Total head movemcnt:77 C-LOOK scheduling Enter the previous request serviced 10 21->23->25->35->40->50->2->17 Total head movement: 92

57

Result:
Thus the various disk scheduling algorithms like FCFS, SSTF, SCAN, C-SCAN, LOOK and C-LOOK were implemented, tested and verified.

58

You might also like