Is there a simple way to run multiple instances of the same C++ program on Linux?

I have a C++ program I’d like to run in parallel on a Linux machine, with each instance being a completely independent process that doesn’t communicate with the others. Is there a straightforward way to do the following:

  1. Count the number of CPU cores on the current machine.
  2. For number of CPU cores, run an instance of the program, assigning each instance to one core.
  3. Periodically check if the instances are still running, and restart any if necessary.
Asked By: Quavo

||

Count the number of CPU cores on the current machine.

There are a variety of special files and commands that provides this information, and there are also ways to do this programatically. The last link there is probably the best option, since it takes into account CPU affinity settings.

For number of CPU cores, run an instance of the program, assigning each instance to one core.

That sounds like a loop around fork and exec.

Periodically check if the instances are still running, and restart any if necessary.

You can use the wait system call to see if any of your child processes have exited, and if so, start a new instance.


The above solutions would allow you meet your requirements by writing some additional C/C++ code. You can apply the same principles in a shell script. The following scripts runs the terribly interesting sleep command once per core, ensuring that as commands exit new ones are started to keep the number of running sleep commands constant:

#!/bin/bash

numprocs=$(nproc)
echo "There are $numprocs cores available"

while true; do
    sleep $(( RANDOM % 10 )) &
    (( running++ ))
    echo "running=$running"

    if (( running >= $numprocs )); then
        wait -n -p pid
        (( running-- ))
        echo "$pid stopped"
    fi
done

You could do something like this, replacing the sleep command with whatever command you’re trying to run.

Answered By: larsks

yes 3 | parallel sleep

This will indefinitely run as many sleep 3 as there are processors.

Or in the spirit of another answer but simpler:

for ((i=$(nproc);i>0;i--)) ; do
  while true ; do
    sleep $(( 1+RANDOM%10 ))
  done &
done
Answered By: Thibault LE PAUL
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.