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

做一个商城网站需要多少钱百度指数官网移动版

做一个商城网站需要多少钱,百度指数官网移动版,河南响应式官网建设哪里好,wordpress step2fork与进程等待 引言forkfork创建子进程的过程写时拷贝 进程等待waitwaitpid阻塞等待与非阻塞轮询 总结 引言 fork函数在Linux中是一个非常重要的系统调用接口!它用于在当前的已有进程中创建一个新的进程(子进程)。再由父子进程并发地执行不…

fork与进程等待

  • 引言
  • fork
    • fork创建子进程的过程
    • 写时拷贝
  • 进程等待
    • wait
    • waitpid
    • 阻塞等待与非阻塞轮询
  • 总结

引言

fork函数在Linux中是一个非常重要的系统调用接口!它用于在当前的已有进程中创建一个新的进程(子进程)。再由父子进程并发地执行不同地代码块,就相当于父子进程给子进程派了一块代码让他去执行。
在子进程执行完代码块后,应该给父进程一个发聩,这个时候就需要父进程去等待子进程,然后回收子进程,以免形成内存泄漏等问题。
接下来就来详细地介绍fork函数以及进程等待:

fork

fork可以从当前进程中创建一个新进程,已有的进程就是父进程,新进程就是子进程,父进程与子进程并发地执行不同的代码块:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>int main()
{pid_t rid = 0;rid = fork();if(rid < 0){perror("fork:");}else if(rid == 0) //子进程{printf("i am child\n");}else //父进程(rid > 0){printf("i am parent\n");}return 0;
}

在这里插入图片描述

fork创建子进程的过程

在创建子进程时:
操作系统会给子进程分配新的内存块与内核数据结构;
然后父进程的部分数据被拷贝到子进程;
然后子进程会被操作系统添加到调度列表中;
最后会分别返回值给父子进程,对父进程返回子进程的pid,对子进程返回0

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>int main()
{pid_t rid = 0;int a = 10;printf("before: %d\n", a); rid = fork(); //创建子进程++a;printf("rid: %d: after: %d\n", rid, a);if(rid == 0) //子进程{++a;printf("child: %d\n", a);}else if(rid > 0) //父进程{a+=2;printf("parent: %d\n", a);}return 0;
}

在这里插入图片描述

在这段代码中,定义了一个变量a,我们可以通过这个变量a的变化来验证fork创建新进程的过程:

首先打印了一遍before,此时a的值为10。说明这时只有父进程一个执行流在执行代码;
然后发现after打印了两遍,两遍a的值都是11,由父子进程分别打印。这首先说明fork之后有父子进程两个执行流在执行代码。并且在创建子进程时,子进程获取到了父进程之前的变量a,所以两个执行流在这里打印出的值都是11;
然后if_else对代码进行了分流,父进程打印a+=2后的结果13,子进程打印a++后的结果12。说明进程之间是独立的,他们有自己的进程地址空间与页表,转化到不同的物理内存,对自己进程中数据的改变不会影响对方进程。
在这里插入图片描述
需要注意的是,父子进程的调度先后,完全由调度器决定

写时拷贝

通过上面的介绍,我们知道在创建子进程时,父进程要将自己的数据拷贝给子进程。但是对于代码或者子进程没有进行修改的数据,在物理内存中在将这些数据存储一份显然是浪费内存空间的。

所以父子进程在拷贝数据是是以写时拷贝的方式来进行的
创建子进程时,父进程将自己的进程地址空间与页表拷贝给子进程,即父子进程的数据段和代码段在通过页表映射后指向同一块物理内存:
在这里插入图片描述

如果子进程中会对一些数据做修改时,就会发生写时拷贝。即将要修改的数据拷贝一份到另外的物理空间,页表的转化关系也指向这块新的物理空间,再由子进程对其进行修改:
在这里插入图片描述

通过这样的写时拷贝的方式,就可以减少内存的浪费。

进程等待

子进程退出后,父进程应该获取子进程的退出状态,看看子进程是否正常退出,如果出现异常的错误码是什么;
父进程也应该回收子进程的资源,如果子进程的资源没有被回收,就会造成内存泄漏;
没有被父进程回收的子进程就会成为 “僵尸进程”,无法被杀死。

父进程可以通过waitwaitpid函数来对子进程进行等待:
在这里插入图片描述

wait

pid_t wait(int* status) 用于等待任一子进程
等待成功返回子进程pid,等待中出错返回-1,errno被设置;
参数为输出型参数,用于获取子进程的退出状态,由操作系统填充(若不关心返回状态,参数设为NULL即可)。

