即使在运行过程中终端也会停止打印

问题描述 投票:0回答:1

我曾尝试在新终端中打开子进程,但没有在线或任何书籍找到解决方案。我最近研究了Unix域套接字和传递文件描述符,并尝试使用它们来实现它。

以下是我程序的伪代码。 send_fdrecv_fd是用于传递Richard Richards书中定义的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;
}
c sockets unix terminal gnome-terminal
1个回答
0
投票

终端程序将设置启动的子过程的标准输入和输出,并将其连接到自身以进行I / O,当子过程终止时,终端程序也会终止。如果相反,如果您告诉gnome-terminal在第一个./temp程序终止后启动外壳程序,那么它将保持打开状态,我认为您的第一个程序应该能够继续读取和写入传输的文件描述符。

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

请参见this Unix answer

© www.soinside.com 2019 - 2024. All rights reserved.