【Linux】自定义子进程(第十篇)

exec替换进程映像(vfork 要结合 exec使用)

1.execl函数

用这个函数可以把当前进程替换为一个新进程,且新进程与原进程有相同的PID。

在进程的创建上Unix采用了一个独特的方法,它将进程创建与加载一个新进程映象分离。这样的好处是有更多的余地对两种操作进行管理。

当我们创建了一个进程之后,通常将子进程替换成新的进程映象,这可以用exec系列的函数来进行。当然,exec系列的函数也可以将当前进程替换掉。

示例:

#include <stdio.h>
#include <unistd.h>
int main()
{
​
    pid_t pid = fork();
    if(pid ==0)
     {
         execl("/bin/ls","ls","-al",NULL);
         printf("execl endl\n");  //会被覆盖
     }
     printf("process.....\n");
   return 0;
}
​

注意事项: 如果需要子进程执行自定义代码或者任务,需要在fork后,exec之前完成。

2.exec系列函数(execl、execlp、execle、execv、execvp)

头文件<unistd.h>

extern char **environ;

原型:

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

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

int execv(const char *path, char *const argv[]);

int execvp(const char *file, char *const argv[]);

int execle(const char *path, const char *arg, ..., char * const envp[]);

参数:

path参数表示你要启动程序的名称包括路径名

arg参数表示启动程序所带的参数,一般第一个参数为要执行命令名,不是带路径且arg必须以NULL结束

返回值:成功返回0,失败返回-1

以上exec系列函数区别:

1,带l 的exec函数:execl,execlp,execle,表示后边的参数以可变参数的形式给出且都以一个空指针结束。

2,带 p 的exec函数:execlp,execvp,表示第一个参数path不用输入完整路径,只有给出命令名即可,它会在环境变量PATH当中查找命令

3,不带 l 的exec函数:execv,execvp表示命令所需的参数以char *arg[]形式给出且arg最后一个元素必须11

是NULL

4,带 e 的exec函数:execle表示,将环境变量传递给需要替换的进程

示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
​
int main(void)
{
    printf("entering main process---\n");
    execl("/bin/ls","ls","-l",NULL); // l 所有的参数需要以独立字符里的形式传入
    execlp("ls","ls","-l",NULL); //p 使用者无需填写程序位置,系统自动查找适配
    char* arg_array[] ={"ls","-al",NULL};
    execv("/bin/ls",arg_array); //v 所有参数封装到数组中,传入数组
   // execle("/bin/ls","ls","-al",NULL,arg_array); //e 允许exec重载时使用开发者自定义环境变量
    printf("exiting main process ----\n");
    return 0;
}
​

execle的用法示例:

execle.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(int argc, char *argv[])
{
    char * const envp[] = {"AA=11", "BB=22", NULL};
    printf("Entering main ...\n");
    int ret;
    //ret =execl("./hello", "hello", NULL);
    ret =execle("./hello", "hello", NULL, envp);
    if(ret == -1)
        perror("execl error");
    printf("Exiting main ...\n");
    return 0;
}

hello.c

#include <unistd.h>
#include <stdio.h>
extern char** environ;  声明就能用 
 
int main(void)
{
    printf("hello pid=%d\n", getpid());
    int i;
    for (i=0; environ[i]!=NULL; ++i)
    {
        printf("%s\n", environ[i]);
    }
    return 0;
}

使用方法:

mytest@buka:~/mytest/fork$ vi execle.c
mytest@buka:~/mytest/fork$ gcc execle.c -o app
mytest@buka:~/mytest/fork$ vi hello.c
mytest@buka:~/mytest/fork$ gcc hello.c -o hello
mytest@buka:~/mytest/fork$ ./app
father process
mytest@buka:~/mytest/fork$ hello pid=18589
AA=aa
BB=bb
​

注意:NULL:哨兵节点,exec初始化加载参数到NULL结束,必须写,否则exec报错。(NULL不可以用0代替)

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱编程的小猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值