如何在c ++中并行执行系统命令

时间:2014-05-11 16:33:35

标签: c++ parallel-processing system executable

通常,当我想从c ++代码运行可执行文件时。我只是使用代码:

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

现在,我想并行运行可执行文件。我用2个线程。第一个线程将调用:

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

第二个主题将调用:

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

然而,它并没有像我预期的那样并行运行。

有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:4)

您可以运行多个命令,如下所示:

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

这样两者都会并行运行,并且你的程序不需要多线程。

答案 1 :(得分:1)

  • 对于Windows:CreateProcess(请参阅MSDN)函数。
  • 对于* NIX:首先,使用fork spwan一个孩子,然后用execXX替换子代码(execl,execle,execlp,execv,execvp)。
相关问题