Java-用于多个服务器的调度算法

时间:2018-12-13 14:10:00

标签: java job-scheduling

我是一名大学生,我有一个作业,要创建一个简单的调度模拟器。我将创建约4种算法,FCFS是第一种,但这是问题所在。我制定了算法,并且只对一台服务器起作用(因为他的提速通常是最好/更快的是四台服务器中的一台,因此每个任务都分配给这台服务器)。我无法找到如何将任务/作业“分配”到其他服务器上以达到某种平衡,而不仅是一项艰巨的工作。例如,我通常创建4台服务器,加速从8到12,以及20个任务。所有任务将检查具有最佳完成时间的服务器,因此,如果ID 2的服务器的加速达到12,则将始终选择该服务器。我对如何指示调度程序识别任务是否仍然可以分配给最佳服务器或分配给另一个服务器的编程部分感到困惑。另外,尽管我将前4个任务分配给每个服务器,但是后来我不知道如何检查哪个服务器完成了他的任务以及如何通知系统以使第5个任务完成。我认为这与每个任务的到达时间有关,但我不确定。因此,结论是,我所需要的只是帮助我重建代码,以便它可用于许多服务器,而不仅限于一台服务器,我检查了伪代码,但在编程方面并没有那么大的帮助,如果有人以前做过此事并且可以提供帮助,我将不胜感激。给我说明或代码示例。如果您不懂或需要更多信息,请问我。

预先感谢您!

注意:到达时间不是0,每个任务都在不同的时间到达,它也是一个模拟,所以我不使用多线程,而只是基本编程。

这是FCFS代码:

public static void Fcfs(ArrayList<Tasks> task,ArrayList<Server> server){
    int taskList_size,serverList_size,i,j,bestServer = -1;
    double tmp_burst,tmp_arrival,bestTime,tmpNewBurst;

    taskList_size = task.size();
    serverList_size = server.size();

    //Find the best server and store + sort all new burst times of a task per server
    i = 0;
    while(i < taskList_size){
        System.out.println("\nNumber: "+(i+1)+" Task ID: "+task.get(i).getTaskId());

        tmp_burst = task.get(i).getBurstTime();        
        System.out.printf("Original burst time: %.3f\n",tmp_burst);

        bestTime = 300000000000.00;
        j = 0;
        while(j < serverList_size){
            tmpNewBurst = tmp_burst/server.get(j).getSpeeUp();
            task.get(i).setServerListTime(tmpNewBurst);

            if(tmpNewBurst < bestTime){
                bestTime = tmpNewBurst;
                bestServer = j;
            }
            System.out.printf("Server ID: %d new burst time %.3f\n",j,tmpNewBurst);
            j++;
        }
        task.get(i).setNewBurtTime(bestTime);
        task.get(i).setBestServer(bestServer);

        System.out.printf("Best server ID: %d best time: %.2f\n",bestServer,bestTime);
        i++;
    }
    //sortTaskArray(task);     

    //Calculate average + total waiting time + average/total turn around time
    averageTime(task);
}

等待时间计算代码:

public static void taskWaitingTime(ArrayList<Tasks> task){
    int i,taskSize;
    double[] serviceTime;
    double tmpBurst;
    double tmpWaitingTime;
    double tmpArrivalTime;

    taskSize = task.size();
    serviceTime = new double[taskSize];

    serviceTime[0] = 0.0;
    tmpWaitingTime = 0;
    for(i = 1; i < taskSize; i++){
        // Add burst time of previous processes
        tmpBurst = task.get(i-1).getNewBurstTime();
        serviceTime[i] = serviceTime[i-1] + tmpBurst;

        // Find waiting time for current process =
        // sum - at[i]
        tmpArrivalTime = task.get(i).getArrivalTime();
        tmpWaitingTime = serviceTime[i] - tmpArrivalTime;
        task.get(i).setWaitingTime(tmpWaitingTime);

        // If waiting time for a process is in negative
        // that means it is already in the ready queue
        // before CPU becomes idle so its waiting time is 0
        if (tmpWaitingTime < 0)
            task.get(i).setWaitingTime(0);      
    }
}

扭转时间计算代码:

public static void taskTurnAroundTime(ArrayList<Tasks> task){
    int i,taskSize;
    double tmpBurst,tmpTurnAroundTime,tmpWaitingTime;

    taskSize = task.size();
    for(i = 0;i < taskSize;i++){
        tmpBurst = (double)task.get(i).getNewBurstTime();
        tmpWaitingTime = task.get(i).getWaitingTime();

        tmpTurnAroundTime = tmpBurst + tmpWaitingTime;
        task.get(i).setTurnAroundTime(tmpTurnAroundTime);
    }
}

平均时间计算代码:

