# updating a kbd table from C (shared lib)

**URL:** <https://forum.kx.com/t/updating-a-kbd-table-from-c-shared-lib/9877>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [May 25, 2015, 2:23pm UTC](https://forum.kx.com/t/updating-a-kbd-table-from-c-shared-lib/9877 "2015-05-25T14:23:00Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![mark\_rooney](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@mark\_rooney](https://forum.kx.com/u/mark_rooney)\
**Post date:** [May 29, 2015, 9:07pm UTC](https://forum.kx.com/t/updating-a-kbd-table-from-c-shared-lib/9877/21 "2015-05-29T21:07:00Z")

</div>

Hi,

It’s probably optimal in terms of performance to use pipes on a Linux machine, but we used sockets instead of pipes for a few reasons:

&nbsp; - **Trying to keep the code simple** : One of the goals was that the code should compile and run on both Linux and Windows, so we made use of the selectable socketpair available here: https://github.com/ncm/selectable-socketpair.  
This made it easier to abstract over the different operating systems and is reliable well tested code. Keeping the code simple also allows people to focus on the interaction with the kdb+ api to see how it works rather than wading through  
platform specific implementation details or having to bother with the differences between sockets vs pipes (e.g send/recv and read/write calls).

&nbsp; - **Sockets are not that much slower than pipes on Linux** : If you look at the implementation of the socket pair in the library above, you will see that it uses the AC\_UNIX/AC\_LOCAL socket family on Linux.  
This is intended for communicating between processes/threads on the same machine and cuts down on a fair bit of the overhead you would get when using AC\_INET sockets. On Windows I believe that the  
performance gap between named pipes and sockets is bigger, but you can’t just blindly replace the socket with a pipe because of the issue below.

&nbsp; - **It’s tricky to get pipes to work nicely with some functions on Windows** : the implementations of pipes on Linux and Windows are very different, and in particular trying to use the select() function on a pipe fails.  
This can make the sd1() callback hang if passed a file descriptor that belongs to a pipe. I think that the Windows implementation of select() will only work on the handles generated via WinSock (but I’m not 100%  
sure on this).

I hope this answers your question!

Thanks

---

<div class="post-metadata">

**Author:** ![tiagor2010](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@tiagor2010](https://forum.kx.com/u/tiagor2010)\
**Post date:** [May 30, 2015, 8:30am UTC](https://forum.kx.com/t/updating-a-kbd-table-from-c-shared-lib/9877/22 "2015-05-30T08:30:00Z")

</div>

Hi Mark,

&nbsp; I expect a pipe to be about 10% faster than a UNIX socket on OSX and Linux . This shouldn’t be much of a problem since other parts of the code will probably be on the critical path well before the performance delta becomes an issue, Also, your note on windows select() failing on a pipe justifies the option for sockets on portability alone.

&nbsp; Just a note. If you'll be having multiple threads triggering the same callback function, a pipe will guarantee that the writes are atomic (if less than PIPE\_BUF). The selectable-socketpair is using a STREAM socket, which is not guaranteed to be atomic and I assume could be problematic in this scenario. One could probably change it to use a DATAGRAM socket to get over this (though I haven’t tested). You probably knew this already, but might be useful for someone using the feedhandler tutorial as a template. :)

Again, thank you all for your answers. They have been most helpful.

Tiago

[Previous page](https://forum.kx.com/t/updating-a-kbd-table-from-c-shared-lib/9877.md?page=1)
