Sunday, 24 March 2013

Disk Scheduling Simulator with JavaFX



import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

/**
 *
 * @author Cristina
 */
public class JavaFXApplication1 extends Application {
   
    @Override
    public void start(Stage primaryStage) {
      primaryStage.setTitle("Disk Scheduling Simulator");
      Group root = new Group();
      Scene scene = new Scene(root, 750, 500);
     
      int[] queue = {98, 183, 37, 122, 14, 124, 65, 67};
      int headStart = 53; 
       
      DiskScheduler fcfs = new FCFS(queue, headStart);
     
      int [] result = fcfs.path();
    
      Path path = new Path(); 
   
      MoveTo moveTo = new MoveTo();
      moveTo.setX(headStart);
      moveTo.setY(50);
      path.getElements().add(moveTo); 
     
  for(int i = 0; i < result.length; i++){      
          LineTo lineTo = new LineTo();
          lineTo.setX(4*result[i]);
          lineTo.setY(50 + i*50); 
          path.getElements().add(lineTo);
      }
     
      path.setStrokeWidth(3);
      path.setStroke(Color.BLUE);
      root.getChildren().add(path);  
      primaryStage.setScene(scene);
      primaryStage.show();
    }

Bibliography

[1] http://java-buddy.blogspot.ro/2012/02/javafx-20-exercise-draw-basic-line.html
[2] Avi Silberschatz, Peter Baer Galvin, Greg Gagne: Operating System Concepts with Java

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