Saturday, 23 March 2013

FCFS Disk Scheduling Algorithm with Java

package javaos12;

/**
 *
 * @author Cristina
 */
public class JavaOS12 {

    public static void main(String[] args) {
   
        int[] queue = {98, 183, 37, 122, 14, 124, 65, 67};
        int headStart = 53; 
       
        DiskScheduler fcfs = new FCFS(queue, headStart);
       
        System.out.println("Head starts at: " + headStart);
        System.out.print("Queue = ");
        for(int i: queue){
            System.out.print(i + " ");
        }
        System.out.println("");
       
        fcfs.println();
       
    }
}

interface DiskScheduler
{
    public int serviceRequests();
    public int[] path();
    public void println();
}


class FCFS implements DiskScheduler{
    int[] queue;
    int initialCylinder;
    public FCFS(int[] queue, int initialCylinder){
        this.queue = queue;
        this.initialCylinder = initialCylinder;
    }
    public int serviceRequests(){
        int headMovement = 0;
        int prev = initialCylinder;
        int [] rpath = path();
        System.out.println(rpath.length);
        for (int i=0; i < rpath.length; i++) {
           headMovement += Math.abs(rpath[i]-prev);
            prev = rpath[i];
        }
        return headMovement;
    }
   
    public int[] path(){
       int [] resultPath = new int[queue.length];
       for (int i = 0; i < queue.length; i++){
        resultPath[i] = queue[i];
    }
       return resultPath;
    }
   
    public void println(){
    System.out.println("FCFS head movement = " + serviceRequests());
       
        System.out.print("FCFS Path = ");
        for(int i: path()){
            System.out.print(i + " "); 
        }
        System.out.println("");
}
}


Bibliography

Operating System Concepts with Java, Avi Silberschatz, Peter Baer Galvin, Greg Gagne

1 comment:

RAJENDRA said...

Thanks that helped a lot