作者koalahsu (koala)
看板Perl
标题Re: [问题] 关於system
时间Sat Oct 18 13:59:05 2008
经由版友reizarc建议
下面是我写的程式,外部会执行的程式为 a.c 编译成 a.out
程式还是会像 利用$aa=`cmd`;print $aa; 的方式一样,
a.out执行完毕才会印出变数 $aa
可以请问还有什麽地方有问题吗?
run.pl
----------
use Tk;
$mw = MainWindow->new;
$bt = $mw->Button(-text => 'Clink to start', -command => \&Run);
$bt->pack(-fill => 'x');
$tx = $mw->Text(-width => 50, -height => 50);
$tx->pack;
MainLoop;
sub Run {
$bt->configure(-text => 'Running',-state=>'disabled');
open $fd, "./a.out |";
$mw->fileevent($fd, 'readable' => sub{
sysread $fd, $buf, 30;
$aa=$buf;
$tx->insert('end', $buf);
print $aa;
$done='Done';
if($aa =~/$done/ ){
close $fd;
undef $fd;
print "Done!!";
$bt->configure(-text => 'Clink to start ping',
-command => \&RunPing,
-state => 'normal');
}
});
}
a.c
-----
#include<stdio.h>
int main()
{
int i,j;
for(i=0;i<10;i++)
{
printf("a %d Hello World\n",i);
}
for(i=0;i<10;i++)
{
if(i%2==0)
for(j=0;j<2000000000;j++)
;
printf("b %d Hello World\n",i);
}
printf("Done\n");
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.114.203.128
1F:→ reizarc:呼叫的外部执行档要设定 auto flush 10/18 14:41
2F:→ reizarc:不然送出的 output 会被 buffer fileevent 不会即时收到 10/18 14:43
3F:→ koalahsu:@@ 不是把 $fd->->autoflush(1); ? 10/18 15:52
4F:→ reizarc:你举的例子的话 是 呼叫的 a.out 里面要 auto flush 喔 10/18 20:24
5F:→ koalahsu:意思就是变成无解了> <,感谢 10/19 00:53