(P13)exec替换进程映像

2024-06-08 06:18
文章标签 进程 替换 exec 映像 p13

本文主要是介绍(P13)exec替换进程映像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 1.exec替换进程映像
    • 2.exec关联函数组(execl、execlp、execle、execv、execvp)
    • 3.fcntl设置文件状态标志:FD_CLOEXEC

1.exec替换进程映像

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

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

  • fork的特点:创建一个进程,新进程与原进程是一样的!
    在shell命令提示符下输入ps的过程:如何加载ps程序?
    fork会创建一个新进程,但是该进程与原进程是一样的,但是可将新进程用ps程序替换(可称之为加载一个新的程序,用exec系列函数来加载新的程序)

  • eg:代码:P13exec.c

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(int argc, char *argv[])
{printf("Entering main ...\n");//将当前进程替换掉,代码段和数据段被替换了,但是进程控制块还是一样的,//所以只要替换成功,“Exiting main...\n”是不会输出的execlp("ls", "ls", "-l", NULL);printf("Exiting main...\n");return 0;
}
  • 测试:
    在这里插入图片描述
  • eg:代码:P13exec2.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(int argc, char *argv[])
{printf("Entering main ...\n");//替换之前的pid与hello程序输出的pid是一样的,替换之前与替换之后的pid应该是一样的printf("pid = %d\n", getpid());//将当前进程替换掉,代码段和数据段被替换了,但是进程控制块还是一样的,//所以只要替换成功,“Exiting main...\n”是不会输出的execlp("./hello", "hello", NULL);printf("Exiting main...\n");return 0;
}
  • 代码:
#include <stdio.h>
int main(void)
{printf("hello pid = %d\n", getpid());return 0;
}
  • 测试:P13hello.c
    在这里插入图片描述
  • eg:代码:P13exec3.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(int argc, char *argv[])
{printf("Entering main ...\n");//替换之前的pid与hello程序输出的pid是一样的,替换之前与替换之后的pid应该是一样的printf("pid = %d\n", getpid());int ret = execlp("hello", "hello", NULL);if (ret == -1)perror("execlp error");printf("Exiting main...\n");return 0;
}
  • 测试:
    为什么错误?
    Linux不会从当前路径底下搜索源程序,而是从环境变量的路径下搜索源程序!
    在这里插入图片描述

2.exec关联函数组(execl、execlp、execle、execv、execvp)

  • man execle
    功能:用exec函数可以把当前进程替换为一个新进程。
    exec名下是由多个关联函数组成的一个完整系列。
    头文件<unistd.h>

extern char **environ;int execl(const char *path, const char *arg, .../* (char  *) NULL */);
int execlp(const char *file, const char *arg, .../* (char  *) NULL */);
int execle(const char *path, const char *arg, .../*, (char *) NULL, char * const envp[] */);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);参数:
path:表示你要启动程序的名称,包括路径名
arg:表示启动程序所带的参数返回值:
成功返回0;
失败返回-1
  • execlp与execvp相比较
    代码:P13execvp.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(int argc, char *argv[])
{printf("Entering main ...\n");char *const args[] = {"ls", "-l", NULL};//将当前进程替换掉,代码段和数据段被替换了,但是进程控制块还是一样的,//所以只要替换成功,“Exiting main...\n”是不会输出的//execlp(程序文件名称,参数表(第一个参数是ls,第二个参数是-l),参数结束是NULL控制);// execlp("ls", "ls", "-l", NULL);//与execlp相比较,execvp将可变参数列表保存到变量argv中execvp("ls", args);printf("Exiting main...\n");return 0;
}

测试:
在这里插入图片描述

  • execlp与execl相比较
    代码:P13execl.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(int argc, char *argv[])
{printf("Entering main ...\n");// execlp("ls", "ls", "-l", NULL);//与execlp相比,execl的参数与其一样,但是ls命令必须指定全路径,他不会在全路径$PATH//下进行搜索,全路径可以是绝对的,也可以是相对的//带p,会到环境变量下找ls程序,不带p,则不会到环境变量下找ls程序int ret = execl("/bin/ls", "ls", "-l", NULL);if (ret == -1)ERR_EXIT("execl error");printf("Exiting main...\n");return 0;
}

测试:
在这里插入图片描述

  • execvp与execv相比较
    execv与execvp一样的,只是他不带p,不会去环境变量$PATH下去寻找程序

  • execle与execl相比较
    execle他不带p,不会去环境变量$PATH下去寻找程序;
    与execl相比较,execle他带了一个环境信息
    代码:P13execle.c

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(int argc, char *argv[])
{printf("Entering main ...\n");char *const envp[] ={"AA=11", "BB=22", NULL}//指定envp环境变量项,传递给hello2进程int ret = execle("./test1", "hello", NULL, envp);// int ret = execl("./test2", "hello", NULL);if (ret == -1)ERR_EXIT("execl error");printf("Exiting main...\n");return 0;
}

测试:

显示指定了环境变量信息,不会从shell继承了
在这里插入图片描述
若执行hello1.c程序:P13hello1.c

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

environ是一个指针数组,指针的指针,environ中的每一项如下图右边所示:
在这里插入图片描述

测试:
实现了env指令的功能
在这里插入图片描述

输入env,得到当前shell的环境变量
在这里插入图片描述

  • 总结
    (1)带l和不带l的区别,带l指定的是可变参数列表,最后一个参数是空指针,不带l,将参数列表存在指针数组argv中;
    带p和不带p的区别,带p的会从$PATH中搜索程序是否存在;
    带e和不带e的区别,带e的会传递一个环境信息给指定运行的进程(要加载的程序);
    (2)上述的函数最终都会调用execve系统调用函数(man execve)
int execve(const char *filename, char *const argv[],char *const envp[]);
第一个参数:文件名
第二个参数:参数表
第三个参数:环境信息

3.fcntl设置文件状态标志:FD_CLOEXEC

  • eg:代码:P13fcntl_exec.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(int argc, char *argv[])
{printf("Entering main ...\n");//将标准输出fd的FD_CLOSEEXC进行置位,说明1号文件描述符已经被关闭了//hello2程序是无法输出的//与man 2 open的flags的O_CLOEXEC的功能是一样的int ret = fcntl(1, F_SETFD, FD_CLOEXEC);if (ret == -1)ERR_EXIT("fcntl error");execlp("./test1", "tes1", NULL);printf("Exiting main...\n");return 0;
}
  • 测试:
    在这里插入图片描述

  • Makefile

.PHONY:clean all
CC=gcc
CFLAGS=-Wall -g
BIN=01exec hello hello1
all:$(BIN)
%.o:%.c$(CC) $(CFLAGS) -c $< -o $@
clean:rm -f *.o $(BIN)

这篇关于(P13)exec替换进程映像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1041403

相关文章

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st

一文解密Python进行监控进程的黑科技

《一文解密Python进行监控进程的黑科技》在计算机系统管理和应用性能优化中,监控进程的CPU、内存和IO使用率是非常重要的任务,下面我们就来讲讲如何Python写一个简单使用的监控进程的工具吧... 目录准备工作监控CPU使用率监控内存使用率监控IO使用率小工具代码整合在计算机系统管理和应用性能优化中,监

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.