Inter-process communication using shared memory and POSIX named semaphores

Does anyone have any experience of doing this? Using OCaml would be of most interest, but even using something like C would also be interesting. I am wondering what the performance is like compared to, eg, communicating over Unix sockets or pipes etc.

Perhaps this is what ocamlnet shm does? Or hack_parallel? Perhaps there are other ways to signal the other process than using shared semaphores?

1 Like

There are commercial products that do this sort of thing: IBM’s DB2 database used POSIX shm+sem for client->server comms on a single machine. But it’s tricky, and doesn’t integrate well with the rest of UNIX I/O, so … well, I wouldn’t do it myself. To wit, when things got “messed-up” you basically had to kill a bunch of hung processes, delete all the POSIX resources under the DB2 userid, and then restart. Very messy failure semantics.

What does work well, is to use shm for bulk transport, and sockets/pipes for control. The original X-windows SHM transport did this quite effectively: ownership of sub-regions of the shared-memory segment were transferred with a message-header over the control socketpair, and failure semantics was really clean, b/c closing the socket was pretty definitive that the other end would no longer be communicating using the segment. Of course, this only works when you’re trying to implement message-passing, and want shm only for acceleration.

I don’t know how much overhead there is in thread- context-switching using POSIX sem vs. writing/reading a small bit of data over a socketpair.

2 Likes

What I want here is OCaml bindings to Ach IPC. Alas, it isn’t something I want enough to get off my seat and build it myself. But we use the C bindings to Ach at my day job, and it is absolutely The Move. OCaml bindings would be nice.

1 Like

Thanks for both of these replies.

Have a look at this:

1 Like