Created
March 13, 2023 02:22
-
-
Save joelagnel/1533f5c63cc14c4489e463c60049f61c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function cleanup { | |
echo "Exiting script, killing child processes..." | |
kill $pid1 $pid2 | |
exit 1 | |
} | |
trap cleanup EXIT | |
while true; do | |
# Spawn first process with real-time priority and pin to CPU 2 | |
taskset -c 2 chrt -f 99 sh -c 'while true; do :; done' & | |
# Get the process ID of the first process | |
pid1=$! | |
# Set real-time priority for first process | |
chrt -f -p 99 $pid1 | |
# Spawn second process with real-time priority and pin to CPU 2 | |
taskset -c 2 chrt -f 99 sh -c 'while true; do :; done' & | |
# Get the process ID of the second process | |
pid2=$! | |
# Set real-time priority for second process | |
chrt -f -p 99 $pid2 | |
# Wait for a few seconds to let the processes run | |
sleep 5 | |
# Check if each process used at most 90% of CPU time | |
cpu_usage1=$(ps -p $pid1 -o %cpu | awk 'NR>1 {print $1}') | |
cpu_usage2=$(ps -p $pid2 -o %cpu | awk 'NR>1 {print $1}') | |
if (( $(echo "$cpu_usage1 <= 96.0" | bc -l) )) && (( $(echo "$cpu_usage2 <= 96.0" | bc -l) )); then | |
echo "Success: Process 1 CPU usage = $cpu_usage1%, Process 2 CPU usage = $cpu_usage2%" | |
else | |
echo "Error: Process 1 CPU usage = $cpu_usage1%, Process 2 CPU usage = $cpu_usage2%" | |
fi | |
# Kill both processes and start over | |
kill $pid1 $pid2 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment