gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:25:29 25 4
gpt4 key购买 nike

我不确定在从我的应用程序生成外部后台进程时是使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com