我想同时运行一个具有不同参数的命令,N次,就像一个for循环,其中所有循环同时运行
我知道通过在特定行的末尾使用 & 这样我们可以同时运行它们,但我想要一种运行 N 次的方法,可以是变体
./a &
./a &
./a &
我可以在特定的核心上运行每一个吗?或者我应该用 openMP 和 C 来做这样的事情?如果是,我如何使用 C++ 运行命令?
#paragma omp parallel
for(int i=0;i<n ; i++ )
{
/// ??? how can I run these command by c++
}
但即使它有效,该解决方案也仅在 (n< #core) :(
最佳答案
也许你可以使用 std::thread...
#include <iostream>
#include <thread>
#include <string>
void runcmd(std::string param)
{
// something like system(param.c_str());
}
int main()
{
std::thread* threadarray[10];
for (int i=0; i<10; ++i)
threadarray[i] = new std::thread(runcmd,"./a");
for (int i=0; i<10; ++i) {
threadarray[i]->join;
delete threadarray[i];
}
return 0;
}
我认为你可以使用 sched_setaffinity 指定核心 ID。
关于c++ - 使用 bash 脚本使用不同的参数(例如并行)同时运行一个命令 n 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462298/