Python:何时使用 pty.fork() 与 os.fork()

标签 python linux fork kill pty

我不确定在从我的应用程序生成外部后台进程时是使用 pty.fork() 还是 os.fork()。 (比如国际象棋引擎)

我希望生成的进程在父进程被杀死时终止,就像在终端中生成应用程序一样。

这两个 fork 有哪些优缺点?

最佳答案

os.fork()创建的子进程继承父进程的stdin/stdout/stderr,而pty.fork()创建的子进程连接到新的伪终端。当你编写像 xterm 这样的程序时你需要后者:父进程中的 pty.fork() 返回一个描述符来控制子进程的终端,这样你就可以直观地表示来自它的数据并将用户操作转换成终端输入序列。

更新:

来自 pty(7) 手册页:

A process that expects to be connected to a terminal, can open the slave end of a pseudo-terminal and then be driven by a program that has opened the master end. Anything that is written on the master end is provided to the process on the slave end as though it was input typed on a terminal. For example, writing the interrupt character (usually control-C) to the master device would cause an interrupt signal (SIGINT) to be generated for the foreground process group that is connected to the slave. Conversely, anything that is written to the slave end of the pseudo-terminal can be read by the process that is connected to the master end.

关于Python:何时使用 pty.fork() 与 os.fork(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1922254/

相关文章:

结合 fork、fifo 和 execlp?

c - 进程组中的进程是否应该在 Unix/Linux 中与其父进程一起终止?

git 为 fork 的上游标签创建本地分支

python - 使用OpenCV修改numpy数组

python - 无法解析余数 : '==obj.id' from 'sobj.id==obj.id'

python - 如何在 Python 中深度复制 xml 子元素

linux - getopts 在 linux shell 脚本中抛出无效参数错误

c - 使用 C 退出到 linux 的 ssh session

Linux - 仅打印目录和子目录的文件名

Python 检查用户输入