master pty如何检测slave tty是否已退出?

时间:2023-01-01 19:33:24

I am using BSD style pty/tty pairs to implement running a sub shell. When the user exits the sub shell, how do I detect in the master process that this has occurred? I am using select(nfds, &read_fds, NULL, NULL, &timeout); with the master pty file descriptor set in the read_fds on the master side.

我正在使用BSD样式的pty / tty对来实现运行子shell。当用户退出子shell时,如何在主进程中检测到发生了这种情况?我正在使用select(nfds,&read_fds,NULL,NULL,&timeout);在主端的read_fds中设置master pty文件描述符。

2 个解决方案

#1


3  

The subshell is typically created by a fork() of some sort. The PID of the child is returned to the master, which can check (with waitpid(), perhaps) if it's still running.

子shell通常由某种fork()创建。子进程的PID返回给master,如果它仍然在运行,它可以检查(或者使用waitpid())。

#2


2  

I've found the answer to this question by examining the telnetd source code found in the GNU inetutils package. In telnetd, they use a SIGCHLD handler like this:

通过检查GNU inetutils包中的telnetd源代码,我找到了这个问题的答案。在telnetd中,他们使用SIGCHLD处理程序,如下所示:

int status;
pid_t pid = waitpid((pid_t)-1, &status, WNOHANG);
syslog (LOG_INFO, "child process %ld exited: %d",
    (long) pid, WEXITSTATUS(status));
// do cleanup code

#1


3  

The subshell is typically created by a fork() of some sort. The PID of the child is returned to the master, which can check (with waitpid(), perhaps) if it's still running.

子shell通常由某种fork()创建。子进程的PID返回给master,如果它仍然在运行,它可以检查(或者使用waitpid())。

#2


2  

I've found the answer to this question by examining the telnetd source code found in the GNU inetutils package. In telnetd, they use a SIGCHLD handler like this:

通过检查GNU inetutils包中的telnetd源代码,我找到了这个问题的答案。在telnetd中,他们使用SIGCHLD处理程序,如下所示:

int status;
pid_t pid = waitpid((pid_t)-1, &status, WNOHANG);
syslog (LOG_INFO, "child process %ld exited: %d",
    (long) pid, WEXITSTATUS(status));
// do cleanup code