利用perl研究fork函数
1#!/bin/perl2$return=fork;
3if ($return == 0){
4print "this is the child process;return value is $return.\n";
5}
6elsif (defined $return){
7print "parent process:return pid is $return.\n";
8}
9else {
10die "fork error:$!\n";
11}
12print "from now no,everything will be outputed twice.\n";
13print "research fork function.\n"
# 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函数了。
页:
[1]