APUE 17章高级IPC——本地转发文件表指针(什么用?)

来源:互联网 发布:己知直径求周长 编辑:程序博客网 时间:2024/06/10 13:50
本来叫做passing file descriptors, 传递文件描述符,后文有说:

Technically, we are passing a pointer to an open file table entry from one process toanother. This pointer is assigned the first available descriptor in the receiving process.(Saying that we are passing an open descriptor mistakenly gives the impression that thedescriptor number in the receiving process is the same as in the sending process, whichusually isn’t true.) Having two processes share an open file table is exactly whathappens after afork.

于是我们这里直接就叫做本地转发文件表指针以避免误解(我看到过很多不明真相的群众按字面意思理解这个,以为真就是传递了一个文件描述符)。还有一点非常重要,那就是:

Closing the descriptor by the sender doesn’t really close the file or device, since the descriptor is still considered open by the receiving process (even if the receiver hasn’t specifically received the descriptor yet). 

应该是传递的时候,文件表里头引用计数被加一,哪怕接受进程没有显示地调用unix domain socket去接受这个描述符(这其实有点疑问,因为对方不接受,发送的进程就阻塞在sendmsg函数这里了)。我们转发这个文件表指针有何用呢?

1)用于进程池连接:

假设我们本地有工作进程A,B,C。每个都提供不一样的服务用于网络。我们有两种策略,一种是每个进程都是一个服务器进程,都占用不一样的端口或者地址,分离开来分别管理;另外一种是我们建立个连接服务器来管理外部连接,每次把socket 描述符转发给对应的工作进程让工作进程处理后续通讯,这样我们只要占用一个地址和端口就可以了,而且我们可以集中化管理认证各个连接。

2)用于构造类似系统调用的服务:

这个可以详见APUE里头17章open server小节。







阅读全文
0 0
原创粉丝点击