fork () / execv () зависает на узлах MPI (C ++)

Я пишу C ++ программу с MPI, которая будет запускать внешние программы на узлах MPI. Для этого я использую fork () / execv ().

Проблема в том, что процесс запускается нормально, но затем останавливается, если используется большое количество процессоров (nCPU> 48). У меня есть основания полагать, что проблема вызвана методом, который использует fork () / execv ().

Код:

int execute (char *task) {
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

pid_t child_pid;
int status;

if ((child_pid = fork()) < 0) {
cout << "Warning: fork failure on Node#" << world_rank << " with task " << task << endl;
perror("Warning (fork failure)");
exit(1);
}

if (child_pid == 0) {
//Execute on child thread

//Prepare command line arguments as a null-terminated array
std::vector<char*> args;
char* tasks = strtok(task, " ");
while(tasks != NULL) {
args.push_back(tasks);
tasks = strtok(NULL, " ");
}
args.push_back(NULL);

//Execute program args[0] with arguments args[0], args[1], args[2], etc.
execv(args[0], &args.front());

//Print on failure
cout << "Warning: execl failure on Node#" << world_rank << " with task " << task << endl;
perror("Warning (execl failure)");
_exit(1);
}
else {
//Parent process - wait for status message
wait(&status);
}

//Return status message
return status;
}

ПОСТАНОВИЛИ (по крайней мере, это выглядит так …)

Я изменил свой код для реализации vfork () вместо fork (), и теперь все работает. Обратите внимание, что после vfork () сразу же следует execve или _exit, когда он возвращается к дочернему процессу.

Код:

int execute (char *task) {
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

pid_t child_pid;
int status;

//Prepare command line arguments as a null-terminated array
std::vector<char*> args;
char* tasks = strtok(task, " ");
while(tasks != NULL) {
args.push_back(tasks);
tasks = strtok(NULL, " ");
}
args.push_back(NULL);

if ((child_pid = vfork()) < 0) {
exit(1);
}

//Child process
if (child_pid == 0) {

// !!! Since we are using vfork the child process must immediately call _exit or execve !!!
// !!!    _Nothing_ else should be done in this part of the code to avoid corruption    !!!

//Execute program args[0] with arguments args[0], args[1], args[2], etc.
execve(args[0], &args.front(), NULL);
_exit(1);
}
//Parent process
else {
//Wait for child process
wait(&status);
}

//Return status message
return status;
}

1

Решение

Задача ещё не решена.

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector