zoukankan      html  css  js  c++  java
  • Linux execve函数簇用法

    exec函数簇实现的功能都是用一个新程序替换原来的程序,替换的内容包括堆栈段,代码段,进程控制器PCD,但是原进程的PID保持不变
    int execl(const char *path, const char *arg, ...);
    ...表示参数是可变参数列表,例如execl("hello","参数1","参数2","参数3",NULL)
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    int main(int arg,char *args[])
    {
        execl("/bin/ls","/bin/ls","-la",NULL);
        /*备注:这里参数写"ls",execl()无法找到ls程序*/
        printf("fly with me
    ");
        return 0;
    }
    int execlp(const char *file, const char *arg, ...);
    p表示无需填写路径,只要填写文件名称,因为execlp()函数会在当前环境变量PATH下查找该文件
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    int main(int arg,char *args[])
    {
        execlp("ls","ls","-la",NULL);
        printf("fly with me
    ");
        return 0;
    }
    int execle(const char *path, const char *arg,..., char * const envp[]);
    参数envp是一个以NULL结尾的字符串数组,代表程序员自定义的环境变量,标准写法是KEY=VALUE
    如果envp参数传值NULL,在被替换进程打印的是NULL
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    int main(int arg,char *args[])
    {
        printf("old getpid()=%d
    ",getpid());
        char *envps[]={"aa=11","bb=22",NULL};
        execle("./hello","./hello",NULL,envps);
        return 0;
    }
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    extern char **environ;
    
    int main(int arg,char *args[])
    {
        printf("fly with me;getpid()=%d
    ",getpid());
        int i=0;
        for(i=0;environ[i]!=NULL;i++)
        {
            printf("%s
    ",environ[i]);
        }
        return 0;
    }
    int execv(const char *path, char *const argv[]);
    int execvp(const char *file, char *const argv[]);
  • 相关阅读:
    [转] MathType的灵活运用
    [zz] 模式识别,计算机视觉领域,期刊
    SQL语句 合并列值 将一列的多个值合并成一行
    目标板识别为U盘
    android 事件传递机制
    linux有关文件权限的命令
    linux中的jiffies变量
    分析Android 根文件系统启动过程(init守护进程分析)
    2010年暂订读书目录
    Android Power Management
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/6067580.html
Copyright © 2011-2022 走看看