【问题标题】:Terminal stops printing even with a running process即使进程正在运行,终端也会停止打印
【发布时间】:2020-06-03 23:48:44
【问题描述】:

我尝试在新终端中打开子进程,但在网上或任何书籍中都没有找到解决方案。 我最近研究了 Unix Domain Sockets 和传递文件描述符,并尝试使用这些来实现。

以下是我的程序的伪代码。 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;
}

【问题讨论】:

    标签: c sockets unix terminal gnome-terminal


    【解决方案1】:

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

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

    this Unix answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      相关资源
      最近更新 更多