(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下进程的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.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

Java进程CPU使用率过高排查步骤详细讲解

《Java进程CPU使用率过高排查步骤详细讲解》:本文主要介绍Java进程CPU使用率过高排查的相关资料,针对Java进程CPU使用率高的问题,我们可以遵循以下步骤进行排查和优化,文中通过代码介绍... 目录前言一、初步定位问题1.1 确认进程状态1.2 确定Java进程ID1.3 快速生成线程堆栈二、分析

Python如何判断字符串中是否包含特殊字符并替换

《Python如何判断字符串中是否包含特殊字符并替换》这篇文章主要为大家详细介绍了如何使用Python实现判断字符串中是否包含特殊字符并使用空字符串替换掉,文中的示例代码讲解详细,感兴趣的小伙伴可以了... 目录python判断字符串中是否包含特殊字符方法一:使用正则表达式方法二:手动检查特定字符Pytho

C#继承之里氏替换原则分析

《C#继承之里氏替换原则分析》:本文主要介绍C#继承之里氏替换原则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#里氏替换原则一.概念二.语法表现三.类型检查与转换总结C#里氏替换原则一.概念里氏替换原则是面向对象设计的基本原则之一:核心思想:所有引py

Python多进程、多线程、协程典型示例解析(最新推荐)

《Python多进程、多线程、协程典型示例解析(最新推荐)》:本文主要介绍Python多进程、多线程、协程典型示例解析(最新推荐),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 目录一、multiprocessing(多进程)1. 模块简介2. 案例详解:并行计算平方和3. 实现逻

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序