gpt4 book ai didi

perl 后台进程

转载 作者:行者123 更新时间:2023-12-04 22:14:23 26 4
gpt4 key购买 nike

我正在尝试在 perl 中运行后台进程。我创建了一个子进程,用于调用另一个 perl 脚本。我想与这个子进程并行运行几行代码。在子进程完成后,我想打印一行代码。

主脚本

#!/usr/bin/perl

$|=1;

print "before the child process\n";

my $pid = fork();

if (defined $pid)
{
system("perl testing.pl");
}

print "before wait command\n";

wait();

print "after 20 secs of waiting\n";

测试文件
#!/usr/bin/perl

print "inside testing\n";

sleep(20);

预期输出

在子进程之前
等待命令之前
(应该等待 20 秒然后打印)
等待 20 秒后

最佳答案

你的脚本有很多问题。总是:

use strict;
use warnings;
local使用特殊变量是一种很好的做法。只有一个包含特殊值 undef 的变量 defined 返回 false .因此,每个其他值(即使是 0 ;这里就是这种情况)对于 defined 返回 true .在另一个脚本中, shebang是错的。
#!/usr/bin/perl

use strict;
use warnings;

local $| = 1;

print "Before the child process\n";

unless (fork) {
system("perl testing.pl");
exit;
}

print "Before wait command\n";
wait;
print "After 20 secs of waiting\n";

关于perl 后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13530345/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com