0% found this document useful (0 votes)
6 views7 pages

Assignment - 4 Nikhil Kumar Agrawal (2021btech077)

Uploaded by

yaminisharma650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Assignment - 4 Nikhil Kumar Agrawal (2021btech077)

Uploaded by

yaminisharma650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JK Lakshmipat University, Jaipur

Institute of Engineering and Technology


Department of Computer Science Engineering

Assignment -4

Course code:
CS 1108

Submitted by:
Nikhil Kumar Agrawal
(2021Btech077)
WAP to Implement Priority (Preemptive) and Round Robin CPU
Scheduling algorithm.
User Input:
i. Number of jobs, N
ii. Arrival time of each job, AT
iii. CPU/ Burst time of each job, BT
iv. Priority of each job

Output:
i. Print the job sequence
ii. Completion time of each job, CT
iii. Waiting Time of each job, WT (WT = TAT - BT)
iv. Average Waiting Time, AWT (AWT = (WT1 + WT2 + ........ + WTN)/N)
v. Turn Around Time of each job, TAT (TAT = CT – AT or TAT = WT + BT)
vi. Average Turn Around Time, ATAT (ATAT = (TAT1 + TAT2 + ........ + TATN)/N)

Code in Linux for Priority ( Preemptive ) and Round Robin:


#!/bin/bash
# Function to calculate Average
calculate_average() {
local sum=0
for num in "${@}"; do
((sum += num))
done
echo "scale=2; $sum / $#" | bc
}
# Priority Scheduling (Preemptive)
priority_preemptive() {
local N=$1
local AT=("${@:2:$N}")
local BT=("${@:($N+2):$N}")
local P=("${@:($N*2+2):$N}")
local completed=0
local current_time=0
declare -a RT CT WT TAT
for ((i = 0; i < N; i++)); do
RT[$i]=${BT[$i]}
done
while ((completed < N)); do
local highest_priority=-1
local highest_priority_index=-1
for ((i = 0; i < N; i++)); do
if ((AT[$i] <= current_time && RT[$i] > 0)); then
if ((highest_priority == -1 || P[$i] < highest_priority)); then
highest_priority=${P[$i]}
highest_priority_index=$i
fi
fi
done
if ((highest_priority_index == -1)); then
((current_time++))
else
((RT[$highest_priority_index]--))
current_time=$((current_time + 1))
if ((RT[$highest_priority_index] == 0)); then
CT[$highest_priority_index]=$current_time
TAT[$highest_priority_index]=$((CT[$highest_priority_index] - AT[$highest_priority_index]))
WT[$highest_priority_index]=$((TAT[$highest_priority_index] - BT[$highest_priority_index]))
completed=$((completed + 1))
fi
fi
done
# Output results for Priority (Preemptive)
echo "Priority Scheduling (Preemptive):"
echo "Job | Arrival Time | Burst Time | Completion Time | Waiting Time | Turnaround Time"
echo "------|--------------|------------|-----------------|--------------|----------------"
for ((i = 0; i < N; i++)); do
printf "Job %2d| %12d | %10d | %15d | %12d | %15d\n" $((i + 1)) ${AT[$i]} ${BT[$i]} ${CT[$i]}
${WT[$i]} ${TAT[$i]}
done
AWT=$(calculate_average "${WT[@]}")
echo "Average Waiting Time (AWT): $AWT"
ATAT=$(calculate_average "${TAT[@]}")
echo "Average Turnaround Time (ATAT): $ATAT"
}
# Round Robin Scheduling
round_robin() {
local N=$1
local AT=("${@:2:$N}")
local BT=("${@:($N+2):$N}")
local time_quantum
# Input time quantum
read -p "Enter Time Quantum: " time_quantum
declare -a CT WT TAT RT
for ((i = 0; i < N; i++)); do
RT[$i]=${BT[$i]}
done
local current_time=0
local completed=0
local queue=()
while ((completed < N)); do
for ((i = 0; i < N; i++)); do
if ((AT[$i] <= current_time && RT[$i] > 0)); then
if ((RT[$i] <= time_quantum)); then
current_time=$((current_time + RT[$i]))
CT[$i]=$current_time
RT[$i]=0
completed=$((completed + 1))
else
current_time=$((current_time + time_quantum))
RT[$i]=$((RT[$i] - time_quantum))
queue+=($i)
fi
fi
done
if [[ ${#queue[@]} -ne 0 ]]; then
current_index=${queue[0]}
unset 'queue[0]'
queue=("${queue[@]}")
queue+=($current_index)
else
((current_time++))
fi
done
for ((i = 0; i < N; i++)); do
TAT[$i]=$((CT[$i] - AT[$i]))
WT[$i]=$((TAT[$i] - BT[$i]))
done
# Output results for Round Robin
echo "Round Robin Scheduling:"
echo "Job | Arrival Time | Burst Time | Completion Time | Waiting Time | Turnaround Time"
echo "------|--------------|------------|-----------------|--------------|----------------"
for ((i = 0; i < N; i++)); do
printf "Job %2d| %12d | %10d | %15d | %12d | %15d\n" $((i + 1)) ${AT[$i]} ${BT[$i]} ${CT[$i]}
${WT[$i]} ${TAT[$i]}
done
AWT=$(calculate_average "${WT[@]}")
echo "Average Waiting Time (AWT): $AWT"
ATAT=$(calculate_average "${TAT[@]}")
echo "Average Turnaround Time (ATAT): $ATAT"
}
# Input
read -p "Enter the number of jobs, N: " N
# Initialize arrays for job data
declare -a AT
declare -a BT
declare -a P
# Input arrival time, burst time, and priority for each job
for ((i = 0; i < N; i++)); do
read -p "Enter Arrival Time for Job $((i + 1)): " AT[$i]
read -p "Enter Burst Time for Job $((i + 1)): " BT[$i]
read -p "Enter Priority for Job $((i + 1)): " P[$i]
done
# Call Priority Scheduling (Preemptive) function
priority_preemptive "$N" "${AT[@]}" "${BT[@]}" "${P[@]}"
# Call Round Robin Scheduling function
round_robin "$N" "${AT[@]}" "${BT[@]}"
Output:

You might also like