在等待结束后,可以通过 WIFEXITED(status);查看进程是否是正常退出,若为正常终止子进程,则为真;
通过 WEXITSTATUS(status);查看进程的退出码,若WIFEXITED返回真,提取子进程退出码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main()
{pid_t rid = 0;rid = fork();if(rid == 0) //子进程{printf("i am child\n");sleep(5);}else if(rid > 0) //父进程{printf("i am parent\n");int status = 0;pid_t ret = 0;ret = wait(&status); //等待if(WIFEXITED(status) != 0 && ret == rid) //如果等待成功打印子进程退出码{printf("child return :%d\n", WEXITSTATUS(status));}else{return 1;}}return 0;
}

在这里插入图片描述

waitpid

pid_t waitpid(pid_t pid, int *status, int options); 可以等待任一子进程,也可以用于等待指定子进程。等待成功返回子进程pid,等待中出错返回-1,errno被设置;

第一个参数pid表示指定要等待子进程的pid,若要等待任一子进程,则传参-1;
第二个参数为输出型参数,参数为输出型参数,用于获取子进程的退出状态,由操作系统填充(若不关心返回状态,参数设为NULL即可);
第三个参数为等待状态,有多种选项:当设置为WNOHANG:,表示若pid指定的子进程没有结束,则waitpid函数返回0,不予以等待(即非阻塞等待)。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>int main()
{pid_t rid = 0;rid = fork();if(rid == 0){printf("i am child\n");sleep(5);}else if(rid > 0){printf("i am parent\n");int status = 0;pid_t ret = 0;ret = waitpid(rid, &status, 0); //阻塞式等待pid为rid的子进程if(WIFEXITED(status) != 0 && ret == rid){printf("child return :%d\n", WEXITSTATUS(status));}else{return 1;}}return 0;
}

在这里插入图片描述

阻塞等待与非阻塞轮询

在使用waitwaitpid进行进程等待时,若子进程正在执行,父进程就会阻塞式的等待子进程执行结束,等待成功后再继续执行接下来的代码:

int main()
{pid_t rid = 0;rid = fork();if(rid == 0){int n = 10;while(n--){printf("i am child, %d\n", n);sleep(1);}}else if(rid > 0){int status = 0;pid_t ret = 0;ret = waitpid(rid, &status, 0); //阻塞式等待,成功等待子进程后才会执行后面的代码if(WIFEXITED(status) != 0 && ret == rid){printf("child return :%d\n", WEXITSTATUS(status)); }else{return 1;}}return 0;
}

在这里插入图片描述

但是如果父进程阻塞等待子进程的时间过长,就会影响代码的效率。
如果父进程在等子进程时发现子进程正在执行,父进程可以选择不阻塞等待,而是去执行别的代码,隔一段时间去看看子进程有没有退出。这样的非阻塞轮询的方式可以提高代码的效率:

非阻塞等待的方式可以通过waitpid函数的WNOHANG选项来实现:

int main()
{pid_t rid = 0;rid = fork();if(rid == 0){int n = 10;while(n--){printf("i am child, %d\n", n);sleep(1);}}else if(rid > 0){int status = 0;pid_t ret = 0;do //非阻塞轮询,当成功等待时终止循环{ret = waitpid(rid, &status, WNOHANG);if(ret == 0){printf("child is running, i do something...\n");sleep(1);}}while(ret == 0); if(WIFEXITED(status) != 0 && ret == rid){printf("child return :%d\n", WEXITSTATUS(status));}else{return 1;}}return 0;
}

在这里插入图片描述

这样就实现了父进程在等待子进程期间也可以做一些事,提高了效率(这里的父子进程是并发进行的,两个进程抢占显示器打印的先后是由调度器决定的,所以不是很按顺序属于正常)。

总结

到此,关于fork函数与进程等待的知识就介绍完了

如果大家认为我对某一部分没有介绍清楚或者某一部分出了问题,欢迎大家在评论区提出

如果本文对你有帮助,希望一键三连哦

希望与大家共同进步哦


文章转载自:
http://dinncoprefix.tpps.cn
http://dinncoichthyofauna.tpps.cn
http://dinncosinsemilla.tpps.cn
http://dinncocystotomy.tpps.cn
http://dinncoimpassably.tpps.cn
http://dinncosicanian.tpps.cn
http://dinncophonetics.tpps.cn
http://dinncofinisher.tpps.cn
http://dinncoosa.tpps.cn
http://dinncoducat.tpps.cn
http://dinncoareosystyle.tpps.cn
http://dinncoarchway.tpps.cn
http://dinncoafterschool.tpps.cn
http://dinncojordanian.tpps.cn
http://dinncorisque.tpps.cn
http://dinncointercultural.tpps.cn
http://dinncocorruption.tpps.cn
http://dinncocorposant.tpps.cn
http://dinncosymbolization.tpps.cn
http://dinncohyperbatic.tpps.cn
http://dinncoalgin.tpps.cn
http://dinncopleomorphous.tpps.cn
http://dinncojejuneness.tpps.cn
http://dinncoarea.tpps.cn
http://dinncomilligram.tpps.cn
http://dinncoinitializers.tpps.cn
http://dinncointuitional.tpps.cn
http://dinncoboycott.tpps.cn
http://dinncocuticolor.tpps.cn
http://dinncospigotty.tpps.cn
http://dinnconeuroleptic.tpps.cn
http://dinncoupperclassman.tpps.cn
http://dinncoblackfeet.tpps.cn
http://dinncocreasy.tpps.cn
http://dinncobonhomous.tpps.cn
http://dinncostagestruck.tpps.cn
http://dinncogerard.tpps.cn
http://dinncoradioelement.tpps.cn
http://dinncomicrocephalous.tpps.cn
http://dinncojester.tpps.cn
http://dinncoefflux.tpps.cn
http://dinncoluganda.tpps.cn
http://dinnconaples.tpps.cn
http://dinncoeyedrop.tpps.cn
http://dinncoscoresheet.tpps.cn
http://dinncodescendent.tpps.cn
http://dinncondr.tpps.cn
http://dinncolevelpeg.tpps.cn
http://dinncounguiculated.tpps.cn
http://dinncodovelike.tpps.cn
http://dinncopiat.tpps.cn
http://dinncoinseparable.tpps.cn
http://dinncodemophil.tpps.cn
http://dinncobattleground.tpps.cn
http://dinncoquarte.tpps.cn
http://dinncotransubstantiate.tpps.cn
http://dinncoadios.tpps.cn
http://dinncocruel.tpps.cn
http://dinnconuke.tpps.cn
http://dinncoskywalk.tpps.cn
http://dinncoquetzal.tpps.cn
http://dinncowore.tpps.cn
http://dinncopatternize.tpps.cn
http://dinncooecumenicity.tpps.cn
http://dinncoperiostitis.tpps.cn
http://dinncodensity.tpps.cn
http://dinncoidealize.tpps.cn
http://dinncokeelman.tpps.cn
http://dinncoirisher.tpps.cn
http://dinncointerlaced.tpps.cn
http://dinncouncharted.tpps.cn
http://dinncoanodontia.tpps.cn
http://dinncosomnambulance.tpps.cn
http://dinncogoalie.tpps.cn
http://dinncoreferenced.tpps.cn
http://dinncoenclave.tpps.cn
http://dinncoinveigher.tpps.cn
http://dinncoragabash.tpps.cn
http://dinncoericeticolous.tpps.cn
http://dinncoamende.tpps.cn
http://dinncopaddlesteamer.tpps.cn
http://dinncoparentally.tpps.cn
http://dinncopulverator.tpps.cn
http://dinncoberavement.tpps.cn
http://dinncocrater.tpps.cn
http://dinncocurvirostral.tpps.cn
http://dinncoshoes.tpps.cn
http://dinncocigarette.tpps.cn
http://dinncohibernation.tpps.cn
http://dinncomenad.tpps.cn
http://dinncobecquerel.tpps.cn
http://dinncopose.tpps.cn
http://dinncomeaningless.tpps.cn
http://dinncosloid.tpps.cn
http://dinncoexpansion.tpps.cn
http://dinncopretonic.tpps.cn
http://dinncoslosh.tpps.cn
http://dinncoproprieties.tpps.cn
http://dinncogoosegirl.tpps.cn
http://dinncocarbo.tpps.cn
http://www.dinnco.com/news/92897.html

相关文章:

  • 金华手机建站模板网络营销品牌推广公司
  • 揭阳做网站设计网站搜索优化价格
  • 江苏建设厅网站做网络推广费用
  • 网站建设运营期末考试附近广告公司联系电话
  • 企业网站申请流程南宁百度seo排名价格
  • django 做的网站免费注册网址
  • 在网站做推广要钱吗网络营销文案策划
  • 漳州网站开发免费发广告的平台有哪些
  • 直接点击链接就能玩的小游戏福州关键词排名优化
  • 福州外贸网站制作上海网站关键词排名
  • 太原富库网站建设网络广告电话
  • 什么网站可以做实验室郑州seo技术培训班
  • 一个主体可以备案几个网站seo优化一般包括哪些
  • wpbus-d4 wordpress theme优化网站排名费用
  • 做网站图片软件谷歌chrome浏览器官方下载
  • 专做项目报告的网站代发关键词包收录
  • 婚庆网站策划书网站推广系统方案
  • 微网站建设包含真正免费的网站建站平台
  • 建设网站及域名费用陕西网站设计
  • 手机网站返回跳转页面代码微博seo排名优化
  • 建设视频网站的视频源网站产品怎么优化
  • 顺德微网站建设站长工具的使用seo综合查询排名
  • 比较好的企业网站在线子域名二级域名查询工具
  • 移动网站怎么做360开户推广
  • 如何做电商网站首页淘宝网页版
  • 网站建设开发的主要流程google play谷歌商店
  • 中国国际空间站拒绝十个国家seo文章
  • 怎样做网站认证在线看crm系统
  • 做技术网站在背景图天津企业seo
  • 太原今日头条新闻最新seo排名哪家公司好