0% found this document useful (0 votes)
33 views10 pages

Cpu Scheduling Bash Script Part 1

The script simulates non-preemptive shortest job first (SJF) scheduling by sorting processes based on their burst times, executing them in that order, and calculating their wait times and turnaround times. It then calculates the average wait time and total execution time.

Uploaded by

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

Cpu Scheduling Bash Script Part 1

The script simulates non-preemptive shortest job first (SJF) scheduling by sorting processes based on their burst times, executing them in that order, and calculating their wait times and turnaround times. It then calculates the average wait time and total execution time.

Uploaded by

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

FIRST COME FIRST SERVE BASH SCRIPT

#!/bin/bash waiting_time=$((current_time -
arrival_time))

# Define an associative array to store


process burst times # Accumulate total waiting time

declare -A burst_times=( total_waiting_time=$((total_waiting_time


+ waiting_time))
["P1"]=10

["P2"]=5
# Print process details
["P3"]=8
echo "Process: $process"
["P4"]=12
echo "Burst Time: $burst_time ms"
)
echo "Waiting Time: $waiting_time ms"

echo "------------------------"
# Initialize variables
done
current_time=0

total_waiting_time=0
# Calculate average waiting time

num_processes=${#burst_times[@]}
# Iterate over processes in the order they
were received average_waiting_time=$
((total_waiting_time / num_processes))
for process in "${!burst_times[@]}"; do

burst_time=${burst_times[$process]}
# Print average waiting time
arrival_time=$current_time
echo "Average Waiting Time:
$average_waiting_time ms"
# Execute the process

sleep "$burst_time"

# Update current time and calculate


waiting time

current_time=$((current_time +
burst_time))
Certainly! Let's go through the script line by line to explain each part:

#!/bin/bash

This line is called a shebang and is used to specify the interpreter for the script, in this case,
bash.

declare -A burst_times=( [ "P1" ]=10 [ "P2" ]=5 [ "P3" ]=8 [ "P4" ]=12 )

Here, we declare an associative array called burst_times to store the burst times of the
processes. Each key-value pair represents a process name (e.g., "P1", "P2") and its
corresponding burst time.

current_time=0 total_waiting_time=0

These two lines initialize the variables current_time and total_waiting_time to zero.
current_time keeps track of the current time during execution, and total_waiting_time
accumulates the waiting time of all processes.

for process in "${!burst_times[@]}" ; do burst_time= ${burst_times[$process]}


arrival_time= $current_time

This is a for loop that iterates over the keys of the burst_times array. It assigns each process
name to the variable process in each iteration. Inside the loop, we retrieve the corresponding
burst time from the array using ${burst_times[$process]} and store it in the variable
burst_time. We also store the current time in the variable arrival_time.

sleep "$burst_time"

This line simulates the execution of a process by using the sleep command. The script pauses
for the duration of the burst_time, effectively simulating the processing time.

current_time=$((current_time + burst_time)) waiting_time=$((current_time - arrival_time))

After the process execution, we update the current_time variable by adding the burst_time.
This gives us the new current time. We then calculate the waiting time by subtracting the
arrival_time from the current_time.

total_waiting_time=$((total_waiting_time + waiting_time))

This line accumulates the waiting time of each process by adding the waiting_time to the
total_waiting_time.
echo "Process: $process" echo "Burst Time: $burst_time ms" echo "Waiting Time:
$waiting_time ms" echo "------------------------"

Here, we print the details of each process, including the process name, burst time, and waiting
time. The dashes at the end create a visual separator between each process.

num_processes= ${#burst_times[@]} average_waiting_time=$((total_waiting_time /


num_processes))

These lines calculate the number of processes (num_processes) by getting the count of
elements in the burst_times array. Then, the average_waiting_time is calculated by
dividing the total_waiting_time by the num_processes.

echo "Average Waiting Time: $average_waiting_time ms"

Finally, we print the average waiting time.

That's a line-by-line explanation of the script. The script essentially iterates over the processes,
simulates their execution using the sleep command, calculates the waiting time for each
process, accumulates the total waiting time, and finally calculates and displays the average
waiting time.
SHORTEST JOB FIRST

NON PREEMPTIVE SJF

#!/bin/bash

#NON PREEMPTIVE SJF # Iterate over the sorted processes

# Define the process structure for process in "${sorted_processes[@]}"; do

