在 OpenMP 循环中调用 execv
Call execv in OpenMP loop
我想 运行 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 "
echo 'Please for me for 10 seconds...'
sleep 10
echo 'Thank you!'
我运行我的程序是这样的:
./hello
来自包含“hello”可执行文件和“任务”脚本的目录。
3
2
0
1
Hello, I am process 3
Please for me for 10 seconds...
Thank you!
似乎当第一个线程(在我的例子中是第 3 个线程)调用 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);
}
}
函数 execv
将用给定函数的参数创建的新进程替换当前进程。
为了达到你想要的效果,你应该使用 system
,或者 fork
/execv
.
我想 运行 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 "
echo 'Please for me for 10 seconds...'
sleep 10
echo 'Thank you!'
我运行我的程序是这样的:
./hello
来自包含“hello”可执行文件和“任务”脚本的目录。
3
2
0
1
Hello, I am process 3
Please for me for 10 seconds...
Thank you!
似乎当第一个线程(在我的例子中是第 3 个线程)调用 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);
}
}
函数 execv
将用给定函数的参数创建的新进程替换当前进程。
为了达到你想要的效果,你应该使用 system
,或者 fork
/execv
.