【问题标题】:executing two program in differet processors in parallel在不同的处理器中并行执行两个程序
【发布时间】:2011-12-15 06:10:21
【问题描述】:

我使用这个 c/c++ 代码安排 2 个处理器并行运行 2 个不同的程序。请问如何确认 2 个处理器并行运行 2 个程序?

#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <sched.h>
#include <stdio.h>
#include <cstdlib>

int main(int argc, char *argv[])
{
cpu_set_t  mask;
CPU_ZERO(&mask);
int pid;
pid=fork();
  if (pid == 0) { /* second child */
        CPU_SET(0, &mask);
     sched_setaffinity(0, sizeof(mask), &mask);
     system("/home/ifeanyi/Process/PID/Debug/PID");
  }
  else if (pid > 0) { // Parent ends
      CPU_SET(1, &mask);
      sched_setaffinity(getpid(), sizeof(mask), &mask);
      cout << getpid() << endl;
      system("/home/ifeanyi/Process/checkpointing/Debug/checkpointing"); // Last leaf
      }
       cout << endl;
  }

【问题讨论】:

  • 您至少应该检查 sched_setaffinity 的结果代码 - 它返回 0 或 -1:linux.die.net/man/2/sched_setaffinity
  • 当我检查两者的结果代码时,它返回 0;有没有办法检查它 - 说处理器 0 运行 PID 和处理器 1 运行检查点?感谢您提前提供帮助

标签: parallel-processing processors


【解决方案1】:

相比在程序中硬编码这些细节,通常使用taskset 从命令行执行它更容易、更灵活,例如

$ taskset -c 0 ./my_program &
$ taskset -c 1 ./my_program &

【讨论】:

    猜你喜欢
    • 2012-01-08
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多