views:

964

answers:

5

Hi all,

I have never written any IPC C++ on Linux before.

My problem is that I will have multiple clients (writers), and a single server (reader). All of these will be on the same machine. The writers will deliver chunks of data (a string/struct) to the reader. The reader will then read them in FIFO and do something with them.

The types of IPC on Linux are either Pipes or Sockets/Message Queues as far as I can tell.

I was just wondering if someone could recommend me a path to go down. I'm leaning towards sockets, but I have no real basis for that. Is there anything I should read/understand before embarking on this journey?

Thanks

+3  A: 

System V IPC is somewhat fiddly to use but it is a mature, robust technology. Message queues would probably do what you want and support atomic queuing/de-queuing.

Sockets are easy to use and also support communication over a network. However, they do not do any queuing, so you would have to write the queue management code within your server. Using sockets with C++ is not vastly different to using them with C. There are plenty of guides to this on the net and books such as Stevens' 'Unix Network Programming (vol 1)' that cover this topic in some depth.

ConcernedOfTunbridgeWells
+2  A: 

A good place to get your feet wet is this sockets tutorial.

You'll then need to bone-up on threads & mutexes and here.

With the above you're all set to start playing ;-)

slashmais
Maybe the select() system call is enough for what Salgar wants to do. It is not a bad thing to learn about threads though.
Ben
+6  A: 

The main issue you should consider is what kind of data you are passing as this will in part determine your options. This comes down to whether your data is bounded or not. If it isn't bounded then something stream oriented like FIFOs or sockets are appropriate; if it is then you might make better use of of things like MQs or shared memory. Since you mention both strings and structs it is hard to say what is appropriate in your case, though if your strings are bounded within some reasonable maximum you can use anything with some minor fiddling.

The second is speed. There is never a completely correct answer for this but generally it goes something like: shared memory, MQs, FiFOs, domain sockets, network sockets.

The third is ease of use. Shared memory is the biggest PITA since you have to handle your own synchronization. Pipes are easy so long as your message lengths stay below PIPE_BUF size. The OS handles most of your headaches with MQs. Sockets are easy enough but you have the setup boilerplate.

Lastly several of the IPC mechanisms have both POSIX and SYSV variants. Generally POSIX is the way to go unless the SYSV type has some feature you really need or want.

EDIT: Count0's answer reminded me that you might be interested in something more abstract and higher level. In addition to ACE you can look at Poco. And, of course, no SO answer is complete if it doesn't mention Boost somewhere.

Duck
Thanks for that :), Speed is not a concern of mine in this case, I think i'm going to go with MQs for this one, it seems to handle most things for me which is nice.
Salgar
+1  A: 

Try to take a look at ACE (Adaptive Communication Environment). The ACE libraries are free available, very mature and cross platform. Unfortunately a good documentation is not, i would recommend this book to look for a good solution. You might try to take a look at this tutorial to get a feel of the patterns (at the end of the document). ACE uses a bunch of patterns to deal very successfully and efficient with those problems especially in a networked context, so it should be a good start to scope for good patterns and methods to use.

Especially Ace_Task using the Message_Queue allow to do what you need.

count0
+2  A: 

Though you've not asked for books, and because the answers above are so good, I'm only going to suggest you get your hands on copies of these two tomes:

UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications, W. Richard Stevens

Advanced Programming in the UNIX Environment, Second Edition, W. Richard Stevens and Stephen A. Rago

There are inevitable ins & outs with this kind of coding, these two books will help you through whatever confusion you encounter.

Eric M