c++ - 使用 bash 脚本使用不同的参数(例如并行)同时运行一个命令 n 次

标签 c++ linux performance bash openmp

我想同时运行一个具有不同参数的命令,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。

看: http://www.thinkingparallel.com/2006/08/18/more-information-on-pthread_setaffinity_np-and-sched_setaffinity/

关于c++ - 使用 bash 脚本使用不同的参数(例如并行)同时运行一个命令 n 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462298/

相关文章:

c++ - 如何在 C++11 中使用随机生成器作为类成员

python - ImportError : matplotlib is required for plotting when the default backend "matplotlib" is selected

c++ - 在这种情况下,为什么 STL priority_queue 并不比 multiset 快多少?

php - 拥有更有效的代码以返回错误

c++ - 在 Windows 上强制刷新 ofstream 文件

c++ - 链表中的析构函数是如何递归调用的?

c++ - 为文本中的每个单词创建图像的建议

c++ - 如何在不离开 X.11 环境的情况下开发 DirectFB 应用程序

linux - 在 Fedora 中构建 KeePass 2.0 Alpha 6

MySQL InnoDB 查询性能