-
Notifications
You must be signed in to change notification settings - Fork 8
/
fcfs.sh
138 lines (126 loc) · 3.04 KB
/
fcfs.sh
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
# Bash script to implement first come first served CPU scheduling algorithm.
sort(){
for ((i = 0; i<$n; i++))
do
for((j = 0; j<`expr $n - $i - 1`; j++))
do
if [ ${arrival_time[j]} -gt ${arrival_time[$((j+1))]} ]
then
# swap
temp=${arrival_time[j]}
arrival_time[$j]=${arrival_time[$((j+1))]}
arrival_time[$((j+1))]=$temp
temp=${burst_time[j]}
burst_time[$j]=${burst_time[$((j+1))]}
burst_time[$((j+1))]=$temp
temp=${pid[j]}
pid[$j]=${pid[$((j+1))]}
pid[$((j+1))]=$temp
elif [ ${arrival_time[j]} -eq ${arrival_time[$((j+1))]} ]
then
if [ ${pid[j]} -eq ${pid[$((j+1))]} ]
then
temp=${arrival_time[j]}
arrival_time[$j]=${arrival_time[$((j+1))]}
arrival_time[$((j+1))]=$temp
temp=${burst_time[j]}
burst_time[$j]=${burst_time[$((j+1))]}
burst_time[$((j+1))]=$temp
temp=${pid[j]}
pid[$j]=${pid[$((j+1))]}
pid[$((j+1))]=$temp
fi
fi
done
done
}
border(){
z=121
for ((i=0; i<$z; i++))
do
echo -n "-"
done
echo ""
}
findWaitingTime(){
service_time[0]=0
waiting_time[0]=0
for ((i=1; i<$n; i++))
do
z=1
y=`expr $i - $z`
service_time[$i]=`expr ${service_time[$y]} + ${burst_time[$y]} `
waiting_time[$i]=`expr ${service_time[$i]} - ${arrival_time[$i]}`
if [ ${waiting_time[$i]} -lt 0 ]
then
waiting_time[$i]=0
fi
done
}
findTurnAroundTime(){
for ((i=0; i<$n; i++))
do
tat[$i]=`expr ${waiting_time[$i]} + ${burst_time[$i]}`
done
}
findAverageTime(){
sort
findWaitingTime
findTurnAroundTime
total_wt=0
total_tat=0
border
printf "|%-18s|%-20s|%-18s|%-20s|%-18s|%-20s|\n" "Process Id" "Burst time" "Arrival time" "Waiting time" "Turn around time" "Completion time"
border
for ((i=0; i<$n; i++))
do
total_wt=`expr $total_wt + ${waiting_time[$i]}`
total_tat=`expr ${tat[$i]} + $total_tat`
completion_time=`expr ${arrival_time[$i]} + ${tat[$i]}`
printf "|%-18s|%-20s|%-18s|%-20s|%-18s|%-20s|\n" ${pid[$i]} ${burst_time[$i]} ${arrival_time[$i]} ${waiting_time[$i]} ${tat[$i]} $completion_time
#echo "${burst_time[$i]} ${arrival_time[$i]} ${waiting_time[$i]} ${tat[$i]} $completion_time"
done
border
#avgwt=`echo "scale=3; $total_wt / $n" | bc`
echo -n "Average waiting time ="
printf %.3f\\n "$(($total_wt / $n))"
#avgtat=`echo "scale=3; $total_tat / $n" | bc`
echo -n "Average turn around time ="
printf %.3f\\n "$(($total_tat / $n))"
for ((i=0; i<8*n+n+1; i++))
do
echo -n "-"
done
echo ""
for ((i=0; i<$n; i++))
do
echo -n "| "
echo -n "P${pid[$i]}"
echo -n " "
done
echo "|"
for ((i=0; i<8*n+n+1; i++))
do
echo -n "-"
done
echo ""
echo -n "0 "
for ((i=0; i<$n; i++))
do
echo -n "`expr ${arrival_time[$i]} + ${tat[$i]}`"
echo -n " "
done
echo ""
}
echo -n "Enter the number of processes: "
read n
for ((i=0; i<$n; i++))
do
echo -n "Enter Process Id: "
read pid[$i]
echo -n "Enter arrival time: "
read arrival_time[$i]
echo -n "Enter burst time: "
read burst_time[$i]
done
findAverageTime