Cpu Scheduling Bash Script Part 1
Cpu Scheduling Bash Script Part 1
#!/bin/bash waiting_time=$((current_time -
arrival_time))
["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"
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.
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.
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.
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.
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
#!/bin/bash
["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"
#!/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.
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.
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.
These lines simply print the headers for the process execution details.
This line starts a loop that iterates over each process ID in the sorted_processes array.
This line prints the details of the current process, including the process ID, burst time, wait
time, and 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
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
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 "-----------------------"
processes[$process]="$arrival_time:
echo "Average Waiting Time:
$remaining_time"
$average_wait_time"
current_time=$((current_time +
quantum))
else
remaining_time=0
processes[$process]="$arrival_time:
$remaining_time"
current_time=$((current_time +
burst_time))
completed_processes+=("$process")
total_wait_time=$((total_wait_time
+ current_time - arrival_time - burst_time))
fi
done
done
#!/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.
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.
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.
These lines simply print the headers for the process execution details.
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.
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.
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.
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.