fork + exec

fork是低层次的系统调用,通过复制父进程来创建子进程。

fork的行为

fork用来拷贝当前进程,生成一个基本完全一样的子进程。

my $pid=fork();

如果fork成功:

  • 则表示成功创建子进程,这时会有两条执行路线:继续执行父进程、执行子进程
  • fork成功时,会返回两个值:对父进程返回子进程的pid,对子进程返回0

如果fork失败,将对父进程返回-1,并设置错误信息。fork失败的可能原因有:

  • 内核内存不够,无法申请内存来fork
  • 达到了允许的最大进程数量(进程数上限)
  • 达到了rlimit限制的某种资源上限

例如:

#!/usr/bin/perl
#
use 5.010;

my $pid=fork();
say $pid,"======";

执行该程序,将返回两行数据:

62620======
0======

其中第一行输出是父进程输出的,第二行是子进程输出的。

虽然这里父进程先输出,但fork成功之后,父、子进程并没有执行的先后顺序,也可能cpu会先调度到上子进程去执行。

更复杂一点的例子:

#!/usr/bin/perl
#
use 5.010;
print "id1: ",$pid,"n";
my $pid=fork;
print "id2: ",$pid,"n";
if(!$pid){  # ==0 exec
    say "child process: ",$pid;
}
waitpid($pid,0);
say "parent process: ",$pid;

首先perl进程输出id1。然后fork一个子进程,这时有两条执行路线。假如fork后先执行父进程,则:

  • 此父进程将输出id2
  • 然后判断pid的值,因为fork返回给父进程的的pid变量值为子进程的进程号,所以不会等于0,于是if判断不通过
  • 继续执行waitpid(),它将等待子进程执行结束,以下是子进程的执行过程:
    • 子进程首先输出id2
    • 然后判断$pid,由于fork返回给子进程的pid变量值为0,所以if判断通过,于是子进程输出"child process"
    • 继续执行waitpid(),由于$pid=0,waitpid()的等待pid为0时,表示等待以自己为leader进程的进程组中的其它进程,由于没有进程组,所以waitpid失败
    • 继续执行,输出"parent process"
    • 子进程执行完毕
  • 父进程的waitpid()等待子进程执行完毕,继续向下执行
  • 父进程输出"parent process"

假如fork之后,先执行子进程,且还先把子进程执行完了,cpu才调度到父进程,则也没有影响,子进程执行完毕后,迟早会调度到父进程,而父进程的waitpid($pid)已经没有子进程了,于是waitpid()失败(返回-1),继续向下执行。

显然,上面fork的代码有一些问题。由于fork创建子进程之后。父、子进程都继续执行,且执行的先后顺序不定。所以:

  1. 在fork之后,应该紧接着判断是否是子进程,避免有些在操作父子中都执行
  2. 在父进程中等待子进程
  3. 在子进程中加入执行完后就退出子进程的动作,免得执行本该父进程执行的动作

大概代码如下:

my $pid=fork;
unless($pid){   # 判断子进程的语句紧跟着fork
    CODE1;
    exit;       # 要让子进程退出
}
waitpid($pid,0);  # 要等待子进程
CODE2;

fork和exec结合

一般fork和exec会一起用,fork用来创建新的子进程,exec启动一个程序替代当前子进程并在子进程结束时退出子进程。

例如system "date"命令,替换为低层次的fork+exec+waitpid。

defined(my $pid=fork) or die "Cannot fork: $!";
unless($pid){
    # 进入到这里执行的,表示进入子进程
    exec 'date';       # exec正确执行时,执行完后将结束子进程
    die "cannot exec date: $!";     # exec启动失败时,将执行die来结束子进程
}

# 这里的表示是父进程
waitpid($pid,0);

system、exec、fork等的区别

第一个人解释:

  • exec replaces the current process with another one.
  • system runs another program, wait for its completion.
  • fork copies the current process; both the original and the copy continue from the same point.
  • pipe sets up a pipe between two handles.
  • syscall makes a low-level system call.
  • eval executes (the string form will first compile) a piece of Perl code.

第二个人解释:

  • exec is used to execute the given process by replacing the current process. If the given process get executed successfully then exec will not return the value. exec returns the value in case of failure only.
  • System is also doing the same thing as exec but system returns value in both success and failure cases. And parent process waits for the child process to complete. System() runs the command through a shell,while exec() runs the command directly.
  • fork is used to create a new process(child process). And it is returning the PID of child to parent and zero to child if the fork is successful. The difference between the fork and exec is exec replaces the current process but fork doesn't.
  • pipe is used for communicating between two processes. We can use both named and nameless pipes. It returns open a pair of pipes. In one end we can write. And in another end we can read the content.
  • syscall is used to call the system call which is specified as a first argument. Remaining elements are the arguments to the system call.
内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!