Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
1
Indexable
Never
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class RoundRobbin {
    public static class ProcessRR{
        String name;
        int execution;
        int execution2;
        int arrival;
        int complete;
        float waiting_time=0;
        boolean completion= false;
        float avt;
    }
    public static void main(String[] args) {
        ArrayList<ProcessRR> processes1= new ArrayList<ProcessRR>();
        int quantomTime=1;
        File file = new File("text.txt");
        ArrayList<ProcessRR> processes= new ArrayList<ProcessRR>();

        try {
            //reading file
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()){
                String str= scanner.nextLine();
                String[] data= str.split(" ");
                ProcessRR processRR= new ProcessRR();
                processRR.name=data[0];
                processRR.execution=Integer.valueOf(data[1]);
                processRR.execution2=Integer.valueOf(data[1]);
                processRR.arrival=Integer.valueOf(data[2]);
                processes1.add(processRR);

            }
            scanner.close();
            //sorting
            while (processes1.size()!=0){
                int minArrival=processes1.get(0).arrival;
                ProcessRR processRRMin = processes1.get(0);
                for (ProcessRR processRR:processes1) {
                    if(processRR.arrival<minArrival){
                        minArrival=processRR.arrival;
                        processRRMin= processRR;
                    }
                }
                processes.add(processRRMin);
                processes1.remove(processRRMin);
                if(processes1.size()==0) break;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }






        int count_of_complete=0;
        int time=0;
        boolean time_up=false;
        while(processes.size()!=count_of_complete) {
            time_up=false;
            for (ProcessRR pr:processes) {
                if(pr.arrival<=time){
                    if(!pr.completion){
                        for (int i = 0; i < quantomTime; i++) {
                            pr.execution--;
                            time++;
                            time_up=true;
                            if(pr.execution==0){
                                pr.completion=true;
                                pr.complete=time;
                                count_of_complete++;
                                break;
                            }

                        }

                    }
                }

            }
            if(!time_up) time++;
        }








        float waiting_time_Average=0;
        float aVT=0;
        for (ProcessRR pr:processes) {
            pr.avt=pr.complete-pr.arrival;
            if(pr.avt<0) pr.avt=0;
            pr.waiting_time=pr.avt-pr.execution2;
            if(pr.waiting_time<0) pr.waiting_time=0;
        }
        for (ProcessRR processRR:processes) {
            waiting_time_Average+= processRR.waiting_time;
            aVT+= processRR.avt;
        }
        if(waiting_time_Average!=0){
            waiting_time_Average= waiting_time_Average/processes.size();
        }
        if(aVT!=0){
            aVT= aVT/processes.size();
        }
        System.out.println(waiting_time_Average);
        System.out.println(aVT);

    }
}