流产前将贝壳传给孩子

发布于 2024-09-06 09:34:17 字数 267 浏览 5 评论 0原文

当前场景,我启动了一个分叉的进程,一段时间后它中止()。
问题是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

是伱的 2024-09-13 09:34:17

我认为您可能必须使用第三个进程来处理用户交互,通过管道与“父级”和“子级”进行通信。

您甚至可以使其成为一个相当轻量级的包装器,只需将数据来回传递到父级和终端,直到父级死亡,然后切换到传递给子级或从子级传递数据。

还要进一步补充一点,我认为您将遇到的根本问题是 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.

情栀口红 2024-09-13 09:34:17

bash(我相信其他 shell)有 wait 命令:

等待:等待[n]

等待指定的进程并报告其终止状态。如果
未给出 N,将等待所有当前活动的子进程,
并且返回码为零。 N 可以是进程 ID 或作业
规格;如果给出了作业规范,则作业中的所有进程
正在等待管道。

bash (and I believe other shells) have the wait command:

wait: wait [n]

Wait for the specified process and report its termination status. If
N is not given, all currently active child processes are waited for,
and the return code is zero. N may be a process ID or a job
specification; if a job spec is given, all processes in the job's
pipeline are waited for.

厌味 2024-09-13 09:34:17

你有没有考虑过颠倒亲子关系?

如果新进程终止的顺序是可预测的,则运行将在“子进程”中中止的代码以及将在父进程中继续的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文