execv使用

轉自http://blog.csdn.net/west_609/article/details/5941968

  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 <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <unistd.h>  
  4. #include <errno.h>  
  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. }  

 

 

  

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 *const argv[]);
int execvp(const char *
file, char *const argv[]);


發佈了0 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章