在不同的处理器中并行执行两个程序

问题描述:

我用这个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; 
    } 
+0

你至少应该从了sched_setaffinity检查结果代码 - 它返回0或-1:http://linux.die.net/man/2/sched_setaffinity – 2011-12-15 06:57:15

而不是硬编码在程序中这些细节时,通常更容易,更灵活地使用taskset命令行,例如做

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