IPC Basics

What is TCP/IP?

TCP/IP is the address system of the internet and plays a fundamental role in its operation. For a general overview, see here.

In kdb+/q, TCP/IP is used as the protocol for processes to communicate. So how does it work? Well, as some of you may know, it wasn't so long ago that if you wanted to send someone a message you would have to send them a letter in the post; to do so, you needed their address. Similarly in TCP/IP, every device on the network has an IP address

However (to keep our analogy going) an address usually isn't enough - multiple people could live at the same address, so you would have to use their address and their name so that the letter went to the correct person. Similarly in TCP/IP, multiple programs could be running on the same device, so to correctly identify the correct program, we need to specify a port.

So, in kdb+, to connect two processes together using the TCP/IP protocols we need to know their IP address and port.

Getting Started

First, we will need to start up a q process and assign that process a port number (the IP address will just be the IP address of our current device). 

To do so, we have two options:

When the process is running, we can use the command '\p' with no input to check which port the process is running (or listening) on.

Connecting two processes

For a process to connect to another process, it needs to know the IP address and port of that process. This allows it to find that 'target' process and initiate a connection to it.

In kdb+/q the function or keyword that we use to initiate that connection is called hopen and in its most simple form can take just a port as an input (it will assume the target process is running on the same server and has no user/pass authentication).  

Starting up a second process in addition to the one above, and using the hopen command:

It returns an integer, 3, which is now the reference to the handle opened to that process on that port.

You may be wondering why the first handle opened by this process is 3 rather than 0, 1 or 2. The reason is that 0-3 are reserved for the 'system handles'. 0 is always the handle for the console, and 1 and 2 are always the handles for stdout and stderr

We can now run queries on that process using that handle in the format handle"query", for example:

Additionally, variables can now be set on the target process:

Closing connections

'hclose' is the command used to close a connection

Comments