流产前将贝壳传给孩子
当前场景,我启动了一个分叉的进程,一段时间后它中止()。
问题是 fork 和原始进程都会打印到 shell,但是在原始进程终止后,shell 将“返回”到提示符。
我想避免 shell 返回提示符并保持进程没有终止的状态,让孩子处理那里的情况。
我试图弄清楚如何做到这一点,但还没有,我的第一个猜测是围绕 tty 处理,但不确定它是如何工作的。
我忘了提及,子进程的 shell 接管可以在 fork 时完成,如果这样更容易的话,可以通过 fd 复制或某种重定向来完成。
Current scenario, I launch a process that forks, and after a while it aborts().
The thing is that both the fork and the original process print to the shell, but after the original one dies, the shell "returns" to the prompt.
I'd like to avoid the shell returning to the prompt and keep as if the process didn't die, having the child handle the situation there.
I'm trying to figure out how to do it but nothing yet, my first guess goes somewhere around tty handling, but not sure how that works.
I forgot to mention, the shell takeover for the child could be done on fork-time, if that makes it easier, via fd replication or some redirection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您可能必须使用第三个进程来处理用户交互,通过管道与“父级”和“子级”进行通信。
您甚至可以使其成为一个相当轻量级的包装器,只需将数据来回传递到父级和终端,直到父级死亡,然后切换到传递给子级或从子级传递数据。
还要进一步补充一点,我认为您将遇到的根本问题是 shell 执行命令的方式不可行。 shell 所做的相当于调用 system() ——它将等待它刚刚生成的进程终止,一旦终止,它将再次向用户显示提示。这实际上并不是 tty 的问题,而是 shell 工作方式的问题。
I think you'll probably have to go with a third process that handles user interaction, communicating with the "parent" and "child" through pipes.
You can even make it a fairly lightweight wrapper, just passing data back and forth to the parent and terminal until the parent dies, and then switching to passing to/from the child.
To add a little further, as well, I think the fundamental problem you're going to run into is that the execution of a command by the shell just doesn't work that way. The shell is doing the equivalent of calling system() -- it's going to wait for the process it just spawned to die, and once it does, it's going to present the user with a prompt again. It's not really a tty issue, it's how the shell works.
bash(我相信其他 shell)有
wait
命令:bash (and I believe other shells) have the
wait
command:你有没有考虑过颠倒亲子关系?
如果新进程终止的顺序是可预测的,则运行将在“子进程”中中止的代码以及将在父进程中继续的代码。
Have you considered inverting the parent child relationship?
If the order in which the new processes will die is predictable, run the code that will abort in the "child" and the code that will continue in the parent.