-
Notifications
You must be signed in to change notification settings - Fork 0
/
First-In-First-Out.sh
48 lines (37 loc) · 1.09 KB
/
First-In-First-Out.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
#!/bin/bash
# Define the Process structure
declare -A Process
# Function to initialize a Process
initialize_process() {
Process[$1]="pid:$2,AT:$3,BT:$4"
}
# Function to execute FIFO algorithm
FIFO() {
local processes=("$@")
local TWT=0
local TAT=0
for process in "${processes[@]}"; do
local pid=$(echo "$process" | cut -d',' -f1 | cut -d':' -f2)
local AT=$(echo "$process" | cut -d',' -f2 | cut -d':' -f2)
local BT=$(echo "$process" | cut -d',' -f3 | cut -d':' -f2)
if [ "${#queue[@]}" -ne 0 ]; then
TAT=$((TAT + ${queue[0]##*,BT:}))
fi
queue+=("$process")
echo "Process $pid is being executed."
TWT=$((TWT + TAT - AT))
unset queue[0]
done
local avgWT=$(awk "BEGIN {print $TWT / ${#processes[@]}}")
local avgTAT=$(awk "BEGIN {print $TAT / ${#processes[@]}}")
echo "Average Waiting Time: $avgWT"
echo "Average Turnaround Time: $avgTAT"
}
# Main function
main() {
initialize_process 1 0 3
initialize_process 2 2 5
initialize_process 3 4 2
FIFO "${Process[@]}"
}
main