Loading…
OS · CPU scheduling · Program 31 · In the record
Write A Program in shell script to implement the round robin scheduling Algorithm
1#!/bin/bash2 3echo "Enter number of processes:"4read n5 6echo "Enter burst time of each process:"7for (( i = 0; i < n; i++ ))8do9 read bt[i]10 rem[i]=${bt[i]}11done12 13echo "Enter time quantum:"14read q15 16time=017left=$n18gantt="0"19 20# Keep cycling through the ready queue until every process is done21while (( left > 0 ))22do23 for (( i = 0; i < n; i++ ))24 do25 if (( rem[i] > 0 ))26 then27 if (( rem[i] > q ))28 then29 time=$((time + q))30 rem[i]=$((rem[i] - q))31 else32 time=$((time + rem[i]))33 rem[i]=034 ct[i]=$time35 left=$((left - 1))36 fi37 gantt="$gantt | P$((i + 1)) | $time"38 fi39 done40done41 42echo43echo "Gantt chart: $gantt"44echo45 46total_wt=047total_tat=048printf "%-10s%-8s%-8s%s\n" "Process" "BT" "WT" "TAT"49for (( i = 0; i < n; i++ ))50do51 tat[i]=${ct[i]}52 wt[i]=$((tat[i] - bt[i]))53 total_wt=$((total_wt + wt[i]))54 total_tat=$((total_tat + tat[i]))55 printf "%-10s%-8s%-8s%s\n" "P$((i + 1))" "${bt[i]}" "${wt[i]}" "${tat[i]}"56done57 58echo59echo "Average Waiting Time = $(echo "scale=2; $total_wt / $n" | bc)"60echo "Average Turnaround Time = $(echo "scale=2; $total_tat / $n" | bc)"3 ⏎ 5 ⏎ 3 ⏎ 8 ⏎ 4Enter number of processes: 3 Enter burst time of each process: 5 3 8 Enter time quantum: 4 Gantt chart: 0 | P1 | 4 | P2 | 7 | P3 | 11 | P1 | 12 | P3 | 16 Process BT WT TAT P1 5 7 12 P2 3 4 7 P3 8 8 16 Average Waiting Time = 6.33 Average Turnaround Time = 11.66
Run program 31 in your browser
bash · just-bash. Change the code or the input, run it, break it, and see what happens.