execv函数族用法展示

来源:互联网 发布:淘宝店家虚假发货 编辑:程序博客网 时间:2024/06/13 04:44

传智扫地僧课程学习笔记。


/*

      #include <unistd.h>

      extern char **environ;

      int execl(const char *path, const char *arg, ...);

      int execlp(const char *file, const char *arg,...); PATH

      intexecle(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[]);

 

*/

总结:l代表可变参数列表,p代表在path环境变量中搜索file文件。envp代表环境变量


#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <signal.h>#include <errno.h>#include <signal.h>int main(void ){pid_t pid;printf("getpid():%d\n", getpid());      //int execlp(const char *file, const char *arg, ...);      //  int execle(const char *path, const char *arg,           //       ..., char * const envp[]);                      char * const envp[] = {"aaa=111", "bbb=2222", NULL};              //            int execle(const char *path, const char *arg,     //             ..., char * const envp[]);     //当你不填写envp时,默认传递当前用户的env环境变量、到hello印象中      //当你填写envp时,传递你写的envp    //env shell的环境变量的概念 execle("./hello", NULL, envp);printf("hello\n");return 0;}



#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <signal.h>#include <errno.h>#include <signal.h>extern char **environ;int main(){int i = 0;printf("getpid():%d\n", getpid());for (i=0; environ[i] != NULL; i++){printf("%s\n", environ[i]);}return 0;}

0 0
原创粉丝点击