import
java.util.*;
class
GFG
{
static
class
process {
int
processno;
int
AT;
int
BT;
int
BTbackup;
int
WT;
int
TAT;
int
CT;
}
static
process[] p =
new
process[
4
];
static
int
totaltime =
0
;
static
int
prefinaltotal =
0
;
static
int
findlargest(
int
at)
{
int
max =
0
, i;
for
(i =
0
; i <
4
; i++) {
if
(p[i].AT <= at) {
if
(p[i].BT > p[max].BT)
max = i;
}
}
return
max;
}
static
void
findCT()
{
int
index;
int
flag =
0
;
int
i = p[
0
].AT;
while
(
true
) {
if
(i <=
4
) {
index = findlargest(i);
}
else
index = findlargest(
4
);
System.out.print(
"Process executing at time "
+ totaltime +
" is: P"
+ (index +
1
) +
"\t"
);
p[index].BT -=
1
;
totaltime +=
1
;
i++;
if
(p[index].BT ==
0
) {
p[index].CT = totaltime;
System.out.println(
" Process P"
+ p[index].processno
+
" is completed at "
+ totaltime);
}
System.out.println();
if
(totaltime == prefinaltotal)
break
;
}
}
public
static
void
main(String[] args)
{
int
i;
for
(i =
0
; i <
4
; i++) {
p[i] =
new
process();
p[i].processno = i +
1
;
}
for
(i =
0
; i <
4
; i++)
{
p[i].AT = i +
1
;
}
for
(i =
0
; i <
4
; i++) {
p[i].BT =
2
* (i +
1
);
p[i].BTbackup = p[i].BT;
prefinaltotal += p[i].BT;
}
System.out.print(
"PNo\tAT\tBT\n"
);
for
(i =
0
; i <
4
; i++) {
System.out.print(p[i].processno +
"\t"
);
System.out.print(p[i].AT +
"\t"
);
System.out.println(p[i].BT +
"\t"
);
}
System.out.println();
Arrays.sort(p, (process p1, process p2) -> {
return
p1.AT - p2.AT;
});
totaltime += p[
0
].AT;
prefinaltotal += p[
0
].AT;
findCT();
int
totalWT =
0
;
int
totalTAT =
0
;
for
(i =
0
; i <
4
; i++) {
p[i].TAT = p[i].CT - p[i].AT;
p[i].WT = p[i].TAT - p[i].BTbackup;
totalWT += p[i].WT;
totalTAT += p[i].TAT;
}
System.out.print(
"After execution of all processes ... \n"
);
System.out.print(
"PNo\tAT\tBT\tCT\tTAT\tWT\n"
);
for
(i =
0
; i <
4
; i++) {
System.out.print(p[i].processno +
"\t"
);
System.out.print(p[i].AT +
"\t"
);
System.out.print(p[i].BTbackup +
"\t"
);
System.out.print(p[i].CT +
"\t"
);
System.out.print(p[i].TAT +
"\t"
);
System.out.println(p[i].WT +
"\t"
);
}
System.out.println();
System.out.println(
"Total TAT = "
+ totalTAT);
System.out.println(
"Average TAT = "
+ (totalTAT /
4.0
));
System.out.println(
"Total WT = "
+ totalWT);
System.out.println(
"Average WT = "
+ totalWT /
4.0
);
}
}