轉自http://blog.csdn.net/west_609/article/details/5941968
- 函數原型
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. 示例
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- main(void)
- {
- pid_t pid = fork();
- if( pid == 0 ) // this is the child process
- {
- execv("/bin/ls", 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);
- }
- }
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[]);