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

诸城手机网站建设网站怎么更新内容

诸城手机网站建设,网站怎么更新内容,做音乐网站,企业网站建设 厦门✅1主页#xff1a;#xff1a;我的代码爱吃辣 #x1f4c3;2知识讲解#xff1a;Linux——进程替换 ☂️3开发环境#xff1a;Centos7 #x1f4ac;4前言#xff1a;我们创建子进程的目的是什么#xff1f;想让子进程帮我们执行特定的… ✅1主页我的代码爱吃辣 2知识讲解Linux——进程替换 ☂️3开发环境Centos7 4前言我们创建子进程的目的是什么想让子进程帮我们执行特定的任务。那么如何让子进程去执行一段新的代码呢 一.背景 二.子进程程序替换 三.替换函数 1.execv 2.execlp 3.execle 4.命名理解 四.实现minishell 一.背景 我们创建子进程的目的是什么想让子进程帮我们执行特定的任务。 1.让子进程执行父进程的一部分代码 2.如果子进程指向一个全新的代码呢这就是进程的程序替换。 见一见单进程版本进程替换即父进程指向一个全新的代码: 隆重介绍一个接口 int execl(const char *path, const char *arg, ...); path替换程序的路径。arg如何执行该程序。可变参数如何执行该程序的参数等。 测试代码 #include stdio.h #include unistd.hint main() {printf(--------------------begin-------------\n);execl(/usr/bin/ls, ls, -a, -l, NULL);printf(--------------------end---------------\n);return 0; } 注意 我们想替换的程序 ls -a -l。ls 的路径:/usr/bin/ls。-a -l 时ls命令的参数。最后结束要以NULL结尾。 测试结果 注意 进程替换以后我们并没有看到我们源代码里面的最后一个打印。原因是程序在替换以后在后续执行完ls的代码就会退出了不会回到execl调用后面。 二.子进程程序替换 创建好子进程让子进程调用execl让子进程去执行替换的程序。 用fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并未改变。 测试代码 #include stdio.h #include unistd.h #include sys/types.h #include sys/wait.hint main() {int status;printf(--------------------begin-------------\n);pid_t pid fork();if (pid 0){// 我们想替换的程序 ls -a -l// ls 的路径:/usr/bin/ls//-a -l 时ls命令的参数// 最后结束要以NULL结尾execl(/usr/bin/ls, ls, -a, -l, NULL);}waitpid(-1, status, 0);//阻塞等待子进程退出。if (WIFEXITED(status))printf(子进程退出,退出码:%d\n, WEXITSTATUS(status));elseprintf(子进程异常,收到信号%d\n, status 0x7F);printf(--------------------end---------------\n);return 0; } 测试结果 进程替换原理 当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动 例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并未改变。 三.替换函数 其实有六种以exec开头的函数,统称exec函数: #include unistd.hint execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg, ...,char *const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execve(const char *path, char *const argv[], char *const envp[]); 函数解释 这些函数如果调用成功则加载新的程序从启动代码开始执行,不再返回。如果调用出错则返回-1。所以exec函数只有出错的返回值而没有成功的返回值。 介绍其中几个 1.execv int execv(const char *path, char *const argv[]); path:程序所在的路径argv是一个指针数组数组每一个元素代表我们需要怎么执行程序。 测试代码 #include stdio.h #include unistd.h #include sys/types.h #include sys/wait.hint main() {int status;printf(--------------------begin-------------\n);pid_t pid fork();if (pid 0){// 我们想替换的程序 ls -a -l// ls 的路径:/usr/bin/ls//-a -l 时ls命令的参数// 最后结束要以NULL结尾char *argv[] {ls, -a, -l, NULL};execv(/usr/bin/ls, argv);}waitpid(-1, status, 0);if (WIFEXITED(status))printf(子进程退出,退出码:%d\n, WEXITSTATUS(status));elseprintf(子进程异常,收到信号%d\n, status 0x7F);printf(--------------------end---------------\n);return 0; } 测试结果 2.execlp int execlp(const char *file, const char *arg, ...); file:程序名称。后续参数需要怎么执行程序。不需要给出程序路径execlp会自己到环境变量中找对应的程序。 测试代码 #include stdio.h #include unistd.h #include sys/types.h #include sys/wait.hint main() {int status;printf(--------------------begin-------------\n);pid_t pid fork();if (pid 0){// 我们想替换的程序 ls -a -l// ls 的路径:/usr/bin/ls//-a -l 时ls命令的参数// 最后结束要以NULL结尾execlp(ls, ls, -a, -l, NULL);}waitpid(-1, status, 0);if (WIFEXITED(status))printf(子进程退出,退出码:%d\n, WEXITSTATUS(status));elseprintf(子进程异常,收到信号%d\n, status 0x7F);printf(--------------------end---------------\n);return 0; }测试结果 3.execle int execle(const char *path, const char *arg, ...,char *const envp[]); path:程序路径。envp:是一个指针数组用来传输环境变量。arg:我们如何执行程序。 测试代码 #include stdio.h #include unistd.h #include sys/types.h #include sys/wait.hint main() {extern char **environ;int status;printf(--------------------begin-------------\n);pid_t pid fork();if (pid 0){// 我们想替换的程序 ls -a -l// ls 的路径:/usr/bin/ls//-a -l 时ls命令的参数// 最后结束要以NULL结尾execle(./newdir/otherproc, otherproc, NULL, environ);}waitpid(-1, status, 0);if (WIFEXITED(status))printf(子进程退出,退出码:%d\n, WEXITSTATUS(status));elseprintf(子进程异常,收到信号%d\n, status 0x7F);printf(--------------------end---------------\n);return 0; } newdir/otherproc.cc #include iostream #include unistd.h #include stdlib.husing namespace std;int main() {cout hello world hello: getenv(hello) endl;cout hello world hello: getenv(hello) endl;cout hello world hello: getenv(hello) endl;return 0; } 测试结果 4.命名理解 这些函数原型看起来很容易混,但只要掌握了规律就很好记。 l(list) : 表示参数采用列表v(vector) : 参数用数组p(path) : 有p自动搜索环境变量PATHe(env) : 表示自己维护环境变量  exec调用总结 #include unistd.h int main() {char *const argv[] {ps, -ef, NULL};char *const envp[] {PATH/bin:/usr/bin, TERMconsole, NULL};execl(/bin/ps, ps, -ef, NULL);// 带p的可以使用环境变量PATH无需写全路径execlp(ps, ps, -ef, NULL);// 带e的需要自己组装环境变量execle(ps, ps, -ef, NULL, envp);execv(/bin/ps, argv);// 带p的可以使用环境变量PATH无需写全路径execvp(ps, argv);// 带e的需要自己组装环境变量execve(/bin/ps, argv, envp);exit(0); } 事实上,只有execve是真正的系统调用,其它五个函数最终都调用 execve,所以execve在man手册 第2节,其它函数在man手册第3节。这些函数之间的关系如下图所示。 四.实现minishell 用下图的时间轴来表示事件的发生次序。其中时间从左向右。shell由标识为sh的方块代表它随着时间的流逝从左向右移动。shell从用户读入字符串ls。shell建立一个新的进程然后在那个进程中运行ls程序并等待那个进程结束。 然后shell读取新的一行输入建立一个新的进程在这个进程中运行程序 并等待这个进程结束。 所以要写一个shell需要循环以下过程: 1. 获取命令行 2. 解析命令行 3. 建立一个子进程fork 4. 替换子进程execvp 代码 #include stdio.h #include string.h #include stdlib.h #include unistd.h #include sys/types.h #include sys/wait.h #include assert.h#define SEP #define MAX 1024 #define MAX_SUB 64// 分割命令ls -a -l -- ls,-a -l void split(char buff[], char *subbuff[]) {assert(buff);assert(subbuff);int i 0;subbuff[i] strtok(buff, SEP);while (subbuff[i] strtok(NULL, SEP)) // 如果没有找到分割就会返回NULL; }// 代码测定打印命令 void debug(char *subbuff[]) {int i 0;while (subbuff[i]){printf(%s\n, subbuff[i]);} }// 显示环境变量 void showenv() {extern char **environ;for (int i 0; environ[i]; i){printf([%d]:%s\n, i, environ[i]);} }int main() {int status 0; // 退出码参数char myenv[32][512] {0}; // 用户自定义环境变量int index_env 0;int last_exit 0;while (1){char buff[MAX] {0}; // 存储命令行输入的命令字符串char *subbuff[MAX_SUB] {NULL};printf(wq[aliyum]$:);fflush(stdout);// 1.获得命令字符串fgets(buff, sizeof(buff), stdin);// 2.解析命令// ls -a -l\n\0,strlen9,index(\n)8,去除回车。buff[strlen(buff) - 1] \0;// 分割字符串split(buff, subbuff);// 处理内建命令// 注意cd,export,env,echo,等命令都是内建命令即这些命令不能创建子进程执行只能bash自己执行if (strcmp(subbuff[0], cd) 0){if (subbuff[1] ! NULL)chdir(subbuff[1]);continue;}else if (strcmp(subbuff[0], export) 0){if (subbuff[1] ! NULL){// 这里不能将subbuff[1]直接导入环境变量因为环境变量表存储的都是指针必须使用一个单独空间strcpy(myenv[index_env], subbuff[1]);putenv(myenv[index_env]);}continue;}else if (strcmp(subbuff[0], env) 0){showenv();continue;}else if (strcmp(subbuff[0], echo) 0){// echo $PATHif (subbuff[1][0] $){if (subbuff[1][1] ?) // echo $?//最近一次推出吗{printf(%d\n, last_exit);}else // 提取环境变量{// PATHchar *subenv subbuff[1] 1;char *get_env getenv(subenv);if (get_env ! NULL){printf(%s%s\n, subenv, get_env);}}}continue;}if (strcmp(subbuff[0], ls) 0){int comm_index 0;while (subbuff[comm_index]){comm_index;}// 增加高亮subbuff[comm_index] --colorauto;}// 3.创建子进程pid_t pid fork();assert(pid 0);if (pid 0) // 子进程{extern char **environ;// 4. 程序替换execve(subbuff[0], subbuff, environ);}// // 测试// debug(subbuff);waitpid(pid, status, 0);if (WIFEXITED(status)){// 子进程退出设置退出码last_exit WEXITSTATUS(status);}}return 0; }
http://www.zqtcl.cn/news/137740/

