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

关于理解Perl的fork函数的一个范例

发布时间:2020-12-15 20:55:06 所属栏目:大数据 来源:网络整理
导读:方便理解,Perl的fork函数派生子进程的过程: ? #!/usr/bin/perl -w? #?wangxiaoyu#live.com use strict; ? ? defined(my? $pid =fork())? or ? die ? "Fork?process?failured:$!n" ; ? unless( $pid ) ? { ? ?????#?This?is?the?child?process. ? ?????sys

方便理解,Perl的fork函数派生子进程的过程:

?

 
 
  1. #!/usr/bin/perl -w?
  2. #?wangxiaoyu#live.com
  3. use strict; ?
  4. ?
  5. defined(my?$pid=fork())?or?die?"Fork?process?failured:$!n"; ?
  6. unless($pid) ?
  7. { ?
  8. ?????#?This?is?the?child?process. ?
  9. ?????system?"date"; ?
  10. ?????sleep(3); ?
  11. ?????print?("Exit?child?after?3?seconds?wait!n"); ?
  12. ?????exit(); ?
  13. } ?
  14. #?This?is?the?parent?process. ?
  15. waitpid($pid,0); ?
  16. system?"date"; ?
  17. print?("exit?parent!n"); ?

程序执行结果如下:

# perl perl-fork02.pl
Wed Aug? 4 22:51:38 HKT 2010
Exit child after 3 seconds wait!
Wed Aug? 4 22:51:41 HKT 2010
exit parent!
#
?

如果不在父进程中提供waitpid函数的话,父进程会在成功创建子进程后不等待子进程执行完毕,立即执行其余的代码,这样的话执行的结果如下:

# perl perl-fork02.pl Wed Aug? 4 22:50:25 HKT 2010 Wed Aug? 4 22:50:25 HKT 2010 exit parent! # Exit child after 3 seconds wait! ?

(编辑:李大同)

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

    推荐文章
      热点阅读