Sei sulla pagina 1di 4

public class Main{

static void findWaitingTime(int processes[], int n, int bt[], int wt[], int
at[])
{
int service_time[] = new int[n];
service_time[0] = 0;
wt[0] = 0;

for (int i = 1; i < n ; i++)


{
service_time[i] = service_time[i-1] + bt[i-1];
wt[i] = service_time[i] - at[i];
if (wt[i] < 0)
wt[i] = 0;
}
}

static void findTurnAroundTime(int processes[], int n, int bt[],


int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

static void findavgTime(int processes[], int n, int bt[], int at[])


{
int wt[] = new int[n], tat[] = new int[n];
findWaitingTime(processes, n, bt, wt, at);
findTurnAroundTime(processes, n, bt, wt, tat);
System.out.print("Processes " + " Burst Time " + " Arrival Time "
+ " Waiting Time " + " Turn Around Time "
+ " Completion Time \n");
int total_wt = 0, total_tat = 0;
for (int i = 0 ; i < n ; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
int compl_time = tat[i] + at[i];
System.out.println(i+1 + "\t\t" + bt[i] + "\t\t"
+ at[i] + "\t\t" + wt[i] + "\t\t "
+ tat[i] + "\t\t " + compl_time);
}

System.out.print("Average waiting time = "


+ (float)total_wt / (float)n);
System.out.print("\nAverage turn around time = "
+ (float)total_tat / (float)n);
}
public static void main(String args[]) {

int processes[] = {1, 2, 3, 4, 5};


int n = processes.length;

int burst_time[] = {6, 4, 1, 3, 5};


int arrival_time[] = {0, 2, 5, 6, 9};

findavgTime(processes, n, burst_time, arrival_time);

}
}
public class Main{

public static void main(String args[]){


Scanner in = new Scanner(System.in);
System.out.print("Enter number of process(s): ");
int processnum = in.nextInt();
Process prosess[] = new Process[processnum];

for(int i=0; i<processnum; i++){


System.out.println("Enter burst time for process "+1);
int bt = in.nextInt();
process[i] = new Process(bt);
}

System.out.println("Enter quantum time: ");


int qt = in.nextInt();
int servicetime=0;

for(int i=0; i<process;){


System.out.println("Process "+i);
System.out.println(servicetime);

for(int time=0; time<qt; time++){


process[i].isComplete();
if(process[i].Complete){
process[i].waiting(servicetime-process[i].checkburst());
process[i].turn(servicetime);
break;
}
else{process[i].minus(); servicetime++;}
}
i++;
if(i == process){
for (int check=0; check<process; check++){
if(process[check].Complete == false){i = 0; break;}
}
}
}
double avgwaittime = 0;
for(int i = 0; i<processnum; i++){
System.out.println("Process: "+i+" Waiting time: "+process[i].waiting+"
Turnararound time: "+process[i].turnaround);
avgwaittime += process[i].waiting;
}
avgwaittime /+ processnum;
System.out.println("Avegrage waiting time: "+avgwaittime);
}

public class Process{


public int burst, staticburst=0;
public int turnaround=0;
boolean Complete=false;
int waiting = 0;
Process(int newburst){
this.burst=newburst;
this.staticburst=newburst;
}

void isComplete(){
if(burst == 0)
this.Complete=true;
}
}

void minus(){
this.burst -= 1;
}

int checkburst(){
return this.staticburst;
}

void waiting (int newwait){


if(this.waiting == 0)
this.waiting = newwait;
}

void turn(int newturn){


if(this.turnaround == 0)
this.turnaround = newturn;
}

Potrebbero piacerti anche