Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2852889
  • 博文数量: 523
  • 博客积分: 11908
  • 博客等级: 上将
  • 技术积分: 5475
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-03 15:50
文章分类

全部博文(523)

文章存档

2019年(3)

2013年(4)

2012年(71)

2011年(78)

2010年(57)

2009年(310)

分类: LINUX

2010-12-08 10:53:51

1.函数原型
int execv(const char *progname, char *const argv[]);   //#include
2. 用法介绍
         execv会停止执行当前的进程,并且以progname应用进程替换被停止执行的进程,进程ID没有改变。
progname: 被执行的应用程序。
argv: 传递给应用程序的参数列表, 注意,这个数组的第一个参数应该是应用程序名字本身,并且最后一个参数应该为NULL,不参将多个参数合并为一个参数放入数组。
 
3. 返回值
          如果应用程序正常执行完毕,那么execv是永远不会返回的;当execv在调用进程中返回时,那么这个应用程序应该出错了(可能是程序本身没找到,权限不够...), 此时它的返回值应该是-1,具体的错误代码可以通过全局变量errno查看,还可以通过stderr得到具体的错误描述字符串:
 
例子:
 

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
  
  
main(void)
{
  pid_t pid = fork();

  printf("pid = %d\n",pid)
      
  if( pid == 0 ) // this is the child process

  {
     execv("/bin/pwd", NULL);
  
     // the program should not reach here, or it means error occurs during execute the ls command.

     printf("command ls is not found, error code: %d(%s)", errno, strerror(errno));
  }
}


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