Chinaunix首页 | 论坛 | 博客
  • 博客访问: 837965
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-18 10:33:47

The simplest way to launch a child process in Perl to run a program is the  system
function. For example, to invoke the Unix date command from within Perl, it looks like:
system "date";
The child process runs the date command, which inherits Perl’s standard input, stand-
ard output, and standard error. This mean that the normal short date-and-time string
generated by date ends up wherever Perl’s STDOUT was already going.
  1. system 'ls -l $HOME';
  2. the same with
  3. system "ls -l \$HOME";
While the child process is running, Perl is patiently waiting for it to finish. So, if the
date command took 37 seconds, then Perl is paused for those 37 seconds.

The return value of the system operator is based upon the exit status of the child com-
mand.† In Unix, an exit value of 0 means that everything is okay, and a nonzero exit
value usually indicates that something went wrong:
unless (system "date") {
  # Return was zero - meaning success
  print "We gave you a date, OK!\n";
}

!system "rm -rf files_to_delete" or die "something went wrong";
In this case, including $! in the error message would not be appropriate because the
failure is most likely somewhere within the experience of the rm command, and it’s not
a system call–related error within Perl that $! can reveal.

The system function creates a child process, which then scurries off to perform the requested action while Perl naps. The exec function causes the Perl process itself to perform the requested action. Think of it as more like a “goto” than a subroutine call.

The function executes a system command and never returns; use instead of if you want it to return. It fails and returns false only if the command does not exist .

Why is this useful? Well, if the purpose of this Perl program were to set up a particular
environment to run another program, the purpose is fulfilled as soon as the other pro-
gram has started. If we’d used system instead of exec, we’d have a Perl program just
standing around tapping its toes waiting for the other program to complete, just so Perl
could finally immediately exit as well, and that’s a wasted resource.
Having said that, it’s actually quite rare to use exec, except in combination with fork
(which you’ll see later). If you are puzzling over system versus exec, just pick system,
and nearly all of the time, you’ll be just fine.

usually, the exec work with fork.
system "date" can be implement by below:
  1. defined(my $pid = fork) or die "Cannot fork: $!";
  2. unless ($pid) {
  3.   # Child process is here
  4.   exec "date";
  5.   die "cannot exec date: $!";
  6. }
  7. # Parent process is here
  8. waitpid($pid, 0);
Here, we’ve checked the return value from fork, which will be undef if it failed. Usually
it will succeed, causing two separate processes to continue to the next line, but here
only the parent process has a nonzero value in $pid, so only the child process executes
the exec function. The parent process skips over that and executes the waitpid function,
waiting for that particular child to finish (if others finish in the meantime, they are.




阅读(431) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~