10-LINUX--进程的替换

一. 进程的替换

1.替换原理

用fork()创建子进程后,子进程调用exec系列函数执行另一个程序。当子进程调用execl系列函数时,多用的空间和数据完全被新程序替代,从新程序的mian()开始执行。

强调:exec系列函数不创建新进程,所以新进程的id不会改变。

2.exec系列替换函数

cs 复制代码
#include<unistd.h>

int execl(const char *path, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char *const envp[]);
int execv(const char *path, char *const argv[]);
int execve(const char *path, char *const argv[], char *const envp[]);
int execlp(const char *file, const char *arg, ...);
int execvp(const char *file, char *const argv[]);

参数解释:

1)函数execl、execle、execlp要求将新程序的每个命令行参数都说明为一个单独的参数,这种参数表以空指针结尾。

2)对于另外3个函数,则先构造一个指向各参数的指针数组,然后将该数组地址作为这3个函数的参数。

3.替换过程

(1).获取命令行

(2).解析命令行

(3).建立一个子进程fork()

(4).替换子进程(exec)

(5).父进程等待子进程退出

4.测试用例

cs 复制代码
include<fcntL.h>
int main(int argc, chart argv[], chart envp[])
printf("pid=%dIn ppid=%d)n",getpid(), getppid());
//成功不返回
//execl("/usr/bin/ps","ps", "-f", (char*)0)://char*e 表示参数结束
//execlp("ps","ps", "-F", (char*)0);1//不用加指定路径,只需要说命令的名:
//execle("/usr/bin/ps","ps", "-f", (char*)o, envp);
char* nyargv[J]-C"pS", "-f", oj;
execv("/usr/bin/ps", nyargv);
execvp("ps", myargv);
execve("/usr/bin/ps", myargv, enyp);
printf("execl errln");exit(0);

5.fork 和 exec 联合使用创建一个全新的进程

如下示例,当前主程序复制产生一个子进程,子进程用新程序"b"替换自身。 当前主程序如下:

cs 复制代码
#include <stdio.h>   
#include <stdlib.h>   
#include <unistd.h>   
#include <assert.h>   
#include <string.h>   
#include <fcntl.h>   
#include <errno.h>   
int main(int argc, char* argv[],char* envp[])   
 {       
printf("main pid=%d\n",getpid());      
pid_t pid = fork();   
assert( pid != -1 );   
 if ( pid == 0 )   
     {            
char * myargv[] = {"b","hello","abc","123",(char*)0};   

//char * myenvp[] = {"MYSTR=hello","VAL=100",(char*)0};      
execve("./b",myargv,envp);   
perror("execl error");   
 }   
exit(0);   
wait(NULL);   
printf("main over\n");   
} 

新替换的b程序:

cs 复制代码
 #include <stdio.h>   
 #include <stdlib.h>   
 #include <unistd.h>   
 #include <assert.h>   
 #include <string.h>   
 int main(int argc, char* argv[],char* envp[]) 
   {       
printf("b pid=%d\n",getpid());        
int i = 0;   
            
printf("argc=%d\n",argc);   
     for( ; i < argc; i++ )   
     {            
printf("argv[%d]=%s\n",i,argv[i]);  
     }      
     for( i = 0; envp[i] != NULL; i++ )   
     {            
printf("envp[%d]=%s\n",i,envp[i]);   
     }          
 }  
exit(0):
}
相关推荐
Ruiery7 小时前
Linux 6.6内核 CPU 深度解析(七):调度器启动 — 从单核到多核,调度器分阶段点亮
linux·运维·服务器
50万马克的面包7 小时前
CSDN-栈队列数组-知识点整理
linux·运维·服务器
MicrosoftCloud7 小时前
用户权限 04|/etc/sudoers 安全配置:从最小授权到「别把 ALL 随便给人」
运维·安全·ubuntu·sudo·sudoers
wjkjpcba8 小时前
PCBA烧录程序是什么:PCBA包工包料厂家解析烧录与测试
linux·数据库·人工智能·smt贴片加工·pcba贴片加工厂
꯭自꯭闭꯭8 小时前
达梦事物特性及MVCC
linux·运维·数据库
91刘仁德9 小时前
HTTP协议详解:从URL到HTTP服务器的完整实战
服务器·网络·笔记·网络协议·http
亚川楼宇自控系统数据中心厂家10 小时前
实验室智能化管理系统|人环物智一体化管控平台
运维
羔羊++10 小时前
18_实验十七_其他命令与自定义启动变量
linux
zmsup11 小时前
Agent 上下文调优:结合 OpsArk 运维智能体,设计模型每一步真正需要的信息
运维·agent·上下文压缩·运维智能体·上下文调优
DYWorker00112 小时前
Linux驱动:RK3568 GMAC + YT8531C 千兆以太网全流程解剖
linux