(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

相关文章

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

Python进行word模板内容替换的实现示例

《Python进行word模板内容替换的实现示例》本文介绍了使用Python自动化处理Word模板文档的常用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录技术背景与需求场景核心工具库介绍1.获取你的word模板内容2.正常文本内容的替换3.表格内容的

MySQL批量替换数据库字符集的实用方法(附详细代码)

《MySQL批量替换数据库字符集的实用方法(附详细代码)》当需要修改数据库编码和字符集时,通常需要对其下属的所有表及表中所有字段进行修改,下面:本文主要介绍MySQL批量替换数据库字符集的实用方法... 目录前言为什么要批量修改字符集?整体脚本脚本逻辑解析1. 设置目标参数2. 生成修改表默认字符集的语句3

C#使用SendMessage实现进程间通信的示例代码

《C#使用SendMessage实现进程间通信的示例代码》在软件开发中,进程间通信(IPC)是关键技术之一,C#通过调用WindowsAPI的SendMessage函数实现这一功能,本文将通过实例介绍... 目录第一章:SendMessage的底层原理揭秘第二章:构建跨进程通信桥梁2.1 定义通信协议2.2

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

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