Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute system command in parallel in c++

Normally when I want to run a executable from a c++ code. I just use the code:

system("path\to\the\executable param"); 

Now, I want to run the executable file in parallel. I use 2 threads. The first thread will call:

system("path\to\the\executable param1");

The second thread will call:

system("path\to\the\executable param2");

However it doesn't run in parallel as I expect.

Is there in way to solve this?

like image 215
Kelvin Tan Avatar asked Jun 18 '26 07:06

Kelvin Tan


2 Answers

You can run multiple commands as below:

system("path\\to\\the\\executable param1 &");
system("path\\to\\the\\executable param2");

This way both will run in parallel, and your program doesn't need to be multi-threaded for this.

like image 61
Mohit Jain Avatar answered Jun 20 '26 22:06

Mohit Jain


  • For windows: CreateProcess (see MSDN) function.
  • For *NIX: first, spwan a child with fork, then replace a child code with execXX (execl, execle, execlp, execv, execvp).
like image 22
StingX Avatar answered Jun 20 '26 23:06

StingX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!