execv函数使用

1.函数原型

 

#include<unistd.h>

int execv(const char *path, char *constargv[]);   


2. 用法介绍

execv会停止执行当前的进程,并且以path应用进程替换被停止执行的进程,进程ID没有改变。

 

path: 代表文件的路径


argv: 传递给应用程序的参数列表, 注意,这个数组的第一个参数应该是应用程序名字本身,并且最后一个参数应该为NULL,不参将多个参数合并为一个参数放入数组。


3. 返回值

         如果应用程序正常执行完毕,那么execv是永远不会返回的;当execv在调用进程中返回时,那么这个应用程序应该出错了(可能是程序本身没找到,权限不够...), 此时它的返回值应该是-1,具体的错误代码可以通过全局变量errno查看,还可以通过stderr得到具体的错误描述字符串。


4. 示例

#include <stdlib.h>  
#include <stdio.h>  
#include <unistd.h>  
#include <errno.h>  
    
int main(void)  
{  
 char *argv[]={"ls","-l","/home",(char *)0};
  pid_t pid = fork();  

  if( pid == 0 ) // this is the child process  
  {  
     execv("/bin/ls", argv);  
  }  
  return 0;
}  

执行结果:

[root@localhost test]# total 68
drwx------  13 baihaoliang baihaoliang  4096 Jul 21 11:50 baihaoliang
drwxr-xr-x. 20 root        root         4096 Oct 22 11:05 gengchaolong
drwxr-xr-x   3 root        root         4096 Feb 11  2015 git_test
drwxrwxrwx   7 hudefeng    hudefeng     4096 Aug 11 17:42 hudefeng
drwx------. 17 icg         icg          4096 Dec 23  2014 icg
drwxr-xr-x. 18 jiangxin    jiangxin     4096 Oct 23 14:46 jiangxin
drwxrwxrwx.  2 root        root         4096 Aug  3 09:00 lixiao
drwx------.  2 root        root        16384 Dec 22  2014 lost+found
drwxrwxrwx. 13 root        root         4096 Oct 10 09:19 mobile
drwxr-xr-x. 36 renhongquan renhongquan  4096 Oct 26 14:52 renhongquan
drwxr-xr-x   3 root        root         4096 Feb 10  2015 repo_server
drwxrwxrwx  25 tangshun    tangshun     4096 Oct 10 14:32 tangshun
drwxrwxrwx  14 root        root         4096 Oct 23 11:20 wumingming
drw-rw-rw-   2 root        root         4096 Oct 23 13:59 xiexzhishu


5.相关函数

int execl(const char *path, const char*arg, ...);

int execlp(const char *file, const char*arg, ...);

int execle(const char *path, const char*arg,..., char * const envp[]);

int execv(const char *path, char *constargv[]);

int execvp(const char *file, char *constargv[]);



  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值