[英]Execv with files in a child process
I have two programs in the same directory main and example, if I execute ./example file.txt
it manages simple tasks with that file. 我在相同的主目录和示例目录中有两个程序,如果执行
./example file.txt
它将使用该文件管理简单任务。 Then my main program is 然后我的主程序是
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main (int argc, char* argv[]){
pid_t const pid1 = fork();
if(pid1==0){
execv();
}
else{
wait(NULL);
}
}
So I want to execute ./main file.txt
and the child process should execute ./example file.txt
with the same file that main has received in argv[1]. 因此,我想执行
./main file.txt
,子进程应使用main在argv [1]中收到的相同文件执行./example file.txt
。 My question is how to use the execv parameters to accomplish this. 我的问题是如何使用execv参数来完成此任务。
Sorry if I did not explained myself simple. 对不起,如果我没有简单地解释自己。 Thank You
谢谢
The easiest way to do this would be: 最简单的方法是:
execl("./example", "example", argv[1], (char *)NULL);
Alternatively, 或者,
char *args[] = {
"example",
argv[1],
NULL
};
execv("./example", args);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.