synopsis

#include <unistd.h>
 
int execve(const char *pathname, char *const argv[],
           char *const envp[]);
 

discription

execve() executes the program referred to by pathname. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

调用 execve() 的程序会被 pathname 指向的程序替代,新的程序有全新的堆栈和数据段。argvenvp 参数都是字符串指针数组,并且都以 NULL 结尾。

More detail

Usage

通常搭配 fork 一起使用。

example

#include <unistd.h>
 
int main() {
    char *argv[] = {"ls", "-al", ".", NULL};
    char *envp[] = {NULL};
    execve("/bin/ls", argv, envp);
 
    return 0;
}

相当于直接执行命令 ls -al .