我在同一个目录中有两个文件。
directory/
| a.c
| b.c
Run Code Online (Sandbox Code Playgroud)
交流电
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
pid_t pid;
int status;
int wret;
if ((pid = fork()) < 0)
printf("error");
else if(pid == 0)
{
printf("%s", argv[1]);
execv(argv[1], &argv[1]);
}
else
{
/* respawn */
if ((wret = wait(&status)) != -1)
execv(argv[1], &argv[1]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
bc 只是一个打印“hello”的简单程序。
我想从命令行运行./a b以进行a程序调用exexXX以执行b程序。
我不明白为什么如果我使用execv我只能./a b在命令行中写,而不是如果我使用execvp …