控制终端&新会话

发布于 2024-10-08 04:42:43 字数 558 浏览 0 评论 0原文

进程(在我的例子中是会话领导者)如何获得控制终端?

我在程序中所做的事情:

1. fork;
2. parent -> while(1) or smth. similar;
3. child  -> setsid();
             exec "man ps";

我相信我不会在输出中得到任何结果。 (孩子是会话领导者,因此现在它与旧的 tty 没有关系)但我明白但不明白为什么。人的输出。但不具备互动性。当我按 Ctrl-z 时,它会变成交互式,当我按“q”时,它会停止并返回到我的 prog(父级)。所以问题是:

  1. 请解释一下开始时发生了什么(为什么我必须按 ctrl-z,请阅读上面的内容)
  2. 为什么 man 在该 shell 中执行一些输出?
  3. man 如何在没有任何 tty 连接的情况下做到这一点(我用 ps 检查过,man 和 pager 在 TTY 栏中有“?”)
  4. 最后:新的会话领导者如何获得控制终端。除了 open(/dev/tty) 之外还有其他方法吗?

How can a process (in my case session leader) get controlling terminal?

What I do in my program:

1. fork;
2. parent -> while(1) or smth. similar;
3. child  -> setsid();
             exec "man ps";

I beleived that i would get nothing in the output. (child is a session leader and therefore now it has no relation to old tty) But i got and don't understand why. Man ouputs. But is not interactive. When I press Ctrl-z it becomes interactive when i press 'q' it quites and returnes to my prog(parent). So the questions are:

  1. Please explain what happens at the beginning (why I have to press ctrl-z, read above)
  2. Why man does some output in that shell?
  3. How can man do it without any tty connected (I checked it with ps, man and pager have "?" in TTY column)
  4. And finally: how can a new session leader acquire a controlling terminal. Are there any wayes besides open(/dev/tty) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

离鸿 2024-10-15 04:42:43

问 1 至 3:即使在 setsid() 之后,子进程仍保留对 stdin、stdout 等的访问。您需要显式关闭它们(或使用例如 open("/dev/null",O_RDWR); 重新打开它们)。

问 4:

当没有控制终端的会话领导者打开终端设备文件并且打开​​时清除标志 O_NOCTTY 时,如果该终端尚未分配给会话领导者,则该终端将成为分配给会话领导者的控制终端一些会话

< a href="http://uw714doc.sco.com/en/SDK_sysprog/_The_Controlling-Terminal_and_Pr.html" rel="nofollow" title="控制终端和进程组">http://uw714doc.sco。 com/en/SDK_sysprog/_The_Controlling-Terminal_and_Pr.html

Q. 1. to 3.: The child process keep access to stdin, stdout etc., even after setsid(). You need to close them explicitly (or reopen using eg. open("/dev/null",O_RDWR);).

Q 4.:

When a session-leader without a controlling-terminal opens a terminal-device-file and the flag O_NOCTTY is clear on open, that terminal becomes the controlling-terminal assigned to the session-leader if the terminal is not already assigned to some session

http://uw714doc.sco.com/en/SDK_sysprog/_The_Controlling-Terminal_and_Pr.html

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