作者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