【发布时间】:2020-06-03 23:48:44
【问题描述】:
我尝试在新终端中打开子进程,但在网上或任何书籍中都没有找到解决方案。 我最近研究了 Unix Domain Sockets 和传递文件描述符,并尝试使用这些来实现。
以下是我的程序的伪代码。 send_fd 和 recv_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