Member-only story

Harnessing All CPU Cores for Parallel Processing with Bash Script

Kamalesh D
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 usage during execution
cpu_monitor() {
echo "Monitoring CPU usage. Press Ctrl+C to stop."
while true; do
echo "--- CPU Usage ---"
top -l 1 -n 0 | grep -E 'CPU usage'
sleep 2
done
}
cpu_monitor &

Working Explanation

  1. Task Simulation: The task function simulates a computationally intensive task by adding a delay of 1-5 seconds. Each task is assigned a CPU core based on its ID.
  2. Parallel Execution: The xargs command enables parallel execution. The -P option specifies the number of parallel processes (5 in this case), ensuring efficient use of all CPU cores.
  3. Dynamic Task Distribution: Tasks are distributed dynamically to the cores. The script processes 20 tasks in total, utilizing all cores simultaneously.
  4. CPU Monitoring: The cpu_monitor function uses macOS's top command to display real-time CPU usage, helping verify optimal core…

Kamalesh D
Kamalesh D

Written by Kamalesh D

DevOps and Cloud Enthusiast | Cyber Security | DevSecOps Engineer@FordMotors | https://dev.to/kamaleshseervi

No responses yet

Write a response