gpt4 book ai didi

linux - 在 tcl 中,如何关闭 dp_MakeRPCClient 从 fork 子创建的套接字/ channel 而不在父级中关闭它

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:18:15 24 4
gpt4 key购买 nike

我正在使用 Tcl-DP 在 tcl 中的两个进程之间进行通信。我在一个进程上使用 dp_MakeRPCServer 创建服务器,在另一个进程上使用 dp_MakeRPCClient 创建客户端。创建客户端后,它会连接到服务器,并在两个进程之间创建一个新套接字。

第一道工序

dp_MakeRPCServer 50000
file channel
> stdin stdout stderr tcp0

第二个过程

dp_MakeRPCClient <host> 50000
file channel
> stdin stdout stderr tcp0

第一道工序

file channel
> stdin tcp2 stdout stderr tcp0

我们可以看到第一个进程上的tcp2就是socket。现在我正在做 fork , channel 被克隆到 child 中。

子进程(从第一个进程派生)

file channel
stdin tcp2 stdout stderr tcp0

我想关闭子进程上的 tcp2(套接字)而不关闭第一个和第二个进程之间的套接字。

close tcp2 将仅关闭子进程、第一个进程和第二个进程中的套接字。

我也将接受任何其他考虑的建议:

  • 任何时候其他进程都可能尝试连接到第一个进程的服务器。我不希望子进程接收这些调用。
  • 第二个进程可以随时开始向第一个进程发送消息。同样,我不希望子进程接收这些消息。

我应该提到我使用的是 TclDp 4.0 和 tcl 8.4。

最佳答案

以下内容需要您阅读the socket FAQ首先,但这里是关键位:

Generally the difference between close() and shutdown() is: close() closes the socket id for the process but the connection is still opened if another process shares this socket id. The connection stays opened both for read and write, and sometimes this is very important. shutdown() breaks the connection for all processes sharing the socket id. Those who try to read will detect EOF, and those who try to write will reseive SIGPIPE, possibly delayed while the kernel socket buffer will be filled. Additionally, shutdown() has a second argument which denotes how to close the connection: 0 means to disable further reading, 1 to disable writing and 2 disables both.

在 Unix 上,套接字上的 Tcl close 命令将映射到具有标准套接字实现的套接字上的 close() 系统调用,除非您要求特定的要关闭的方向(Tcl 8.6 功能),在这种情况下将使用 shutdown() 系统调用。这意味着对于标准套接字,如果在派生之前在父进程中打开,父进程应该在派生之后关闭套接字。 (让两个进程同时从同一个 TCP 套接字读取数据通常不是一个好主意;事情变得非常困惑。)

但是, TclDP 使用它自己的套接字 channel 实现 only supports closing via the close() system call在 Unix 上。 (我没有检查过 Windows;我不太了解那里的系统 API。)

就是说,我希望子进程中的 close tcp2 能够工作,前提是父进程仍然打开套接字。在这方面,Tcl 确实只是暴露了操作系统的能力。它不会做任何特殊的事情来使 child 的关闭影响 parent 。

另见

关于linux - 在 tcl 中,如何关闭 dp_MakeRPCClient 从 fork 子创建的套接字/ channel 而不在父级中关闭它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854350/

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