Chinaunix首页 | 论坛 | 博客
  • 博客访问: 151295
  • 博文数量: 69
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 602
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-25 20:56
文章分类

全部博文(69)

文章存档

2015年(68)

2014年(1)

我的朋友

分类: LINUX

2015-03-20 17:47:11

  1. 函数原型

int execv(const char *progname, char *const argv[]);   //#include <unistd.h>

 

  

     2. 用法介绍

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

progname: 被执行的应用程序。

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

 

      3. 返回值

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

 

       4. 示例


  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6.   
  7. main(void)  
  8. {  
  9.   pid_t pid = fork();  
  10.      
  11.   if( pid == 0 ) // this is the child process  
  12.   {  
  13.      execv("/bin/ls", NULL);  
  14.   
  15.      // the program should not reach here, or it means error occurs during execute the ls command.  
  16.      printf("command ls is not found, error code: %d(%s)", errno, strerror(errno);  
  17.   }  
  18. }  


阅读(946) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~