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

高唐做网站建设的公司厦门百度seo排名

高唐做网站建设的公司,厦门百度seo排名,上海网站建设天锐科技,盘锦做网站1.进程创建 1.1 fork函数 fork()通过复制调用进程来创建一个新进程。新进程称为子进程,是调用进程的精确副本 进程,但以下几点除外: 子进程有自己的PID,此PID与任何现有进程组的ID不匹配子进程的父进程ID…

1.进程创建

1.1 fork函数

        fork()通过复制调用进程来创建一个新进程。新进程称为子进程,是调用进程的精确副本
进程,但以下几点除外:

  • 子进程有自己的PID,此PID与任何现有进程组的ID不匹配
  • 子进程的父进程ID与父进程的进程ID相同。
  • 子进程没有继承父进程的内存锁
  • 进程资源利用率(getrusage(2))和CPU时间计数器(times(2))在子进程中重置为零
  • 子进程的挂起信号集最初为空
  • 子进程不能继承父进程的信号调整
  • 子进程不从父进程继承记录锁
  • 子进程不从父进程继承计时器
  • 子进程不继承父进程未完成的异步I/O操作,也不继承任何异步操作从它的父进程中获取同步I/O上下文

#include <unistd.h>
pid_t fork();
返回值:fork成功则子进程PID被返回给父进程,0被返回给子进程。失败,-1被返回给父进程,没有子进程创建。-- 给父进程返回子进程的pid,子进程返回0,是因为一个父进程可以有多个子进程,儿子进程只能有一个父进程。

 当一个进程调用fork之后,就有两个二进制代码相同的进程。而且它们都运行到相同的地方。但每个进程都将可执行自己的代码,看如下程序:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>int g_val = 0;
int main()
{pid_t id = fork();if(id < 0){perror("fork");return 0;}else if(id == 0){ //childg_val = 100;printf("child[%d]: %d : %p\n", getpid(), g_val, &g_val);}else{ //parentprintf("parent[%d]: %d : %p\n", getpid(), g_val, &g_val);}sleep(1);return 0;
}

执行结果如下: 

 

由结果可以得出,fork之后,父子进程各自执行自己的代码块

fork调用失败的原因: 系统中有太多的进程 实际用户的进程数超过了限制

1.2 写时拷贝

        通常,父子代码共享,父子在不写入时,数据也是共享的,当任意一方试图写入,便以写时拷贝的方式各自一份副 本。具体见下图:

2.进程终止

进程退出场景:

  • 代码运行结束,结果正确
  • 代码运行结束,结果错误
  • 代码异常终止

 进程退出方式:

  • 从main返回 return n; 
    执行 return n 等同于执行 exit(n), 因为调用 main 的运行时函数会将 main 的返
    回值当做 exit 的参数。
  • 在任意地方调用exit(errno) -- 库函数,终止进程,主动刷新缓冲区
  • _exit() -- 系统调用,终止进程不会刷新缓冲区
  • ctrl + c -- 信号终止

 echo $?  :看记录最后一个进程在命令行执行完毕时对应的退出码

#include <unistd.h>
void _exit(int status);

参数:status定义了进程的终止状态,父进程通过wait来获取该值 虽然statusint,但是仅有低8位可以被父进程所用

#include <unistd.h>
void exit(int status);

exit最后也会调用exit, 但在调用exit之前,还做了其他工作:

1. 执行用户通过 atexit on_exit 定义的清理函数。
2. 关闭所有打开的流,所有的缓存数据均被写入
3. 调用 _exit

3.进程等待 

        检测子进程推出信息,将子进程的退出信息通过status拿回来。

3.1 进程等待必要性

  • 之前讲过,子进程退出,父进程如果不管不顾,就可能造成 僵尸进程 ,进而造成内存泄漏。
  • 另外,进程一旦变成僵尸状态,那就刀枪不入, kill -9 也无能为力
  • 最后,父进程派给子进程的任务完成的如何,我们需要知道。
  • 父进程通过进程等待的方式,回收子进程资源,获取子进程退出信息

3.2 进程等待的方法

wait方法

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int* status);
参数:获取子进程的退出状态

返回值:成功,返回终止子进程的pid,失败返回-1

 waitpid方法

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(pid_t pid, int *status, int options);
参数:pid:pid=-1,等待任意子进程,与wait等效;pid>0,等待其进程id与pid相等的子进程

           status:输出型参数,拿到子进程的退出结果。-- 有自己的为图结构,只关心低16
                        个比特位()0-15,
                        次低八位(8-15):进程退出状态(结果是否正确) -- 设为:(status>>8) &0XFF
                        低七位(0-7):进程终止信号(是否正常结束) -- 设为:status&0X7F
           options:先设为0
返回值:成功,返回收集到的子进程的pid

              若无可以收集的一推出的子进程,return 0;

               若调用出错,返回-1,这时,errno会被设置为相应的值,以指示错误所在。

 WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)

WEXITSTATUS(status): WIFEXITED非零,提取子进程退出码。(查看进程的退出码)        

 下面看一段示例代码:

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>int main()
{pid_t id = fork();if(id == 0){int cnt = 5;while(cnt){printf("我是子进程pid:%d, 父进程:%d, cnt = %d\n", getpid(), getppid(), cnt--);sleep(1);}// int d = 10/0;exit(10);}sleep(7);int status = 0;pid_t ret = waitpid(id, &status, 0);if(id > 0){printf("wait success: %d, sig_number: %d, child_exit_code: %d\n", ret, (status & 0x7F), (status>>8)&0xFF);}sleep(5);return 0;
}

结果下图所示:

第一个图为我执行kill -9 22287得到的结果,进程收到9号信号,进程终止

第二个图为程序正常运行结束的结果。

阻塞等待和非阻塞等待:

        阻塞等待:子进程未推出时,父进程不调用一直等待直到子进程结束。

        非阻塞等待:在与逆行期间一直询问子进程是否结束 waitpid()函数第三个参数设置为WNOHANG。
非阻塞等待的好处:

  • 不会占用父进程的所有精力,可以在轮询期间做别的事情

下面为非阻塞式等待的代码:

#include <stdio.h> 
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{pid_t pid;pid = fork();if(pid < 0){printf("%s fork error\n",__FUNCTION__);return 1;}else if( pid == 0 ){ //childprintf("child is run, pid is : %d\n",getpid());sleep(5);exit(1);} else{int status = 0;pid_t ret = 0;do{ret = waitpid(-1, &status, WNOHANG);//非阻塞式等待if( ret == 0 ){printf("child is running\n");}sleep(1);}while(ret == 0);if( WIFEXITED(status) && ret == pid ){printf("wait child 5s success, child return code is :%d.\n",WEXITSTATUS(status));}else{printf("wait child failed, return.\n");return 1;}}return 0;
}

可以看到,父进程在等待子进程的同时还会执行打印语句。若为非阻塞等待,会一直卡在等待的环节。 

总结:

进程等待:

  • 是什么? -- 通过系统调用,让父进程等待子进程的方式
  • 为什么? -- 释放子进程的僵尸状态,获取子进程状态
  • 怎么等? -- wait/waitpid  阻塞等待/非阻塞等待

4.进程程序替换

        相当于用自己的程序把别人的程序跑起来,支持不同语言(任何后端语言)的替换

4.1 创建子进程的目的

  • 想让子进程执行父进程代码的一部分 -- 执行父进程对应的磁盘代码中的一部分
  • 想让子进程执行一个全新的程序 -- 让紫禁城想办法,家在磁盘上指定的程序,执行新程序的代码和数据

4.2 替换原理

        就是将指定程序的代码和数据加载带指定的位置,进程替换时并没有创建新的进程。在子进程中调用execl函数并不会影响父进程的执行(进程具有独立性)。OS感觉到替换后,则进行写时拷贝,重新分配内存--子进程通过页表重新映射到新的内存。

4.3 替换函数

#include <unistd.h>`
int execl(const char *path, const char *arg, ...);
参数:path:路径
            arg:在命令行怎么执行就怎么传参
...:可变参数列表
返回值:执行失败返回-1,并继续执行源代码,成功不会返回值。
int execlp(const char *file, const char *arg, ...);
p:代表的就是如何找程序的功能,带p字符的函数,不用告诉我替换程序的路径,只需要知道时谁,会自动在环境变量PATH中进行可执行程序的查找。
int execle(const char *path, const char *arg, ...,char *const envp[]);
int execv(const char *path, char *const argv[]);
v
:vector,可以将所有的可执行参数,放入到数组(必须以空作为结束)中,不用可变参数传参
int execvp(const char *file, char *const argv[]);
 
int execve(const char *path, char *const argv[], char *const envp[]); -- 允许自定义环境变量

所有的execl* 系列的接口都必须以NULL结尾 

下面为各个接口的演示:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main()
{// 用我们的程序将别人的程序执行起来// .c -> exe -> load -> process -> 运行 -> 执行我们现在所写的代码printf("process is running...\n");// 只要是个函数,掉用就有可能失败 就是没有替换成功 继续执行下面的原代码// 只有错误时会返回,返回值为-1execl("/usr/bin/ls"/*要执行哪个程序*/, "ls","-a", "-l", "--color=auto", NULL/*你想怎么执行*/);  // 所有的execl* 系列的接口都必须以NULL结尾printf("execl\n");// 为何下面的printf没有执行呢? printf是在execl之后的,execl执行完毕后,代码已经完全被替换 开始新的程序的代码了printf("process running done...\n");return 0;
}

 这个代码使用execl接口将我们的程序替换为了命令行命令,ls -a -l,结果如下:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main(int argc, char *argv[])
{printf("process is running\n!");pid_t id = fork();assert(id!=-1);if(id == 0){sleep(1);execlp("ls", "ls","-a", "-l", NULL);printf("原子进程!\n");exit(10);}// 下面为父进程的代码,子进程的替换不会影响父进程的代码int status = 0;int ret = waitpid(id, &status, 0);if(ret > 0) printf("wait success: exit code:%d, sig:%d\n", (status>>8) & 0xFF, status & 0x7F);return 0;
}

 有这段代码也可以看出,子进程在进行程序替换时,是不会影响父进程的

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main(int argc, char *argv[])
{printf("process is running\n!");pid_t id = fork();assert(id!=-1);if(id == 0){sleep(1);char *arv_[] = { "ls","-a", "-l", NULL };execv("/usr/bin/ls", arv_);exit(10);}// 下面为父进程的代码,子进程的替换不会影响父进程的代码int status = 0;int ret = waitpid(id, &status, 0);if(ret > 0) printf("wait success: exit code:%d, sig:%d\n", (status>>8) & 0xFF, status & 0x7F);return 0;
}

其他几个与上面的类似,这里就不一一列举了。除了可以替换系统命令,还可以将程序替换为自己的程序,下面用一个接口execvp来演示。

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main(int argc, char *argv[])
{printf("process is running\n!");pid_t id = fork();assert(id!=-1);if(id == 0){sleep(1);char *arv_[] = { "mycpp", NULL };execvp("./mybin", arv_);exit(10);}// 下面为父进程的代码,子进程的替换不会影响父进程的代码int status = 0;int ret = waitpid(id, &status, 0);if(ret > 0) printf("wait success: exit code:%d, sig:%d\n", (status>>8) & 0xFF, status & 0x7F);return 0;
}

 


文章转载自:
http://dinncoscoleces.tpps.cn
http://dinncohegelian.tpps.cn
http://dinncopyrolyzate.tpps.cn
http://dinncoglycosaminoglycan.tpps.cn
http://dinncoinstruction.tpps.cn
http://dinncooutrecuidance.tpps.cn
http://dinncowerner.tpps.cn
http://dinncopus.tpps.cn
http://dinncogoal.tpps.cn
http://dinncokithira.tpps.cn
http://dinncoriouw.tpps.cn
http://dinncosubornative.tpps.cn
http://dinncopursuivant.tpps.cn
http://dinncoundisguised.tpps.cn
http://dinncoamoretto.tpps.cn
http://dinncomilitary.tpps.cn
http://dinncoundeclined.tpps.cn
http://dinncomower.tpps.cn
http://dinncogorgonize.tpps.cn
http://dinncoranchero.tpps.cn
http://dinncoantipoverty.tpps.cn
http://dinncoconstructional.tpps.cn
http://dinnconighthawk.tpps.cn
http://dinncofluorochrome.tpps.cn
http://dinncovagrant.tpps.cn
http://dinncoriboflavin.tpps.cn
http://dinncowonderland.tpps.cn
http://dinncopopedom.tpps.cn
http://dinncoyokemate.tpps.cn
http://dinncotemperately.tpps.cn
http://dinncoadpress.tpps.cn
http://dinncosina.tpps.cn
http://dinncodeltoideus.tpps.cn
http://dinncopenetrable.tpps.cn
http://dinncodanaidean.tpps.cn
http://dinncogibing.tpps.cn
http://dinncokatharevousa.tpps.cn
http://dinncomaror.tpps.cn
http://dinncocomprehendingly.tpps.cn
http://dinncoseadog.tpps.cn
http://dinncohireable.tpps.cn
http://dinncojuxtaterrestrial.tpps.cn
http://dinncobondholder.tpps.cn
http://dinncoguitar.tpps.cn
http://dinncoprojectionist.tpps.cn
http://dinncosmudgily.tpps.cn
http://dinncoremodel.tpps.cn
http://dinncomoly.tpps.cn
http://dinncomaraud.tpps.cn
http://dinncoeuglobulin.tpps.cn
http://dinncocaponize.tpps.cn
http://dinncoresponsion.tpps.cn
http://dinncocoprozoic.tpps.cn
http://dinncoallegretto.tpps.cn
http://dinncoavigator.tpps.cn
http://dinncodetonation.tpps.cn
http://dinncodemarcation.tpps.cn
http://dinncominibudget.tpps.cn
http://dinncointragenic.tpps.cn
http://dinncokomiteh.tpps.cn
http://dinncotatou.tpps.cn
http://dinncothirtieth.tpps.cn
http://dinncobarratry.tpps.cn
http://dinncoacosmist.tpps.cn
http://dinncoforeignism.tpps.cn
http://dinncotaler.tpps.cn
http://dinncoaguish.tpps.cn
http://dinncosexagesima.tpps.cn
http://dinncodecalog.tpps.cn
http://dinncoemissary.tpps.cn
http://dinncoalibility.tpps.cn
http://dinncodisme.tpps.cn
http://dinncotyphlosole.tpps.cn
http://dinncocantabank.tpps.cn
http://dinncocosmonaut.tpps.cn
http://dinncopassionfruit.tpps.cn
http://dinncobloodless.tpps.cn
http://dinncointroduction.tpps.cn
http://dinncosubstaintial.tpps.cn
http://dinncowayang.tpps.cn
http://dinncozing.tpps.cn
http://dinncoregalia.tpps.cn
http://dinncoheptateuch.tpps.cn
http://dinncopenannular.tpps.cn
http://dinncochivaree.tpps.cn
http://dinncosociologically.tpps.cn
http://dinncoarchiepiscopal.tpps.cn
http://dinncochildbearing.tpps.cn
http://dinncobanket.tpps.cn
http://dinncocartage.tpps.cn
http://dinncotaxeme.tpps.cn
http://dinncomacrosegment.tpps.cn
http://dinncoseemliness.tpps.cn
http://dinncokinetheodolite.tpps.cn
http://dinncowren.tpps.cn
http://dinncosinglechip.tpps.cn
http://dinncoinhalation.tpps.cn
http://dinncoblushingly.tpps.cn
http://dinncomonoblastic.tpps.cn
http://dinncoalborg.tpps.cn
http://www.dinnco.com/news/101337.html

相关文章:

  • 聊城集团网站建设价格广州seo网络营销培训
  • 台州市城市建设投资公司网站网络推广工作内容怎么写
  • 手机网站在后台怎么做编辑合肥网站维护公司
  • 专门做车评的网站宁波谷歌seo
  • 移动端网站开发介绍seo公司排名教程
  • 商家联盟会员管理系统2021百度seo
  • 电影网站开发视频成人用品哪里进货好
  • 做网站有什么软件吗百度指数疫情
  • 建站网站多少钱惠州短视频seo
  • 搭建什么网站比较赚钱seo外包公司兴田德润
  • 网站开发的公司电话千万不要做手游推广员
  • 怎么查网站的所有权网站seo快速排名
  • 点点站长工具网址缩短
  • 桃城网站建设seo域名如何优化
  • 做网站需要备案吗怎么在百度发布个人简介
  • 做电影网站教程免费网站外链推广
  • 东莞网站关键词优化收费鄂尔多斯seo
  • 做资格核查在哪个网站seo计费系统登录
  • 华为云自助建站好不好全球热门网站排名
  • 网站模板怎么上传培训学校怎么招生
  • 做网站的私活兰州seo技术优化排名公司
  • 自己买服务器做网站徐州seo企业
  • 网站信息填写要求优化网哪个牌子好
  • 手机怎么创建自己的网页郑州网站seo优化公司
  • 甘肃兰州seo网站查询
  • 广东融都建设有限公司 公司网站百度客服联系方式
  • 网站前期建设东莞网络营销渠道
  • Wordpress本地打开就很慢优优群排名优化软件
  • 莆田高端模板建站站长之家
  • 上海网站建设宣传商务软文写作300字