Xara
Xara

Reputation: 9098

Executing multiple processes of c++ program

I have a C++ program. I am executing it on LInux. I want to execute this multiple instances of this program with different arguments. Eg:

./exeutableProgram file.txt
./exeutableProgram file2.txt
./exeutableProgram file3.txt

In other words, I want to create multiple processes such that each process run on different processor.

How can I achieve this task? Do I need to make some program using fork()? or I need to write some shell script? Please provide some guidance in this regard.

Upvotes: 0

Views: 375

Answers (2)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33854

You could write a bash script to do this:

for var in "$@" <-- loops over all of the arguments and sets them to var.
do
    /path/to/executableProgram $var & <-- executes the program with current var 
                                          as argument, & means background process.
done

The & will background the process and they should be allocated to different cores by your operating system.

You could then call with:

./Script file*.txt  <-- '*' is the wildcard character meaning all files with 
                         the signature file??.txt (file1.txt, file2.txt etc) will
                         all become arguments. 

Upvotes: 3

jxh
jxh

Reputation: 70382

If you install the util-linux package on your Linux distribution, you can use the taskset command to start your process on a specific CPU. To start your program on core 0 and then core 5:

$ taskset 0x1 ./executableProgram file.txt
$ taskset 0x20 ./executableProgram file2.txt

Upvotes: 1

Related Questions