0% found this document useful (0 votes)
11 views3 pages

Priority Scehduling Algorithm

Help to schedule

Uploaded by

Shoaib akhter
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)
11 views3 pages

Priority Scehduling Algorithm

Help to schedule

Uploaded by

Shoaib akhter
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/ 3

#!

/bin/bash

# Initialize process data

pids=("P1" "P2" "P3" "P4")

burst_time=(10 5 8 6)

priority=(2 1 4 3)

# Function to sort processes by priority

sort_by_priority() {

for ((i = 0; i < ${#pids[@]}; i++)); do

for ((j = i + 1; j < ${#pids[@]}; j++)); do

if [ ${priority[i]} -gt ${priority[j]} ]; then

# Swap priorities

temp=${priority[i]}

priority[i]=${priority[j]}

priority[j]=$temp

# Swap burst times

temp=${burst_time[i]}

burst_time[i]=${burst_time[j]}

burst_time[j]=$temp

# Swap process IDs

temp=${pids[i]}

pids[i]=${pids[j]}

pids[j]=$temp

fi

done

done

}
# Function to calculate waiting time for each process

calculate_waiting_time() {

waiting_time[0]=0

for ((i = 1; i < ${#pids[@]}; i++)); do

waiting_time[i]=0

for ((j = 0; j < i; j++)); do

waiting_time[i]=$((waiting_time[i] + burst_time[j]))

done

done

# Function to calculate turnaround time for each process

calculate_turnaround_time() {

for ((i = 0; i < ${#pids[@]}; i++)); do

turnaround_time[i]=$((burst_time[i] + waiting_time[i]))

done

# Function to print the results

print_results() {

echo "Process ID | Burst Time | Priority | Waiting Time | Turnaround Time"

for ((i = 0; i < ${#pids[@]}; i++)); do

echo "${pids[i]} | ${burst_time[i]} | ${priority[i]} | ${waiting_time[i]} | ${turnaround_time[i]}"

done

# Call the functions

sort_by_priority

calculate_waiting_time
calculate_turnaround_time

print_results

You might also like