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
MANAGER CLASSpackage 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);
}
}
PROCESS CLASSpackage 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);
}
}
}
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);
// }
}
}
}