簡體   English   中英

即使進程正在運行,終端也會停止打印

[英]Terminal stops printing even with a running process

我試圖在新終端中打開一個子進程,但在網上或任何書中都沒有找到解決方案。 我最近研究了 Unix 域套接字和傳遞文件描述符,並嘗試使用這些來實現它。

以下是我的程序的偽代碼。 send_fdrecv_fd是 Richard Stevens 書中定義的傳遞 fds 的函數。

這些程序幾乎按預期工作。 子進程的輸出和輸入被重定向到新創建的終端。 但是子進程只能在進程temp仍在運行時打印。 temp終止時,child 停止在新終端中打印並且新終端關閉。 我應該如何克服這個問題? 提前致謝。

int forknt()
{
    system("gnome-terminal -- \"./temp\""); //run auxiliary program in a new terminal
    //sfd is fd of the unix domain socket used to connect to ./temp
    int fd0 = recv_fd(sfd); //get stdin of new terminal
    int fd1 = recv_fd(sfd); //get stdout of new terminal
    int c = fork();
    if(!c)
    {
        dup2(fd0, 0);
        dup2(fd1, 1);
    }
    close(sfd);
    return c;
}

int main()
{
    int c = forknt();
    printf("%d\n", c);
    return 0;
}

輔助程序temp代碼

int main()
{
    send_fd(sfd, 0);
    send_fd(sfd, 1);
    return 0;
}

終端程序設置它啟動的子進程的標准輸入和輸出並將它們連接到自身以進行 I/O,當子進程終止時也將終止。 相反,如果你告訴gnome-terminal在第一個./temp程序終止后啟動一個 shell,那么它會保持打開狀態,我認為你的第一個程序應該能夠繼續讀取和寫入傳輸的文件描述符。

gnome-terminal -- "./temp; exec sh"

請參閱此 Unix 答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM