当前位置: 首页 > news >正文

京东商城网站地址是多少临沂百度seo

京东商城网站地址是多少,临沂百度seo,怎么建设信息网站,做网站的被拘留了文章目录 进程程序替换替换原理进程替换的理解 环境变量与进程替换命令行解释器实现逻辑 进程程序替换 前面已经学习了子进程的创建,但是子进程的创建不管怎么说,都是父进程代码的一部分,那么实际上如果想要子进程执行新的程序呢&#xff1f…

文章目录

  • 进程程序替换
    • 替换原理
    • 进程替换的理解
  • 环境变量与进程替换
  • 命令行解释器
    • 实现逻辑

进程程序替换

前面已经学习了子进程的创建,但是子进程的创建不管怎么说,都是父进程代码的一部分,那么实际上如果想要子进程执行新的程序呢?

也就是说,执行全新的代码和访问全新的数据,不再和父进程有瓜葛呢?这个时候就引入了关于进程替换的概念

替换原理

在这里插入图片描述
用fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并未改变

进程替换的理解

首先演示基本用法:

单进程下的用法

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>int main()
{printf("execl begin:\n");execl("/usr/bin/ls", "ls", "-a", "-l", "-n", NULL);printf("execl end:\n");return 0;
}

调用结果:

[test@VM-16-11-centos 11-8]$ ./myprocess 
execl begin:
total 28
drwxrwxr-x  2 1003 1003 4096 Nov  9 10:50 .
drwxrwxrwt 16 1003 1003 4096 Nov  8 20:40 ..
-rw-rw-r--  1 1003 1003   74 Nov  8 20:41 Makefile
-rwxrwxr-x  1 1003 1003 8416 Nov  9 10:50 myprocess
-rw-rw-r--  1 1003 1003  175 Nov  9 10:47 myprocess.c

从中看出,它的基本原理就是在进程中进行进程的替换

为什么最后输出的printf不被调用呢?

这是因为,执行到进程替换函数的时候,如果成功,整个进程的代码和数据都会被替换为所需替换的目标代码和数据,这样在后续执行的时候都会使用这份新的代码和数据,因此不会调用后续出现的代码

多进程版本的程序替换

