|Powered by QM on a Linux server|
KnowledgeBase 00114: Socket Inheritance
QM has support for socket based connections to other systems or other processes on the same system. This can be an incoming (server) connection or an outgoing (client) connection. The application at the remote end of the connection does not need to be QM but might be, for example, a web server.
It is easy to write a program that handles data arriving from or sent to a single socket connection. Processing more than one socket simultaneously requires use of multi-threaded sockets as described in KnowledgeBase article 23.
Although this allows actions on multiple sockets to be interleaved, it is not truly multi-threaded. An incoming message that requires lengthy processing will cause activity on other sockets to stall until it is complete.
An application that needs to process multiple incoming connections in a true multi-threaded manner requires that each socket connection is passed to a separate phantom process. The parent process becomes a network listener that uses ACCEPT.SOCKET.CONNECTION() to wait for and accept an incoming connection and then starts a phantom process to handle that connection. For this to work, the socket connection must be transferred from the parent to the phantom using socket inheritance.
After accepting the incoming connection, the server should set the socket into inheritable mode using the SKT$INHERITABLE mode of the SET.SOCKET.MODE() function. Then, when the process starts a phantom, the inheritable socket is automatically passed to the new process, closing it in the server process. The phantom process can access the inherited socket using the @SOCKET variable.
Although incoming socket connections are the most likley use of this capability, the mechanism is equally applicable to outgoing connections.
A process can only put a single socket into inheritable mode at one time so this should ideally be done immediately before launching the phantom. Attempting to set inheritability on a second socket will cancel the inheritability of the previous socket. Server mode sockets opened with CREATE.SERVER.SOCKET() cannot be made inheritable.
00023: Using Multi-Threaded Sockets