Member-only story
Harnessing All CPU Cores for Parallel Processing with Bash Script
3 min readJan 12, 2025
Utilizing All CPU Cores with Parallel Processing
Introduction
Modern processors are equipped with multiple CPU cores, allowing tasks to be executed in parallel. This capability can dramatically reduce execution time for tasks that are computationally intensive. In this guide, we’ll demonstrate how to utilize all 5 CPU cores for parallel processing using a Bash script. Additionally, we’ll monitor CPU usage in real time to ensure optimal utilization.
Code
Below is the Bash script that performs parallel processing:
#!/bin/bash
# Function to simulate a CPU-intensive task
task() {
echo "Task $1 started on CPU core $(($1 % 5 + 1))"
sleep $((RANDOM % 5 + 1)) # Simulate a random task taking 1-5 seconds
echo "Task $1 completed"
}
# Export the task function so that it's accessible by parallel processes
export -f task
# Number of CPU cores to utilize
CPU_CORES=5
# Number of tasks to simulate
NUM_TASKS=20
# Run tasks in parallel using xargs
# -P specifies the number of parallel processes
# -n1 ensures one argument is passed to each instance of the task function
seq $NUM_TASKS | xargs -n1 -P$CPU_CORES -I{} bash -c 'task "$@"' _ {}
# Output to signify script completion
echo "All tasks completed."
# Script to monitor CPU…