将上述的代码更改为含有子进程的代码,具体如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>int main()
{pid_t id = fork();if(id == 0){// childprintf("pid:%d,begin to exec!\n",getpid());sleep(3);execl("/usr/bin/ls","ls","-a","-l",NULL);printf("pid:%d,end to exec!\n",getpid());}else {// fatherprintf("wait child\n");pid_t rid = waitpid(-1,NULL,0);if(rid > 0){printf("wait success\n");}}return 0;
}

实验结果如下:

[test@VM-16-11-centos 11-8]$ ./myprocess 
wait child
pid:18212,begin to exec!
total 28
drwxrwxr-x  2 test test 4096 Nov  9 11:05 .
drwxrwxrwt 16 test test 4096 Nov  8 20:40 ..
-rw-rw-r--  1 test test   74 Nov  8 20:41 Makefile
-rwxrwxr-x  1 test test 8672 Nov  9 11:05 myprocess
-rw-rw-r--  1 test test  695 Nov  9 11:05 myprocess.c
wait success

从中看出多进程替换中增加了父进程对子进程的等待和回收的部分功能

那在多进程下应该如何理解进程替换呢?用下面图示的过程来演示:

在这里插入图片描述
从这里的进程替换中可以发掘出一些东西,替换的是进程,而不是代码,所以这里可以替换的内容有很多,甚至可以是Java写的程序运行起来的进程等等,看下面的实验

下面实现一个cpp程序

#include <iostream>int main()
{std::cout<<"this is a cpp program"<<std::endl;return 0;
}

对前面的程序进行修改

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>int main()
{pid_t id = fork();if(id == 0){// childprintf("pid:%d,begin to exec!\n",getpid());sleep(3);execl("./cpptest","./cpptest",NULL);//execl("/usr/bin/ls","ls","-a","-l",NULL);printf("pid:%d,end to exec!\n",getpid());}else {// fatherprintf("wait child\n");pid_t rid = waitpid(-1,NULL,0);if(rid > 0){printf("wait success\n");}}return 0;
}

对Makefile进行修改

.PHONY:all
all:myprocess cpptestcpptest:cpptest.cc g++ -o $@ $^myprocess:myprocess.cgcc -o $@ $^
.PHONY:clean
clean:rm -rf myprocess cpptest

这里利用的是Makefile自带的自我推演能力,使用Makefile进行自我推演可以推演出,现在需要myprocess和cpptest,而这两个程序又会分别进行执行运行

此时进行运行,此时会做出如下的实验结果:

[test@VM-16-11-centos 11-8]$ ./myprocess 
wait child
pid:23071,begin to exec!
this is a cpp program
wait success

从中不难看出,确实实现了进程的替换,而且替换的还是其余进程

这也就解释了在不同的公司中是可以存在分块进行构建模块功能的,最后都可以通过进程的形式链接起来

从某种意义来说,进程的替换已经可以被看成是一种系统调用了,站在系统的视角看内存中的所谓进程,实际上是一样的,系统高于一切,它可以对进程进行调度和分配

环境变量与进程替换

当进行进程替换的过程中,对于环境变量的角度来讲,是以什么样的情况进行的传递呢?

结论是:子进程对应的环境变量,是可以直接从父进程来的

对这个结论进行验证:

有关进程替换的一些函数
在这里插入图片描述

  1. execl函数,需要找到命令所在的文件目录,使用方法如下:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main()
{pid_t id = fork();if(id == 0){// child// 进行进程替换execl("/usr/bin/ls", "ls", "-a", "-l", "-d", NULL);}else {// parent// 对子进程回收pid_t rid = waitpid(-1, NULL, 0);if(rid > 0){printf("wait success\n");}}return 0;
}
  1. execlp函数:会到系统默认的路径下寻找命令
execlp("ls", "ls", "-a", "-l", "-d", NULL);
  1. execle函数:用一个程序调用另外一个程序,但环境变量是自己的环境变量,不是系统的,通过获取环境变量查看

如何在进程中添加一个环境变量?用到的是putenv函数:

void *putenv(char *name)

由此可以写出下面的程序

#include <iostream>int main(int argc, char* argv[], char* env[])
{// 输出命令行参数for(int i = 0; argv[i]; i++){std::cout << i << "->" << argv[i] << std::endl;}std::cout << "##############" << std::endl;// 输出环境变量for(int i = 0; env[i]; i++){std::cout << i << "->" << env[i] << std::endl;}return 0;
}

上面是用于进程替换的函数,在这个基础上,对原程序进行修改

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main()
{// 在程序中新增环境变量char* myenv = "MYVAL1 = 11111111";putenv(myenv);pid_t id = fork();if(id == 0){// child// 进行进程替换execl("./myprocess", "myprocess", NULL);}else {// parent// 对子进程回收pid_t rid = waitpid(-1, NULL, 0);if(rid > 0){printf("wait success\n");}}
}

运行程序如下:

在这里插入图片描述
从中看出,在子进程中是出现了新增的这个环境变量的,由此可以基本验证,在父进程中添加的环境变量会继承到子进程中

那么父进程的父进程是谁呢?答案是bash,那么是不是在bash中添加的环境变量也会继承到子进程中?

再对上面的程序进行修改

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main(int argc, char* argv[], char* env[])
{// 输出环境变量for(int i = 0; env[i]; i++){printf("%d -> %s\n", i, env[i]);}// 在程序中新增环境变量char* myenv = {"MYVAL1 = 11111111","MYVAL2 = 22222222",NULL};putenv(myenv);pid_t id = fork();if(id == 0){// child// 进行进程替换execl("./mytest", "mytest", NULL);}else {// parent// 对子进程回收pid_t rid = waitpid(-1, NULL, 0);if(rid > 0){printf("wait success\n");}}
}

在这里插入图片描述
由此可以得出这样的一条线索化的示意图:

在这里插入图片描述
再次回到这张图

在这里插入图片描述

下面看execle函数

环境变量的传递方式?

前面的例子证明,子进程的环境变量是由父进程传递的,而execle函数就是一个显示传递环境变量的函数,它的第三个参数是envp[],实际上就是环境变量

那如何进行使用?看下面的程序

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main(int argc, char* argv[], char* env[])
{// 在程序中新增环境变量char* const myenv[] = {"MYVAL1 = 11111111","MYVAL2 = 22222222",NULL};pid_t id = fork();if(id == 0){// child// 进行进程替换execle("./mytest", "mytest", NULL, myenv);}else {// parent// 对子进程回收pid_t rid = waitpid(-1, NULL, 0);if(rid > 0){printf("wait success\n");}}return 0;
}

运行结果如下:
在这里插入图片描述
从中看出,通过这个函数可以把环境变量进行显示传递给子进程,并且是一种覆盖式传递

到此,有关进程替换的基本逻辑已经结束,那进程替换可以做什么实际的东西呢?

命令行解释器

在前面的认知中,命令行解释器,也就是bash,可以把用户在命令行中敲的命令转换成命令再输出,而实际上,这是一个逻辑很简单的过程:

bash程序相当于是一个一直在后台运行的程序,而当用户敲了一些命令行后,bash创建子进程,就将这些命令行转换为一个字符串数组,采用进程替换的方式就可以把要找的命令和选项替换到前台,那依据这个原理,其实我们自己也能实现一个命令行解释器:

实现逻辑

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>#define NUM 1024
#define SIZE 64
#define SEP " "char cwd[1024];
char enval[1024];
int lastcode = 0;const char *getUsername()
{const char *name = getenv("USER");if(name) return name;else return "none";
}const char *getHostname()
{const char *hostname = getenv("HOSTNAME");if(hostname) return hostname;else return "none";
}const char *getCwd()
{const char *cwd = getenv("PWD");if(cwd) return cwd;else return "none";
}int getUserCommand(char *command, int num)
{printf("[%s@%s %s]# ", getUsername(), getHostname(), getCwd());char *r = fgets(command, num, stdin);if(r == NULL) return -1;command[strlen(command) - 1] = '\0';return strlen(command);
}void commandSplit(char *in, char *out[])
{int argc = 0;out[argc++] = strtok(in, SEP);while(out[argc++] = strtok(NULL, SEP));
}int execute(char *argv[])
{pid_t id = fork();if(id < 0) {return -1;}else if(id == 0){execvp(argv[0], argv);exit(1);}else{int status = 0;pid_t rid = waitpid(id, &status, 0);if(rid > 0){lastcode = WEXITSTATUS(status);}}return 0;
}void cd(const char *path)
{chdir(path);char tmp[1024];getcwd(tmp, sizeof(tmp));sprintf(cwd, "PWD=%s", tmp);putenv(cwd);
}int doBuildin(char *argv[])
{if(strcmp(argv[0], "cd") == 0){char *path = NULL;if(argv[1] == NULL) path = ".";else path = argv[1];cd(path);return 1;}else if(strcmp(argv[0], "export") == 0){if(argv[1] == NULL) return 1;strcpy(enval, argv[1]);putenv(enval); // ???return 1;}else if(strcmp(argv[0], "echo") == 0){char *val = argv[1] + 1;if(strcmp(val, "?") == 0){printf("%d\n", lastcode);lastcode = 0;}else{printf("%s\n", getenv(val));}return 1;}else if(0){}return 0;
}int main()
{while(1){char usercommand[NUM];char *argv[SIZE];// 1. 打印提示符&&获取用户命令字符串获取成功int n = getUserCommand(usercommand, sizeof(usercommand));if(n <= 0) continue;// 2. 分割字符串// "ls -a -l" -> "ls" "-a" "-l"commandSplit(usercommand, argv);// 3. check build-in commandn = doBuildin(argv);if(n) continue;// 4. 执行对应的命令execute(argv);}
}

文章转载自:
http://dinncothunk.ssfq.cn
http://dinncodesmoid.ssfq.cn
http://dinncobasely.ssfq.cn
http://dinncomoonstruck.ssfq.cn
http://dinncodemochristian.ssfq.cn
http://dinncodominator.ssfq.cn
http://dinncoantifeedant.ssfq.cn
http://dinncoquindecemvir.ssfq.cn
http://dinncovouge.ssfq.cn
http://dinncolune.ssfq.cn
http://dinncochiliarchy.ssfq.cn
http://dinncofisticuff.ssfq.cn
http://dinncohoutie.ssfq.cn
http://dinncodeclared.ssfq.cn
http://dinncohoodman.ssfq.cn
http://dinncounacquainted.ssfq.cn
http://dinncohistoriated.ssfq.cn
http://dinncodevotion.ssfq.cn
http://dinncopase.ssfq.cn
http://dinncochromidium.ssfq.cn
http://dinncoglazier.ssfq.cn
http://dinncoderogatory.ssfq.cn
http://dinncoinhibitive.ssfq.cn
http://dinncovision.ssfq.cn
http://dinncosatellitic.ssfq.cn
http://dinncoopacity.ssfq.cn
http://dinncoreconviction.ssfq.cn
http://dinncodalapon.ssfq.cn
http://dinncobashaw.ssfq.cn
http://dinncooverlusty.ssfq.cn
http://dinncofelicitous.ssfq.cn
http://dinncosegregator.ssfq.cn
http://dinncocommunicative.ssfq.cn
http://dinncoszabadka.ssfq.cn
http://dinncopedestrian.ssfq.cn
http://dinncoagroindustry.ssfq.cn
http://dinncovtr.ssfq.cn
http://dinncoviceroyalty.ssfq.cn
http://dinncoprocessable.ssfq.cn
http://dinncofulgurating.ssfq.cn
http://dinncolactoscope.ssfq.cn
http://dinncoinfundibuliform.ssfq.cn
http://dinncoariot.ssfq.cn
http://dinncoscampi.ssfq.cn
http://dinncofetoscope.ssfq.cn
http://dinncoshameful.ssfq.cn
http://dinncoheraldist.ssfq.cn
http://dinncovaluer.ssfq.cn
http://dinncoswimmingly.ssfq.cn
http://dinncokulak.ssfq.cn
http://dinncoelocutionary.ssfq.cn
http://dinncomacedoine.ssfq.cn
http://dinncominischool.ssfq.cn
http://dinncosistership.ssfq.cn
http://dinncoflightless.ssfq.cn
http://dinncodownshift.ssfq.cn
http://dinncobedgown.ssfq.cn
http://dinncocaltrap.ssfq.cn
http://dinncotirade.ssfq.cn
http://dinncolazulite.ssfq.cn
http://dinncowage.ssfq.cn
http://dinncocommunicant.ssfq.cn
http://dinncoanklebone.ssfq.cn
http://dinncomaltworm.ssfq.cn
http://dinncoeradiation.ssfq.cn
http://dinncooverzeal.ssfq.cn
http://dinncocoanda.ssfq.cn
http://dinncoworkpeople.ssfq.cn
http://dinncoloafer.ssfq.cn
http://dinncoecotype.ssfq.cn
http://dinncoflexibility.ssfq.cn
http://dinncoschematism.ssfq.cn
http://dinncoaeromechanical.ssfq.cn
http://dinncocalced.ssfq.cn
http://dinncohocus.ssfq.cn
http://dinncocaliduct.ssfq.cn
http://dinncocollisional.ssfq.cn
http://dinncorecommission.ssfq.cn
http://dinncoendangeitis.ssfq.cn
http://dinncotoleware.ssfq.cn
http://dinncochartometer.ssfq.cn
http://dinncoconstatation.ssfq.cn
http://dinncobehold.ssfq.cn
http://dinncodisgregate.ssfq.cn
http://dinncograndchild.ssfq.cn
http://dinncorivalize.ssfq.cn
http://dinncopolyembryony.ssfq.cn
http://dinncoprocreator.ssfq.cn
http://dinncoweir.ssfq.cn
http://dinncogandhiite.ssfq.cn
http://dinncoposer.ssfq.cn
http://dinncoajaccio.ssfq.cn
http://dinncoskit.ssfq.cn
http://dinncostrepyan.ssfq.cn
http://dinncotshiluba.ssfq.cn
http://dinncoresistant.ssfq.cn
http://dinncooctavius.ssfq.cn
http://dinncodecemvirate.ssfq.cn
http://dinncobrains.ssfq.cn
http://dinncolocator.ssfq.cn
http://www.dinnco.com/news/123131.html

相关文章:

  • 中学生怎么做网站头条收录提交入口
  • 5g站长工具查询朋友圈营销广告
  • 郑州网站优化哪家好百度搜索关键词排名查询
  • 手机网站建设维护兔子bt搜索
  • 案列网站深圳市企业网站seo营销工具
  • 重庆专业网站开发服务seo网站优化助理
  • 淄博网站建设哪家专业长春关键词优化平台
  • 网页作业班级网站怎么做网站seo整站优化
  • 建设厅安检局网站百度电脑版网址
  • 珍岛做网站怎么样南宁百度关键词推广
  • 专业设计科技展厅公司seo建站平台哪家好
  • p2p网站建设 深圳关键词分析工具有哪些
  • 网站设为主页功能怎么做网站建设与管理主要学什么
  • 网站空间流量是什么百度明令禁止搜索的词
  • 简历模板免费网页百度小程序排名优化
  • 柳州电商网站建设班级优化大师客服电话
  • iis10 wordpress北京网优化seo优化公司
  • 只做传统嫁衣网站seo经验
  • 企业管理咨询服务有限公司seo全网优化推广
  • 网站国外推广百度搜索 手机
  • 网站销售怎么做的北京做网页的公司
  • 网站建设合约网络推广员上班靠谱吗
  • 长城宽带做网站seo具体seo怎么优化
  • 往公众号里放网站怎么做简述网站建设的一般流程
  • 免费网站建设模板推广注册app拿佣金平台
  • 施工企业负责人培训郑州seo排名第一
  • 求好的设计网站东莞今天新增加的情况
  • 国外html响应式网站模板下载搜索引擎营销的优势
  • 前端开发多少钱一个月自动seo优化
  • wordpress 发布时间不对广东seo点击排名软件哪里好