#/bin/bash
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 ""
}
n=$(sed -e '1~2d' fcfs1.txt |awk '{ for (i=1; i<=NF; i++) RtoC[i]= (i in RtoC?RtoC[i] OFS :"") $i; } END{ for (i=1; i<=NF; i++) print RtoC[i] }'| awk '{print $1}' |wc -l)
for ((i=0; i<$n; i++))
do
pid[$i]=$(cat fcfs.txt | awk '{print $1}')
arrival_time[$i]=$(cat fcfs.txt | awk '{print $2}')
burst_time[$i]=$(cat fcfs.txt | awk '{print $3}')
done
findAverageTime
fcfs.txt content is like this:
1 15 10
2 17 12
if input file has only one process script working perfect, if more that one gives an error output when only one process in the input file
-------------------------------------------------------------------------------------------------------------------------
|Process Id |Burst time |Arrival time |Waiting time |Turn around time |Completion time |
-------------------------------------------------------------------------------------------------------------------------
|1 |5 |10 |0 |5 |15 |
-------------------------------------------------------------------------------------------------------------------------
Average waiting time =0.000
Average turn around time =5.000
----------
| P1 |
----------
0 15
https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process. – waltinator Jun 26 '22 at 00:41cat
s the entire input file and pipes it into awk to extract values (multiple times, once for each value it wants to extract). It doesn't even attempt to iterate over each line with a while-read loop. And before you go ahead and do that, I suggest you read Why is using a shell loop to process text considered bad practice?. BTW, I'm not saying "don't use shell" because I want to make things more difficult for you - quite the opposite, I want to point you in a direction where what you're doing will be trivially easy. – cas Jun 26 '22 at 10:10