-
Notifications
You must be signed in to change notification settings - Fork 0
/
Priority.java
143 lines (131 loc) · 2.7 KB
/
Priority.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package SPOS;
import java.util.*;
public class priority
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of process : ");
int n= sc.nextInt();
int pid[] = new int[n];
int at[] = new int[n];
int bt[] = new int[n];
int prio[] = new int[n];
int ct[] = new int[n];
int tat[] = new int[n];
int wt[] = new int[n];
int f[] = new int [n];
int i,temp, st=0, tot=0;
float avg_wt=0, avg_tat=0;
for (i=0;i<n;i++)
{
pid[i]= i+1;
System.out.println ("Enter arrival time of process " +(i+1)+ ":");
at[i]= sc.nextInt();
System.out.println("Enter burst time of process " +(i+1)+ ":");
bt[i]= sc.nextInt();
System.out.println("Enter priority of process " +(i+1)+ ":");
prio[i]= sc.nextInt();
}
for(i=0 ; i<n; i++)
{
for(int j=0; j<n-(i+1) ; j++)
{
if( at[j] >= at[j+1])
{
temp = at[j];
at[j] = at[j+1];
at[j+1] = temp;
temp = bt[j];
bt[j] = bt[j+1];
bt[j+1] = temp;
temp = pid[j];
pid[j] = pid[j+1];
pid[j+1] = temp;
temp=prio[j];
prio[j]=prio[j+1];
prio[j+1]=temp;
}
}
}
while(true)
{
int min=99,c=n;
if (tot==n)
break;
for (i=0;i<n;i++)
{
if ((at[i]<=st) && (f[i]==0) && (prio[i]<min))
{
min=prio[i];
c=i;
}
}
if (c==n)
st++;
else
{
ct[c]= st+bt[c];
st=st+bt[c];
f[c]=1;
tot++;
}
for(i=0 ; i<n; i++)
{
for(int j=0; j<n-(i+1) ; j++)
{
if( st>at[j] && st>at[j+1] && f[j]==0)
{
if(prio[j]>prio[j+1] )
{
temp = at[j];
at[j] = at[j+1];
at[j+1] = temp;
temp = bt[j];
bt[j] = bt[j+1];
bt[j+1] = temp;
temp = pid[j];
pid[j] = pid[j+1];
pid[j+1] = temp;
temp=prio[j];
prio[j]=prio[j+1];
prio[j+1]=temp;
}
}
}
}
}
for(i=0 ; i<n; i++)
{
if( i == 0)
{
ct[i]= at[i]+bt[i];
}
else
{
if( at[i]>ct[i-1])
{
ct[i]= at[i] +bt[i];
f[i]=1;
}
else
{
ct[i]= ct[i-1] +bt[i];
f[i]=1;
}
}
tat[i] = ct[i] - at[i] ;
wt[i] = tat[i] - bt[i] ;
avg_wt += wt[i] ;
avg_tat += tat[i] ;
}
System.out.println("Pid\tAT\tBT\tPRIO\tCT\tTAT\tWT\n");
for(i=0;i<n;i++)
{
System.out.println(pid[i] +"\t"+ at[i]+"\t"+bt[i] +"\t"+prio[i] +"\t"+ ct[i] +"\t"+ tat[i] +"\t"+ wt[i]);
}
System.out.print("\n Average Wait Time : "+avg_wt/n);
System.out.print("\n Average Turn Around Time : "+avg_tat/n);
sc.close();
}
}