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函数了。