【发布时间】:2016-04-04 14:47:55
【问题描述】:
我想在 OpenMP 循环中运行一个可执行文件。 我尝试使用以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <omp.h>
int main (int argc, char *argv[])
{
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
char thread_name[4];
sprintf(thread_name, "%d", thread_id);
printf("%s\n", thread_name);
char* arg[] = {"task", thread_name, NULL};
execv("./task", arg);
}
}
可以像这样用gcc生成对应的可执行文件:
gcc -fopenmp hello.c -o hello
任务脚本是一个非常简单的 bash 脚本:
#! /bin/sh
echo "Hello, I am process $1"
echo 'Please for me for 10 seconds...'
sleep 10
echo 'Thank you!'
我像这样运行我的程序:
./hello
来自包含“hello”可执行文件和“task”脚本的目录。
3
2
0
1
你好,我是进程 3
请帮我10秒...
谢谢!
似乎当第一个线程(在我的示例中为第三个)调用 execv 函数时,跳过了对 execv 的其他调用。
有人知道这里有什么问题吗?
谢谢!
编辑:系统的新代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
int main (int argc, char *argv[])
{
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
char thread_name[4];
sprintf(thread_name, "%d", thread_id);
char command[50];
strcat(command, "./task ");
strcat(command, thread_name);
system(command);
}
}
【问题讨论】: