简体   繁体   English

在子进程中执行文件

[英]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.

相关问题 “ execv”子进程的存在和终止 - “execv” child process existance and termination 等待执行可执行脚本的子进程 - Waitpid for a child process that execv an unexecutable script 将stdout馈送到将对execv()进行排序的子进程 - Feeding stdout to a child process which will execv() sort 在子进程中传递 arguments 的正确方法(execv 和 excvp) - The correct way to pass arguments in a child process (execv and excvp) 如果在fork()之后使用execv(),子进程是否可以访问管道? - Does child process get access to pipe if execv() is used after fork()? 在execv执行的子进程中使用Scanf()无法正常工作 - Using Scanf() in child process executed via execv not working 子进程上的execv将其父进程ID保留在新程序中,而不是零 - execv on a child process is keeping its parents process id in the new program instead of zero 如果execv失败,为什么进程会创建一个僵尸进程,但如果execv成功并终止,则为什么不创建僵尸进程? - Why does a process create a zombie if execv fails, but not if execv is successful and terminates? 在 C 中调用 execv(“/bin/sh”, NULL) 时,如何与子进程通信(例如,通过写入运行命令)? - how can I communicate (eg. run commands via write) with a child process when execv(“/bin/sh”, NULL) was called in C? C execv()函数会终止子进程吗? - Does the C execv() function terminate the child proccess?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM