2017-01-26 39 views
2

Basicaly,我只能管結束我寫在C殼.. 我試着去實現管道功能,它幾乎做到了:爲什麼當我發送SIGINT

> ls | cat -e | wc | wc -l 
> 1 

但我有一個問題,當試圖製造更慢/更長的執行。

事實上,當我嘗試管的結果, '貓的/ dev/urandom的',它basicly等待...

> cat /dev/urandom | cat 
> 

但是,當我發送一個SIGINT(用CTRL + C)停止它,然後它打印在標準輸出上所得到的緩衝..

when i ctrl + c , cat /dev/urandom

所以我的問題是:我應該在哪裏尋找在嘗試解決這一問題?

我的菸斗執行部分:

int   exec_apipe(t_me *me, t_node *curs, int is_last) 
{ 
    int  pfd[2]; 
    int  pid; 

    if (pipe(pfd) == -1 || !curs) 
     return (0); 
    if (!(curs->pid = fork())) 
    { 
     dup2(me->fd_in, 0); 
     me->fd_in > 0 ? close(me->fd_in) : 0; 
     if (!is_last) 
      dup2(pfd[1], 1); 
     else if (curs->parent->parent && curs->parent->parent->fd_out >= 0) 
      dup2(curs->parent->parent->fd_out, 1); 
     close(pfd[0]); 
     exec_mpipe(me, curs); 
     exit(-1); 
    } 
    else if (curs->pid > 0) 
    { 
     waitpid(curs->pid, &(curs->ret), WUNTRACED); 
     handle_pid(me, curs->ret); 
     close(pfd[1]); 
     me->fd_in = pfd[0]; 
    } 
    return (pid >= 0 ? 1 : -1); 
} 

我希望有人會明白什麼即時消息說,也許能幫助.. 感謝

+0

閱讀所有upvoted的答案:http://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe –

回答

2

您需要啓動所有在您的管道方案在你等待的任何之後才能完成。

您的shell實現正在等待每個管道元素在啓動下一個管道元素之前完成。但/dev/urandom層出不窮; cat /dev/urandom將運行,直到遇難。所以第二個cat從來沒有開始,直到你控制-C第一個。

+0

這很容易解決它,這要歸功於你非常精確和明確的答案!再次感謝 :) – lifeguru42

相關問題