Skip to content

Instantly share code, notes, and snippets.

@dakanji
Created May 11, 2021 07:06
Show Gist options
  • Save dakanji/d22dc61690cfc987bd118a93ed65cafb to your computer and use it in GitHub Desktop.
Save dakanji/d22dc61690cfc987bd118a93ed65cafb to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
###
# RaceConditionSim.sh
# A script to simulate a Race Condition
#
# Copyright (c) 2021 Dayo Akanji
# MIT License
###
# Provide custom colours
msg_base() {
echo -e "\033[0;36m$1\033[0m"
}
msg_info() {
echo -e "\033[0;33m$1\033[0m"
}
msg_status() {
echo -e "\033[0;32m$1\033[0m"
}
msg_error() {
echo -e "\033[0;31m$1\033[0m"
}
## ERROR HANDLER ##
runErr() { # $1: message
# Declare Local Variables
local errMessage
errMessage="${1:-Runtime Error ... Exiting}"
echo ''
msg_error "${errMessage}"
echo ''
echo ''
exit 1
}
trap runErr ERR
# Run the Simulation
clear
msg_info '## Simple Race Condition Simulator ##'
msg_info '-------------------------------------'
LoopCount=0
for (( ; ; )) ; do
LoopCount=$(( LoopCount + 1 ))
OUR_RAND_A=$(( RANDOM % 3 ))
OUR_RAND_B=$(( RANDOM % 3 ))
OUR_RAND_B=$(( OUR_RAND_B + 1 ))
if (( LoopCount < 10 )) ; then
LoopTally="0${LoopCount}"
else
LoopTally=${LoopCount}
fi
if ((OUR_RAND_A > OUR_RAND_B)) ; then
msg_error "ATTEMPT ${LoopTally}: 'Kernel Panic'"
echo ''
msg_info "Kernel Panic at Attempt ${LoopCount}"
echo ''
break
else
msg_status "ATTEMPT ${LoopTally}: 'Booted'"
fi
if (( LoopCount > 99 )) ; then
echo ''
msg_info "Halted on ${LoopCount} Successful Boots"
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment