加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

利用perl研究fork函数

发布时间:2020-12-16 00:23:04 所属栏目:大数据 来源:网络整理
导读:???? 1? #!/bin/perl ???? 2? $return=fork; ???? 3? if ($return == 0){ ???? 4? print "this is the child process;return value is $return.n"; ???? 5? } ???? 6? elsif (defined $return){ ???? 7? print "parent process:return pid is $return.n";

???? 1? #!/bin/perl
???? 2? $return=fork;
???? 3? if ($return == 0){
???? 4? print "this is the child process;return value is $return.n";
???? 5? }
???? 6? elsif (defined $return){
???? 7? print "parent process:return pid is $return.n";
???? 8? }
???? 9? else {
??? 10? die "fork error:$!n";
??? 11? }
??? 12? print "from now no,everything will be outputed twice.n";
??? 13? print "research fork function.n"
?

[root@stationx perl]# perl f.pl
this is the child process;return value is 0.
from now no,everything will be outputed twice.
research fork function.
parent process:return pid is 4440.
from now no,everything will be outputed twice.
research fork function.
结论:首先,fork行数执行成功将返回值0赋值给return变量,然后从脚本中的第三行到脚本末尾都至于fork出来的子程序中,全部在fork出来的子程序中运行了一边。等fork出来的子程序执行结束后,将fork子程序的pid返回给父程序并将值赋给了return变量,然后从第3行到脚本末尾重新回到了父程序中进行执行。

以前对这个fork函数不是很了解,通过这个perl脚本有点了解fork函数了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读