如何在Linux中使用execv系统调用? [英] How to use execv system call in linux?

查看:390
本文介绍了如何在Linux中使用execv系统调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用execl编写一个程序来执行我的正在测试的exe文件,该文件运行得很好,并在Linux CLI中显示输出.但是我不知道如何将execl更改为execv,尽管我知道两个系统调用都会给出相同的值.我对execv系统调用

I am writing a program using execl to execute my exe file which is testing and it's work very well and display the output in the Linux CLI. But I have not idea how to change the execl to execv, although I know both of the system call will give the same value. I am confused with the array argument for execv system call

这是我的execl示例程序

This is my execl sample program

int main(void)
{
   int childpid;
   if((childpid = fork()) == -1 )
{
   perror("can't fork");
   exit(1);
}
 else if(childpid == 0)
{
  execl("./testing","","",(char *)0);
  exit(0);
}
else
{
printf("finish");
exit(0);
}
}

我可以知道如何将execl更改为execv吗?我从网上阅读的内容必须设置exe文件的文件路径和array的参数.为了要求程序执行测试exe文件,需要为数组设置哪种类型的参数? https://support.sas.com/documentation/onlinedoc/sasc /doc/lr2/execv.htm

链接是否包含我想要的东西?但是我从中读取的命令是请求列出文件,而不是执行文件.纠正我,我有任何错误

can I know how to change the execl to execv. What I read from online, we must set the file path for my exe file and the argument of array . What type of argument need to set for the array in order to ask the program to execute the testing exe file ? https://support.sas.com/documentation/onlinedoc/sasc/doc/lr2/execv.htm

Is it the link consist of the thing I want ? But what I read from it ,the command is request the list the file,not execute the file. Correct me I make any mistake

推荐答案

为了查看execl和execv之间的区别,下面是执行

In order to see the difference between execl and execv, here is a line of code executing a

ls -l -R -a

execl("/bin/ls", "ls", "-l", "-R", "-a", NULL);

with execv:

char* arr[] = {"ls", "-l", "-R", "-a", NULL};
execv("/bin/ls", arr);

发送到execv的char *数组将作为argv(在int main(int argc, char **argv)中)传递到/bin/ls

The array of char* sent to execv will be passed to /bin/ls as argv (in int main(int argc, char **argv))

有关详细信息,请参见 execl(3)Linux手册页.

Here is the execl(3) Linux manual page for more detail.

这篇关于如何在Linux中使用execv系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