FCFS

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Write a unix program to implement the SJF Scheduling Algorith

#!/bin/bash

# Function to calculate waiting time and turnaround time


calculate_times() {
local -n arrival_times=$1
local -n burst_times=$2
local -n wait_times=$3
local -n turnaround_times=$4

local n=${#arrival_times[@]}
local total_waiting_time=0
local total_turnaround_time=0

# Calculate waiting time for the first process


wait_times[0]=0
turnaround_times[0]=${burst_times[0]}

# Calculate waiting time for the subsequent processes


for ((i=1; i<n; i++)); do
wait_times[$i]=$((turnaround_times[$((i-1))] - arrival_times[$i]))
if ((wait_times[$i] < 0)); then
wait_times[$i]=0
fi
turnaround_times[$i]=$((burst_times[$i] + wait_times[$i]))
done

# Calculate total waiting time and turnaround time


for ((i=0; i<n; i++)); do
total_waiting_time=$((total_waiting_time + wait_times[$i]))
total_turnaround_time=$((total_turnaround_time + turnaround_times[$i]))
done

# Calculate average waiting time and turnaround time


local avg_waiting_time=$(echo "scale=2; $total_waiting_time / $n" | bc)
local avg_turnaround_time=$(echo "scale=2; $total_turnaround_time / $n" | bc)

echo "Average Waiting Time: $avg_waiting_time"


echo "Average Turnaround Time: $avg_turnaround_time"
}

# Main program
main() {
# Input the number of processes
read -p "Enter the number of processes: " num_processes

declare -a arrival_times
declare -a burst_times
declare -a wait_times
declare -a turnaround_times

# Input arrival time and burst time for each process


for ((i=0; i<num_processes; i++)); do
read -p "Enter arrival time for process $((i+1)): " arrival_times[$i]
read -p "Enter burst time for process $((i+1)): " burst_times[$i]
done
# Calculate waiting time and turnaround time using FCFS
calculate_times arrival_times burst_times wait_times turnaround_times
}

# Run the main program


main

You might also like