[英]Why in GNU do we tcsetpgrp from a shell while it is a background process?
I've been studying this link to finish my shell assignment: http://www.gnu.org/software/libc/manual/html_node/Launching-Jobs.html#Launching-Jobs and it's been particularly helpful.我一直在研究这个链接来完成我的 shell 作业: http ://www.gnu.org/software/libc/manual/html_node/Launching-Jobs.html#Launching-Jobs 它特别有用。 My confusion is that, to give the shell control of the stdin file descriptor again, I need to call tcsetpgrp
from the shell after the child is terminated.我的困惑是,为了再次让 stdin 文件描述符的 shell 控制,我需要在 child 终止后从 shell 调用tcsetpgrp
。
How do I get tcsetpgrp() to work in C? 如何让 tcsetpgrp() 在 C 中工作?
I've searched different Stack Overflow questions, but none properly tell me why GNU promotes this approach.我搜索了不同的 Stack Overflow 问题,但没有一个正确地告诉我为什么 GNU 提倡这种方法。 Because the shell currently is in the "background", tcsetpgrp()
will send SIGTTOU to my process group.因为 shell 当前处于“后台”, tcsetpgrp()
会将 SIGTTOU 发送到我的进程组。 The current solution is to ignore it before calling the method and maybe reset it to default afterwards.当前的解决方案是在调用该方法之前忽略它,然后可能将其重置为默认值。 What should I do?我应该怎么办?
EDIT: I would like to note that the child is first set in another process group before the shell passes control of stdin to it with tcsetpgrp()
.编辑:我想指出的是,在 shell 使用tcsetpgrp()
将 stdin 的控制权传递给它之前,该子进程首先被设置在另一个进程组中。 Once the child dies, the shell calls tcsetpgrp()
to reclaim stdin.一旦孩子死亡,shell 调用tcsetpgrp()
来回收标准输入。 GNU suggests this as a possible implementation, but says it uses a slightly different implementation for simplicity here . GNU 建议将此作为一种可能的实现方式,但表示为了简单起见,它在这里使用了一种略有不同的实现方式。
If
tcsetpgrp()
is called by a member of a background process group in its session, and the calling process is not blocking or ignoring SIGTTOU, a SIGTTOU signal is sent to all members of this background process group.如果tcsetpgrp()
在其会话中被后台进程组的成员调用,并且调用进程未阻塞或忽略 SIGTTOU,则 SIGTTOU 信号将发送到该后台进程组的所有成员。
I'm also towards the end of implementing a shell program, and can take a stab at this question:我也快要完成一个 shell 程序了,可以试一试这个问题:
TL;DR: even though the shell will be a background process at that point, it needs to reclaim the terminal foreground for itself once the most recent foreground process group exits; TL;DR:即使此时 shell 将是后台进程,一旦最近的前台进程组退出,它也需要为自己回收终端前台; otherwise the terminal will hang and no process will consume the user's input.否则终端会挂起,没有进程会消耗用户的输入。
Here's what the session looks like when a shell launches a job from a line of command:以下是 shell 从命令行启动作业时会话的样子:
The GNU C Library manual also has another section (28.5.4 Foreground and Background) that briefly explains why the shell must reclaim the terminal forg GNU C 库手册还有另一节(28.5.4 前景和背景)简要解释了为什么 shell 必须回收终端 forg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.