试析企业网站建设模式,穿着西裤做的网站,wordpress手动裁剪,得实网站建设文章目录进程替换进程替换是什么#xff1f;替换的方法进程替换简易shell模拟进程替换 
进程替换是什么#xff1f; 
如下图所示#xff1a; 
 进程替换就是#xff0c;把进程B的代码和数据#xff0c;替换正在执行的进程A的代码和数据在内存中的位置#xff08;若代码…
文章目录进程替换进程替换是什么替换的方法进程替换简易shell模拟进程替换 
进程替换是什么 
如下图所示 
 进程替换就是把进程B的代码和数据替换正在执行的进程A的代码和数据在内存中的位置若代码数据过多可能会改变页表但进程A的整体部分不发生任何改变task_struct、A进程地址空间等等  其实就是用A进程的壳子执行B进程程序不改变A进程的任何东西只改变页表物理地址部分和内存中的数据和代码不创建任何新的进程并且子进程也不会退出。 
替换的方法 
一般用到以下六种函数 
#include unistd.hint exec l(const char *path, const char *arg, …);int exec lp(const char *file, const char *arg, …);int exec le(const char *path, const char *arg, …,char *const envp[]);int exec v(const char *path, char *const argv[]);int exec vp(const char *file, char *const argv[]);int exec ve(const char *path, char *const argv[], char *const envp[]);
命名后缀 l(list) : 表示参数采用列表  v(vector) : 参数用数组  p(path) : 有p自动搜索环境变量PATH  e(env) : 表示自己维护环境变量 程序运行时的环境变量信息(函数不会给你自动继承父进程的环境变量需要手动设置)  返回值 
若替换失败则返回-1但其实可以不用检查返回值因为 调用成功一定执行替换的程序  调用失败一定执行原本的程序  int execl(const char *path, const char *arg, …);  void test1()    
{    
pid_t id  fork();    
if(id  0)    
{    
printf(你好\n);    /*********************************************开始替换******************************************/execl(/usr/bin/ls,ls,-a,-l,-i,NULL);   //你要执行谁想怎么执行在命令行怎么执行就怎么执行可变参数列表以NULL结尾//或者想要执行自己的程序 execl(./当前路径或者 /.../...绝对路径,可执行程序名,NULL);/*********************************************替换完成/失败******************************************/printf(hello\n);    }    sleep(1);    
printf(child exchange succeed\n);     
}    int execlp(const char *file, const char *arg, …); void test2()
{pid_t id  fork();if(id  0){char* argv[]  {ls,-a,-i,-l,NULL};//就是把可变参数列表以数组的形式传给execvprintf(exchange test2---:\n);
/*********************************************开始替换******************************************/execv(/usr/bin/ls,argv);
/*********************************************替换完成/失败******************************************/printf(exchange fail\n);}sleep(1);printf(exchange succeed\n);
}int execle(const char *path, const char *arg, …,char *const envp[]); void test3()
{pid_t id  fork();if(id  0){printf(exchange test3----\n);
/*********************************************开始替换******************************************/execlp(ls,ls,-l,-a,-i,NULL);// 第一个你要执行的是谁但不用带路径path会根据这个程序名去自动搜索它在什么位置第二个是要怎么执行
/*********************************************替换完成/失败******************************************/printf(exchange fail\n);}sleep(1);printf(exchange succeed\n);}int execvp(const char *file, char *const argv[]); void test4()
{pid_t id  fork();if(id  0){printf(exchange test4----\n);char* argv[]  {ls,-a,-l,-i,NULL};
/*********************************************开始替换******************************************/execvp(ls,argv); //第一个参数告诉path要执行的程序他会自动去找路径第二个参数从可变参数列表变为自定义数组
/*********************************************替换完成/失败******************************************/printf(exchange fail\n);}sleep(1);printf(exchange succeed\n);
}  int execle(const char *path, const char *arg, …,char *const envp[]); void test5()
{pid_t id  fork();if(id  0){printf(exchange test5----\n);
/*********************************************开始替换******************************************/char* env[]  {my_envhello,NULL};execle(./print,print,NULL,env);//最后一个参数env指定了新程序的环境列表。参数env对应于新程序的environ数组//传递自己的环境变量给print
/*********************************************替换完成/失败******************************************/printf(exchange fail\n);}sleep(1);printf(exchange succeed\n);}int main()
{extern char** environ;for(int i  0;environ[i];i){if(environ[i]  PATH)continue;//path显示的太多这里屏蔽掉printf(%s\n,environ[i]);}return 0;
}  int execve(const char *path, char *const argv[], char *const envp[]); void test6()
{pid_t id  fork();if(id  0){printf(exchange test5----\n);
/*********************************************开始替换******************************************/char* argv[]  {print,NULL};char* env[]  {my_envhello,NULL};execve(./print,argv,env);
/*********************************************替换完成/失败******************************************/printf(exchange fail\n);}sleep(1);printf(exchange succeed\n);}int main()
{extern char** environ;for(int i  0;environ[i];i){if(environ[i]  PATH)continue;printf(%s\n,environ[i]);}return 0;
}可以看出所有的函数都是在execve基础上封装的 进程替换 子进程需要替父进程执行一些任务就需要进程替换  进程替换只替换子进程在内存中的代码和数据以及页表物理地址部分  进程替换不会创建新进程不会退出子进程  虽然父子代码是共享的但是进程替换会更改内存的代码和数据所以要发生写实拷贝  fork创建子进程后在代码中exec…只会替换子进程因为进程具有独立性  
程序替换的本质是把程序的代码数据加载到指定进程的上下文中 简易shell模拟 
#include stdio.h
#include unistd.h
#include stdlib.h
#include sys/wait.h
#include string.hvoid myshell()
{char command[128];char* argv[64];while(1){command[0]  0;printf([awdVM-16-4-centos myshell]-----);	//打印前缀	fflush(stdout);									//刷新缓冲区fgets(command,128,stdin);						//输入命令command[strlen(command) - 1]  0;				//先当作整个字符串存入command-1是除去\n//fflush(stdout);									//printf(%s\n,command);验证const  char* set   ;							//设置分隔符argv[0]  strtok(command,set);					//把字符串拆解成指令int i  1;								while( argv[i]  strtok(NULL,set) )				//类似strcpy赋值到NULL退出i;/*for(int j  0;j  i;j)printf(%s\n,argv[j]);验证*/if(strcmp(argv[0],cd)  0)					//在子进程cd影响的只是子进程所以要再父进程处理{if(argv[1])chdir(argv[1]);continue;}if(fork()  0)								 //创建子进程{execvp(argv[0],argv);						 //替父进程执行这些指令exit(1);								     	 //若执行到这说明替换失败设置退出码为1}waitpid(-1,NULL,0);								//等待任意一个子进程结束int status  0;if(strcmp(argv[0],echo)  0  strcmp(argv[1],$?)  0)	//打印退出码和终止信号printf(exit code:%d ,exit signal:%d \n,WEXITSTATUS(status),WTERMSIG(status));}}int main()
{myshell();return 0;
} 通过这个简易shell来把之前学到的总结一下 
一般让子进程替父进程执行一些第三方命令那么就需要用到 进程替换和fork子进程每次执行结束需要进程等待为了结束他的僵尸进程并获取它的退出信息退出码、终止信号每次进程退出后又会重新创建子进程所以echo $? 查看的是最近一次执行的退出码证明了每一次命令行执行的指令都是一次进程是基于bash为父进程创建的子进程 
上面这个简陋shell综合了 fork、进程替换函数、进程等待函数、进程退出函数、退出码/终止信号加深了这些接口的理解 文章转载自: http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.zkpwk.cn.gov.cn.zkpwk.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.zpyxl.cn.gov.cn.zpyxl.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.mprtj.cn.gov.cn.mprtj.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.qwdlj.cn.gov.cn.qwdlj.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.fslrx.cn.gov.cn.fslrx.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.qncqd.cn.gov.cn.qncqd.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.qgbfx.cn.gov.cn.qgbfx.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.mkkcr.cn.gov.cn.mkkcr.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.cqwb25.cn.gov.cn.cqwb25.cn http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.xpzrx.cn.gov.cn.xpzrx.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.ndpwg.cn.gov.cn.ndpwg.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn