这个函数当前在学校没有搞懂, 刚从学校出来时也没搞懂, 不会用他来写多线程程序. 今天有人问我在Perl中如何进行多进程编程, 去Google了一把, 发现境界好象比以前高了, 因为感觉上懂了一些了. 下面是写的一个例子:
#!/usr/bin/perl
#
my $pid = fork();
if (!defined($pid))
{
print ("Fork process failured!\n");
exit();
}
if ($pid)
{
# This is the child process.
sleep(1);
print ("exit child after 10 seconds wait!\n");
exit();
}
else
{
# This is the parent process.
print ("exit parent!\n");
}
毕业后的这么多年中, 只有用Java来写过多线程序, 不过它可没有fork函数, 但感觉上, 他们也没有什么区别. 看来可以为refresh_mysql.pl角本增加并行执行支持了. cool!
留言 (8)
我还写过进程间通信呢
Posted by piner | Mar 22, 2007 12:54 PM
以前用c的fork时, 可以用指针来做, 这个怎么来做呢? 用文件或pipe我嫌太麻烦了.
指点一下?
Posted by anysql | Mar 22, 2007 1:47 PM
use POE
nearly multi-thread in one process, thus you can work on many topics, also check POE::Component::LaDBI or other modules on cpan
Posted by joe jiang | Mar 22, 2007 6:38 PM
我只是用Perl来编写一些数据库有关的工具, 在入门阶段, 谢谢大家的指点了.
Posted by anysql | Mar 22, 2007 9:34 PM
大侠,我觉得你大概 父和子进程搞反了
fork Does a fork(2) system call to create a new process running the same program at the same point. It returns the
child pid to the parent process, 0 to the child process, or "undef" if the fork is unsuccessful. File descrip-
tors (and sometimes locks on those descriptors) are shared, while everything else is copied. On most systems
supporting fork(), great care has gone into making it extremely efficient (for example, using copy-on-write tech-
nology on data pages), making it the dominant paradigm for multitasking over the last few decades.
Beginning with v5.6.0, Perl will attempt to flush all files opened for output before forking the child process,
but this may not be supported on some platforms (see perlport). To be safe, you may need to set $| ($AUTOFLUSH
in English) or call the "autoflush()" method of "IO::Handle" on any open handles in order to avoid duplicate out-
put.
If you "fork" without ever waiting on your children, you will accumulate zombies. On some systems, you can avoid
this by setting $SIG{CHLD} to "IGNORE". See also perlipc for more examples of forking and reaping moribund chil-
dren.
Note that if your forked child inherits system file descriptors like STDIN and STDOUT that are actually connected
by a pipe or socket, even if you exit, then the remote server (such as, say, a CGI script or a backgrounded job
launched from a remote shell) won't think you're done. You should reopen those to /dev/null if it's any issue.
Posted by bulletming | Jul 27, 2007 2:17 PM
大侠,我也觉得你把父子进程搞反了
Posted by tonywam | Dec 10, 2007 5:52 PM
应当是搞反了吧, 这方面很不懂.
Posted by anysql | Dec 10, 2007 7:08 PM
确实搞反了,不过写的不错
Posted by swgcxzys | Mar 12, 2008 2:37 PM