桂林临桂区建设局网站,茶叶推广方案,必应搜索引擎,深圳有实力的优化公司exec函数族
1. 执行指定目录下的程序
#include unistd.h
int execl(const char *path, const char *arg, ...);返回值#xff1a;若出错#xff0c;返回-1#xff1b;若成功#xff0c;不返回
分析#xff1a;
path: 要执行的程序的绝对路径变参arg: 要执行的…exec函数族
1. 执行指定目录下的程序
#include unistd.h
int execl(const char *path, const char *arg, ...);返回值若出错返回-1若成功不返回
分析
path: 要执行的程序的绝对路径变参arg: 要执行的程序的需要的参数第一arg:占位后边的arg: 命令的参数参数写完之后: NULL一般执行自己写的程序2. execv函数原型
#include unistd.h
int execv(const char *path, char *const argv[]);返回值若出错返回-1若成功不返回
参数
path /bin/pschar* args[] {ps, aux, NULL};execv(/bin/ps, args);3. 执行PATH环境变量能够搜索到的程序
#include unistd.h
int execlp(const char *file, const char *arg, ...);返回值若出错返回-1若成功不返回
分析
file: 执行的命令的名字第一arg:占位后边的arg: 命令的参数参数写完之后: NULL执行系统自带的程序 execlp执行自定义的程序: file参数绝对路径 4. execvp函数原型
#include unistd.h
int execvp(const char *file, char *const argv[]);返回值若出错返回-1若成功不返回 5. 执行指定路径, 指定环境变量下的程序
#include unistd.h
int execle(const char *path, const char *arg, ..., char *const envp[]);返回值若出错返回-1若成功不返回
分析
path: 执行的程序的绝对路径 /home/itcast/a.outarg: 执行的的程序的参数envp: 用户自己指定的搜索目录, 替代PATHchar* env[] {/home/itcast, /bin, NULL};6. execve函数原型
#include unistd.h
int execve(const char *path, char *const argv[], char *const envp[]);返回值若出错返回-1若成功不返回 二、程序清单
1. 测试代码
#include unistd.h
#include stdio.h
#include stdlib.hint main()
{for(int i 0; i 8; i){printf( parent i %d\n, i);}pid_t pid fork();if(pid 0) {execlp(ps, ps, aux, NULL);perror(execlp);exit(1);}for(int i 0; i 3; i) {printf(----------- i %d\n, i);}return 0;
}
输出结果