- 論壇徽章:
- 0
|
本帖最后由 billypeng 于 2010-03-16 17:10 編輯
有兩個程序, a.pl 和 b.pl , 內(nèi)容如下:
a.pl
- #!/usr/bin/perl
- use strict;
- #my ($f, $pid, $result);
- print "Time1:", time(), "\n";
- eval{
- my ($f, $pid, $result);
- local $SIG{ALRM} = sub { die "timeout"; };
- alarm(60);
- # sleep(120);
- $pid = open($f, "-|", "./b.pl");
- print $pid, "\n";
- $result = <$f>;
- print $result;
- alarm(0);
- };
- if($@)
- {
- #check b state
- }
- print "Time2:", time(), "\n";
- print "OK\n";
復(fù)制代碼 b.pl :
- #!/usr/bin/perl
- sleep(120);
- print "B\n";
復(fù)制代碼 程序不用解釋,一看就懂,我希望a.pl超出60s就自己中斷,然后檢查b.pl的狀態(tài)。
我發(fā)現(xiàn),在a.pl中,稍微調(diào)整一下代碼,運(yùn)行結(jié)果完全不一樣,暫時還沒有達(dá)到我的要求。
例如:
1. 把 my ($f, $pid, $result); 挪到外面;
2. 去掉 eval;
3. 去掉 alarm(0);
4. 直接用sleep(120),而不用執(zhí)行b.pl,模擬延時。
現(xiàn)在的幾種結(jié)果是:
1. a.pl執(zhí)行到結(jié)尾,但沒有退出,必須等b.pl退出以后才退出。
2. a.pl在eval語句等待,但必須等b.pl退出以后才退出,相當(dāng)于alarm沒有起作用。
3. a.pl退出了,只等待了60s,但 if($@)后面的語句沒有執(zhí)行。
我需要的是:a.pl退出了,只等待了60s,執(zhí)行if($@)后面的語句。 |
|