Results 1 to 1 of 1
  1. #1
    SAiNT_JiMMiE is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Shortest Seek-Time First with java.util.concurrent

    Hello!
    I'm having an assignment to simulate SSTF (shortest seek time first) using Java concurrency. Here is my code.
    Can someone look at the code and tell me how to correct the waitTime for each process. As you'll see all the processes are making a request to the manager for a hdd sector (0 to 999999), which is in an actual cylinder (let's say sector 344555 is on cylinder 345). Each cylinder has 1000 sectors and there are 1000 cylinders in the hdd. The hdd implementation is not an issue here, it's more important to implement the threads in a correct way.
    The manager thread needs to satisfy only one process request at a time. That's why i have put queue.poll() in a protected area. The manager thread's sleepTime should be the movement of the head of the hdd (initially on sector 0) to the nearest requested sector (let's say sector 123445 which is on cylinder 124, so the manager sleeps 124 ms to satisfy this request). I hope you understood me just a little bit.

    You can check out the SLEEPING BARBER PROBLEM, it's something similar regarding the thread implementation (one thread manager/barber, n threads processes/clients).

    MAIN CLASS
    package test;

    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Main {
    public static void main(String[] args) {
    Manager manager = new Manager();
    Process processes[] = new Process[10];

    for(int i = 0; i < processes.length; i++){
    processes[i] = new Process(manager, i);
    processes[i].start();
    }
    manager.start();

    try {
    for(int i=0; i<processes.length; i++)
    processes[i].join();
    manager.join();
    } catch(InterruptedException e) {
    Logger.getLogger(Main.class.getName()).log(Level.S EVERE, null, e);
    }

    System.out.println(manager.out);
    for(int i = 0; i < processes.length; i++){
    System.out.println("Wtime\t"+processes[i].getName()+"\t"+manager.waitTime);
    }
    System.out.println("...END...");
    System.exit(0);
    }
    }
    MANAGER CLASS
    package test;

    import java.util.concurrent.locks.*;
    import java.util.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Manager extends Thread {
    private int seekTime = 1;
    private int waitingProcesses = 0;
    private int maxTasks = Process.PROCESSI;
    private Lock lockVar = new ReentrantLock();
    private Lock lockOut = new ReentrantLock();
    private PriorityQueue<Process> queue =
    new PriorityQueue<Process>(Process.PROCESSI,
    new Comparator<Process>() {
    public int compare(Process a, Process b) {
    return a.compareTo(b);
    }
    }
    );
    public String out = "";
    public long waitTime = 0;
    public long reqTime =0;


    public Manager() {
    super("Hard-Disk Manager: ");
    }

    private void printOut(String s){
    this.lockOut.lock();
    this.out = this.out + "\n" + s;
    this.lockOut.unlock();
    }

    public void queueProcess(Process c){
    this.lockVar.lock();
    this.reqTime = System.currentTimeMillis();
    if(this.waitingProcesses < this.maxTasks){

    this.printOut(c.getName() + "requests access to cylinder C_" +
    c.getCylinder() + " ("
    + c.getSector() + " sector)");
    this.waitingProcesses++;
    this.lockVar.unlock();
    // try {
    this.queue.offer(c);
    //Thread.sleep(50);
    // } catch(InterruptedException e){
    // Logger.getLogger(Process.class.getName()).log(Leve l.SEVERE, null, e);
    //}
    }
    }

    public boolean isAvailable(){
    this.lockVar.lock();
    try {
    return this.maxTasks - this.waitingProcesses > 0 ? true : false;
    } finally {
    this.lockVar.unlock();
    }
    }

    public void run(){
    while(this.waitingProcesses > 0) {
    try {


    this.lockVar.lock();
    this.waitTime+=System.currentTimeMillis()-this.reqTime;
    Process c = (Process)this.queue.poll();
    this.printOut(getName() + "processing " + c.getName() + " for C_" + c.getCylinder());

    this.waitingProcesses--;
    this.maxTasks--;
    this.lockVar.unlock();

    Thread.sleep(this.seekTime);

    } catch(InterruptedException e) {
    Logger.getLogger(Manager.class.getName()).log(Leve l.SEVERE, null, e);
    }
    this.printOut("...waiting processes: " + this.waitingProcesses);
    }
    }
    }
    PROCESS CLASS
    package test;

    import java.util.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Process extends Thread implements Comparable<Process> {
    public final static int PROCESSI = 10;
    private Manager manager;
    private int settore;
    private int index;



    public Process(Manager m, int ind){
    super("P_" + ind);
    this.manager = m;
    this.index = ind;
    }

    public int compareTo(Process c) {
    return this.getSector()-c.getSector();
    }

    public int getIndex() {
    return this.index;
    }

    public int getSector() {
    return this.settore;
    }

    public void setSettore(int newSettore) {
    settore = newSettore;
    }

    public int getCylinder() {
    return (this.settore/1000)+1;
    }

    public void run(){
    Random rnd = new Random();
    int random = rnd.nextInt(999999);

    this.setSettore(random);
    while(this.manager.isAvailable()){

    this.manager.queueProcess(this);
    // try {

    // Thread.sleep(50);
    // } catch (InterruptedException e) {
    // Logger.getLogger(Process.class.getName()).log(Leve l.SEVERE, null, e);
    // }
    }
    }
    }
    Last edited by SAiNT_JiMMiE; 05-11-2010 at 10:20 AM.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. RCU in Java? or some list/set/etc. which can handle concurrent access?
    By flok in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-14-2009, 10:59 PM
  3. Replies: 0
    Last Post: 05-18-2008, 09:31 AM
  4. Replies: 1
    Last Post: 03-21-2008, 03:14 PM
  5. seek to mp3 file
    By po0oker in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2007, 11:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •