Re: [問題] 關於system

看板Perl作者 (koala)時間16年前 (2008/10/18 13:59), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串2/2 (看更多)
經由版友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

10/18 14:41, , 1F
呼叫的外部執行檔要設定 auto flush
10/18 14:41, 1F

10/18 14:43, , 2F
不然送出的 output 會被 buffer fileevent 不會即時收到
10/18 14:43, 2F

10/18 15:52, , 3F
@@ 不是把 $fd->->autoflush(1); ?
10/18 15:52, 3F

10/18 20:24, , 4F
你舉的例子的話 是 呼叫的 a.out 裡面要 auto flush 喔
10/18 20:24, 4F

10/19 00:53, , 5F
意思就是變成無解了> <,感謝
10/19 00:53, 5F
文章代碼(AID): #18-Nigvg (Perl)
討論串 (同標題文章)
文章代碼(AID): #18-Nigvg (Perl)