diff --git a/course/content/operating-systems/linux/monitoring/cpu/.test/multithread-cpu.sh b/course/content/operating-systems/linux/monitoring/cpu/.test/multithread-cpu.sh new file mode 100644 index 0000000..b4ab130 --- /dev/null +++ b/course/content/operating-systems/linux/monitoring/cpu/.test/multithread-cpu.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Function to simulate CPU usage +simulate_cpu_usage() { + periodInSeconds=$1 + core=$2 + echo "Simulating CPU usage for $periodInSeconds seconds on core $core..." + end=$((SECONDS + $periodInSeconds)) + while [ $SECONDS -lt $end ]; do + # Perform a CPU-intensive task directly in the script + for i in {1..10000}; do + : # No-op command, just to keep the CPU busy + done + done + echo "CPU usage simulation complete on core $core." +} + +# Number of CPU cores +num_cores=$(nproc) + +# Duration for the simulation +duration=500 + +# Run the function in the background for each core +for core in $(seq 1 $num_cores); do + simulate_cpu_usage $duration $core & +done + +# Wait for all background jobs to complete +wait \ No newline at end of file diff --git a/course/content/operating-systems/linux/monitoring/cpu/.test/singlethread-cpu.sh b/course/content/operating-systems/linux/monitoring/cpu/.test/singlethread-cpu.sh new file mode 100644 index 0000000..2d7a917 --- /dev/null +++ b/course/content/operating-systems/linux/monitoring/cpu/.test/singlethread-cpu.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Function to simulate CPU usage +simulate_cpu_usage() { + periodInSeconds=$1 + echo "Simulating CPU usage for $periodInSeconds seconds..." + end=$((SECONDS + $periodInSeconds)) + while [ $SECONDS -lt $end ]; do + # Perform a CPU-intensive task directly in the script + for i in {1..10000}; do + : # No-op command, just to keep the CPU busy + done + done + echo "CPU usage simulation complete." +} + +# Call the function with the provided duration +simulate_cpu_usage 500 + + +#ps -o pid,ppid,cmd --forest -p \ No newline at end of file diff --git a/course/content/operating-systems/linux/monitoring/cpu/README.md b/course/content/operating-systems/linux/monitoring/cpu/README.md index 5119444..36f45d8 100644 --- a/course/content/operating-systems/linux/monitoring/cpu/README.md +++ b/course/content/operating-systems/linux/monitoring/cpu/README.md @@ -115,13 +115,13 @@ We can take a look at two bash scripts that I have written that demonstrates sin #### example: single threaded code -If we execute `./singlethread-cpu.sh`, we will notice that only one core of the CPU is busy at a time.
+If we execute [./singlethread-cpu.sh](.test/singlethread-cpu.sh), we will notice that only one core of the CPU is busy at a time.
Now because we execute a loop, each iteration of that loop will run on a different core.
Bash itself is single threaded, so this script needs to be optimized if we want to make use of all available CPU cores.
#### example: multi threaded code -If we execute `./multithread-cpu.sh`, we will notice all CPU cores get busy.
+If we execute [./multithread-cpu.sh](.test/multithread-cpu.sh), we will notice all CPU cores get busy.
This is because in this script, we read the number of available cores with the `nproc` command.
Then we loop the number of available cores and execute our `simulate_cpu_usage()` function.
At this point it would technically still be single threaded, because it's still a loop and does not create more threads or processes.
To get around this we use a special character in bash called `&` at the end of our function.