public static void averageTime(ArrayList<Tasks> task){

    int size;
    double waiting,turnAround,avwaiting,avturnAround;

    waiting = turnAround = avwaiting = avturnAround = 0;

    size = task.size();

    //Calculate waiting and turn around time for single best server
    taskWaitingTime(task);
    taskTurnAroundTime(task);

    System.out.println("\nTask's time assigned to the best server");
    for(Tasks x:task){    
        System.out.printf("Task ID: %d Arrival time: %.3f Burst time: %.3f New Burst time: %.3f Waiting Time: %.3f "
                + "Turn around time: %.3f"
                +"\n",x.getTaskId(),(double)x.getArrivalTime(),(double)x.getBurstTime(),x.getNewBurstTime(),x.getWaitingTime(),x.getTurnAroundTime());
    }

    System.out.println();

    //Calculate total waiting and turn around time
    for(Tasks x:task){
        waiting = waiting + x.getWaitingTime();
        turnAround = turnAround + x.getTurnAroundTime();
    }
    System.out.printf("Total waiting time: %.2f Total turn around time: %.2f\n",waiting,turnAround);

    avwaiting = waiting/(double)size;
    avturnAround = turnAround/(double)size;
    System.out.printf("Average waiting time: %.2f Average turn around time: %.2f\n",avwaiting,avturnAround);

}

编辑: 任务/工作班:

public class Tasks{

private int tId;
private double burstTime;
private double newBurstTime;
private double remainingTime;
private double arrivalTime;
private int bestServer;
private int isCompleted;
private double waitingTime;
private double turnAroundTime;
private double completionTime;
private ArrayList<Double> serversTime;

public Tasks(){
    this.tId = 0;
    this.burstTime = 0.000;
    this.newBurstTime = 0.000;
    this.remainingTime = 0.000;
    this.arrivalTime = 0.000;
    this.bestServer = 0;
    this.waitingTime = 0;
    this.turnAroundTime = 0;
    this.isCompleted = 0;
    this.completionTime = 0;
    this.serversTime = new ArrayList<Double>();
}

public Tasks(int tId,double arrivalTime,double burstTime){
    this.tId = tId;
    this.burstTime = burstTime;
    this.arrivalTime = arrivalTime;
    this.serversTime = new ArrayList();
    this.remainingTime = this.burstTime;
}

public void setBurstTime(double x){
    this.burstTime = x;
}

public void setNewBurtTime(double x){
    this.newBurstTime = x;
}

public void setRemainingTime(int x){
    this.remainingTime = x;
}
public void setBestServer(int bestServer){
    this.bestServer = bestServer;
}

public void setServerListTime(double serverTimeExe){
    this.serversTime.add(serverTimeExe);
}

public void setIsCompleted(int compl){
    this.isCompleted = compl;
}

public void setCompletionTime(double completionTime){
    this.completionTime = completionTime;
}

public void setWaitingTime(double waitingTime){
    this.waitingTime = waitingTime;
}

public void setTurnAroundTime(double x){
    this.turnAroundTime = x;
}

public int getTaskId(){
    return this.tId;
}

public double getBurstTime(){
    return this.burstTime;
}

public double getNewBurstTime(){
    return this.newBurstTime;
}

public double getRemainingTime(){
    return this.remainingTime;
}

public double getArrivalTime(){
    return this.arrivalTime;
}

public double getWaitingTime(){
    return this.waitingTime;
}

public double getTurnAroundTime(){
    return this.turnAroundTime;
}

public ArrayList getServerListTime(){
    return this.serversTime;
}

public int getIsCompleted(){
    return this.isCompleted;
}

public double getCompletionTime(){
    return this.completionTime;
}  

public static Comparator<Tasks> TaskArrTime = new Comparator<Tasks>(){
    @Override
    public int compare(Tasks x1, Tasks x2){
        return Double.compare((x1.getArrivalTime()),(x2.getArrivalTime()));
    }
};

}

服务器类:

public class Server {

private int serverID;
private int coreNum;
private int totalWorkTime;
private int availability;
private int currentTaskID;
private int connections;
private double speedUp;
private ArrayList<Tasks> queuedTask;
//private int energyConsumed;

public Server(){
    this.serverID = 0;
    this.coreNum = 4;
    this.connections = 0;
    this.totalWorkTime = 0;
    this.availability = 1;
    this.speedUp = 8.0;
    this.currentTaskID = -1;
    this.queuedTask = new ArrayList<Tasks>();
    //this.energyConsumed = 0;
}

public Server(int serverId,int coreNum,double speedUP){
    this.serverID = serverId;
    this.coreNum = coreNum;
    this.speedUp = speedUP;
    this.availability = 1;
    this.currentTaskID = -1;
}

public void setServerID(int serverID){
    this.serverID = serverID;
}

public void setCoreNum(int coreNum){
    this.coreNum = coreNum;
}

public void setTotalWorkTime(int totalWorkTime){
    this.totalWorkTime = totalWorkTime;
}

public void setAvailability(int avail){
    this.availability= avail; 
}

public void setCurrentTaskID(int curTask){
    this.currentTaskID = curTask;
}

public void setConnections(int x){
    this.connections = x;
}

public void setSpeedUp(){
    this.speedUp -= 1;
}

/*public void setEnergyConsumed(int energyConsumed){
    this.energyConsumed = energyConsumed;
}
*/

public int getServerID(){
    return serverID;
}

public int getCoreNumber(){
    return coreNum;
}

public int getConnections(){
    return connections;
}

public int getTotalWorkTime(){
    return totalWorkTime;
}

public int getAvailability(){
    return availability;
}

public int getCurrentTaskID(){
    return currentTaskID;
}

public double getSpeeUp(){
    return speedUp;
}

public ArrayList<Tasks> getQueuedTasks(){
    return queuedTask;
}
/*public int getEnergyConsumed(){
    return energyConsumed;
}
*/

0 个答案:

没有答案