declare -A processes=( burst_time=${processes[$process]}

["P1"]="10" wait_time=$((current_time))

["P2"]="5" current_time=$((current_time +
burst_time))
["P3"]="8"
turnaround_time=$((current_time))
["P4"]="3"

["P5"]="12"
echo "Process $process: Burst
)
Time=$burst_time, Wait Time=$wait_time,
Turnaround Time=$turnaround_time"

# Sort the processes based on their burst avg_wait_time=$((avg_wait_time +


times wait_time))

sorted_processes=($(for p in "${! total_time=$((total_time +


processes[@]}"; do echo "$p:$ turnaround_time))
{processes[$p]}"; done | sort -t: -k2 -n |
done
cut -d: -f1))

# Calculate average wait time and total


# Initialize variables
execution time
current_time=0
avg_wait_time=$(awk "BEGIN
total_time=0 {printf \"%.2f\", $avg_wait_time / $
{#sorted_processes[@]}}")
avg_wait_time=0
total_time=$((current_time))

echo "Process Execution Order:"


echo "-----------------------"
echo "-----------------------"
echo "Average Wait Time: $avg_wait_time"
echo "Total Execution Time: $total_time"

Certainly! Here's an explanation of the script line by line:

#!/bin/bash

This line specifies the interpreter for the script as /bin/bash.

declare -A processes=( [ "P1" ]= "10" [ "P2" ]= "5" [ "P3" ]= "8" [ "P4" ]= "3" [ "P5" ]= "12" )

This block declares an associative array processes, where the process IDs (e.g., "P1", "P2")
are the keys and the corresponding burst times are the values. You can modify this block to
include your own processes and burst times.

sorted_processes=($( for p in "${!processes[@]}" ; do echo "$p:${processes[$p]}" ; done |


sort -t: -k2 -n | cut -d: -f1))

This line creates a sorted array sorted_processes by iterating over the processes array,
extracting the process ID and burst time as "key:value" pairs, sorting them based on the burst
time, and extracting only the process IDs. The sorted array represents the order in which
processes will be executed.

current_time=0 total_time=0 avg_wait_time=0

These lines initialize variables current_time to keep track of the current time, total_time to
store the total execution time, and avg_wait_time to calculate the average wait time.

echo "Process Execution Order:" echo "-----------------------"

These lines simply print the headers for the process execution details.

for process in "${sorted_processes[@]}" ; do

This line starts a loop that iterates over each process ID in the sorted_processes array.

burst_time= ${processes[$process]} wait_time=$((current_time)) current_time=$


((current_time + burst_time)) turnaround_time=$((current_time))
These lines extract the burst time of the current process from the processes array, assign the
current time to the wait time, update the current time by adding the burst time, and calculate
the turnaround time.

echo "Process $process: Burst Time=$burst_time, Wait Time=$wait_time, Turnaround


Time=$turnaround_time"

This line prints the details of the current process, including the process ID, burst time, wait
time, and turnaround time.

avg_wait_time=$((avg_wait_time + wait_time)) total_time=$((total_time + turnaround_time))

These lines update the avg_wait_time and total_time variables by adding the wait time and
turnaround time of the current process, respectively.

done

This line marks the end of the loop.

avg_wait_time=$(awk "BEGIN {printf \"%.2f\", $avg_wait_time / ${#sorted_processes[@]}}" )


total_time=$((current_time))

These lines calculate the average wait time by dividing the avg_wait_time by the number of
processes in the sorted_processes array. The awk command is used to round the result to
two decimal places. The total execution time is stored in the total_time variable.

echo "-----------------------" echo "Average Wait Time: $avg_wait_time" echo "Total Execution
Time: $total_time"

These lines simply print the average wait time and the total execution time.

That's a line-by-line explanation of the script. If you have any specific questions about any part
of the script, feel free to ask!
PREEMPTIVE SJF

#!/bin/bash # Continue executing until all processes are


completed

while [[ ${#completed_processes[@]} -lt $


# Define the process structure
{#sorted_processes[@]} ]]; do
declare -A processes=(
# Iterate over the sorted processes
["P1"]="0:10"
for process in "${sorted_processes[@]}";
["P2"]="2:5" do

["P3"]="1:8" # Check if the process is already


completed
["P4"]="3:3"
if [[ " ${completed_processes[*]} "
["P5"]="4:12" == *" $process "* ]]; then
) continue

fi
# Sort the processes based on their arrival
time and burst time
# Get the arrival time and burst time
sorted_processes=($(for p in "${! for the process
processes[@]}"; do echo "$p:$
{processes[$p]}"; done | sort -t: -k2 -k3 -n arrival_time=$(echo "$
| cut -d: -f1)) {processes[$process]}" | cut -d: -f1)

burst_time=$(echo "$
{processes[$process]}" | cut -d: -f2)
# Initialize variables

current_time=0
# Check if the process has arrived
quantum=2
if [[ $arrival_time -gt
completed_processes=() $current_time ]]; then
total_wait_time=0 continue

fi
echo "Process Execution Order:"

echo "-----------------------" # Execute the process for the


quantum time or until completion
if [[ $burst_time -gt $quantum ]]; average_wait_time=$(awk "BEGIN
then {printf \"%.2f\", $total_wait_time / $
{#sorted_processes[@]}}")
remaining_time=$((burst_time -
quantum))

echo "-----------------------"
processes[$process]="$arrival_time:
echo "Average Waiting Time:
$remaining_time"
$average_wait_time"
current_time=$((current_time +
quantum))

echo "Process $process: Burst


Time=$burst_time, Wait Time=$
((current_time - arrival_time - burst_time)),
(preempted)"

else

remaining_time=0

processes[$process]="$arrival_time:
$remaining_time"

current_time=$((current_time +
burst_time))

completed_processes+=("$process")

echo "Process $process: Burst


Time=$burst_time, Wait Time=$
((current_time - arrival_time - burst_time)),
Turnaround Time=$((current_time -
arrival_time))"

total_wait_time=$((total_wait_time
+ current_time - arrival_time - burst_time))

fi

done

done

# Calculate average waiting time


Certainly! Here's an explanation of each line in the script:

#!/bin/bash

This line specifies the interpreter for the script as /bin/bash.

declare -A processes=( [ "P1" ]= "0:10" [ "P2" ]= "2:5" [ "P3" ]= "1:8" [ "P4" ]= "3:3"
[ "P5" ]= "4:12" )

This block declares an associative array processes, where the process IDs (e.g., "P1", "P2")
are the keys and the corresponding arrival time and burst time are the values. You can modify
this block to include your own processes, arrival times, and burst times.

sorted_processes=($( for p in "${!processes[@]}" ; do echo "$p:${processes[$p]}" ; done |


sort -t: -k2 -k3 -n | cut -d: -f1))

This line creates a sorted array sorted_processes by iterating over the processes array,
extracting the process ID, arrival time, and burst time as "key:value" pairs, sorting them based
on the arrival time and burst time, and extracting only the process IDs. The sorted array
represents the order in which processes will be executed.

current_time=0 quantum=2 completed_processes=() total_wait_time=0

These lines initialize variables current_time to keep track of the current time, quantum as
the time quantum for each execution, completed_processes as an empty array to store the
process IDs of completed processes, and total_wait_time to calculate the total wait time.

echo "Process Execution Order:" echo "-----------------------"

These lines simply print the headers for the process execution details.

while [[ ${#completed_processes[@]} -lt ${#sorted_processes[@]} ]]; do

This line starts a loop that iterates until all processes are completed. It checks if the number of
elements in the completed_processes array is less than the total number of processes in the
sorted_processes array.

for process in "${sorted_processes[@]}" ; do

This line starts a loop that iterates over each process ID in the sorted_processes array.
if [[ " ${completed_processes[*]} " == * " $process " * ]]; then continue fi

These lines check if the current process is already completed by checking if the process ID is
present in the completed_processes array. If the process is completed, the loop continues to
the next iteration.

arrival_time=$( echo "${processes[$process]}" | cut -d: -f1) burst_time=$( echo "$


{processes[$process]}" | cut -d: -f2)

These lines extract the arrival time and burst time of the current process from the processes
array using the process ID. The cut command is used to split the value by ":" delimiter and
extract the required fields.

if [[ $arrival_time -gt $current_time ]]; then continue fi

This line checks if the process has arrived by comparing the arrival time with the current time.
If the arrival time is greater than the current time, the loop continues to the next iteration.

if [[ $burst_time -gt $quantum ]]; then remaining_time=$((burst_time - quantum))


processes[ $process ]= "$arrival_time:$remaining_time" current_time=$((current_time +
quantum)) echo "Process $process: Burst Time=$burst_time, Wait Time=$((current_time -

You might also like