用fork函数创建子进程后,子进程往往要调用一种exec函数以执行另一个程序,该子进程被新的程序替换,改变地址空间,进程映像和一些属性,但是pid号不变。
execve():
#include
int execve(const char *filename, char *const argv[], char *const envp[]);
参数含义:
filename:路径名,表示载入进程空间的新程序路径。
argv[]:命令行参数,argv[0]为命令名。
envp[]:新程序的环境变量。
返回值:成功时不会返回,使用时不用检查返回值,可通过errno检查。
以下函数都是根据execve实现:
int execl(const char *path, const char arg, …/ (char *) NULL */);
int execlp(const char *file, const char arg, …/ (char *) NULL */);
int execle(const char *path, const char arg, …/, (char *) NULL, char * const envp[] */);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);
实验代码:
创建子进程,子进程使用execl调用ls查看当前目录下的文件。
exec.c:
使用execl调用shell命令查看文件信息。


执行结果:

Linux系统基于这些函数实现了“popen”,“system”等函数,这些函数能够直接调用shell等函数。

|