exec函数族中真正意义上的系统调用只有execve(),
exec函数族的作用是根据指定的文件名,找到可执行文件,并用它来取代调用进程的内容。
在调用进程内部执行一个可执行文件。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int ret;
//1、execl函数
/** int execl(const char *path, const char *arg, ...) **/
//第一个参数指定路径,最后一个参数为NULL
#if 0
ret = execl("/mnt/hgfs/hushuai/CPP/0724/bubble","./bubble", NULL);
if (ret < 0)
{
perror("execl");
return -1;
}
#endif
//2、execlp函数
/** int execlp(const char *file, const char *arg, ...) **/
//execlp()会从PATH环境变量所指的目录中查找符合参数的file的文件名
#if 0
ret = execlp("ls", "ls", "-al", "/mnt", NULL);
if (ret < 0)
{
perror("execlp");
return -1;
}
#endif
//3、execve函数
/** int execve(const char *filename, char *const argv[], char *const envp[]) **/
#if 0
cihar *argv[] = {"ls", "-al", "/mnt", NULL};
char *envp[] = {"PATH = /bin", NULL};//将路径设为新的环境变量
ret = execve("/bin/ls", argv, envp);
if (ret < 0)
{
perror("execve");
return -1;
}
#endif
//4、execvp函数
/** int execvp(const char *file, char *const argv[]) **/
#if 0
char *argv[] = {"vim", "/mnt/hgfs/hushuai/CPP/0724/bubble.c", NULL};
ret = execvp("vim", argv);
if (ret < 0)
{
perror("execvp");
return -1;
}
#endif
//5、system函数
/** int system(const char *string) **/
#if 1
ret = system("ls /mnt/hgfs/hushuai/CPP/0724");
if (ret < 0)
{
perror("system");
return -1;
}
#endif
}