相关文章:

  • 网站搭建系统都有哪些丽水网站开发
  • 网站设计包含哪些技术外行怎么做网站
  • 网站建设运营知识推广软文平台
  • 营销型网站建设用途网站 文件夹结构
  • 制作网站建设策划方案cosy主题wordpress
  • 网站建设服务联享科技net和cn哪个做网站好
  • 深圳网站制作公司哪家好艺考培训学校
  • 潍坊网站的公司电话html网站开发基础
  • 网站模板样式做地图特效的网站
  • 商标查询官方网站有没有免费找客户的软件
  • 网站开发及服务合同行业网站名称
  • 网站建设费包括什么建筑设计领域
  • 网站建设 信科网络建行网站会员注册用户名
  • 网站建设的什么是开发实施注意什么网站开发实用技术pdf
  • 网站设计的资质叫什么贵阳网站建设咨询
  • 郑州哪家公司做网站怎么做自己的销售网站
  • 北大青鸟教网站开发吗中国电信 网站备案
  • 网站目录结构图wordpress ftp连接不上
  • 使用php做的网站有哪些网站备案密码重置申请表
  • php网站开发好找工作吗一叶子电子商务网站建设策划书
  • 运营好还是网站开发好购买域名后怎样建公司官网
  • 优秀设计网站推荐晋江市住房和城乡建设局网站
  • 杭州市区网站制作单位青海公路建设服务网站
  • 大型门户网站建设美丽杭州房价
  • 素材下载解析接口网站开发网站关键词热度
  • 山东seo推广网站建设新乡手机网站建设官网
  • 网站定制公司报价wordpress清新模板下载
  • 斗鱼网站开发是用什么语言东莞人才网智通
  • 淘宝上网站建设为啥这么便宜自己如何建设个网站
  • 做网站判多少年滦南网站建设