6

how would I implement communication between Linux programs written in C? Specifically, I want the following:

My program can run in multiple instances. Upon startup, I want that my program detects all other instances of my program that are already running and then it should be able to send a text string to them. On the other hand, I also want that the instances that are already running get notified that a new instance has been started and they should also be able to send a text string to the new instance.

Could someone point me to some APIs which could be used to implement such a software design on Linux? On Windows, I can simply enumerate over all windows, check their class names to find out all instances of my program, and then register a custom message with the system that I can use to send data to them. But how would I do this on Linux?

Thanks for any hints!

3
  • 1
    I like the use of the word "simply" in your third paragraph. ;-) Commented Feb 8, 2012 at 16:50
  • 1
    I think "simply" is OP's word for "fragilely". :-) Commented Feb 8, 2012 at 17:02
  • 1
    Well, I think simply (again!) using EnumWindows() and comparing the class names is as simple as it can get. The named pipe thingy sounds much more complicated in my ears but that's surely a matter of taste ;) Commented Feb 8, 2012 at 17:07

4 Answers 4

4

You have a lot of options:

  • Named pipes;
  • Msg commands (msgget, msgsend);
  • Using TCP sockets;
  • Using UNIX domain sockets;
  • Using a third party broker, like DBus or ActiveMQ;

If it is for a standalone machine, and only one stream of data, I would recommend the option number 1.

Sign up to request clarification or add additional context in comments.

Comments

2

Comments

0

I would probably start with named pipes

7 Comments

But what happens when the user closes the instance that acts as the pipe server? Then there are only clients left!
@AndreasFalkenhahn There is no pipe server. A named pipe is an actual directory entry of type fifo on the filesystem (as opposed to block device, file, directory, etc; hence the mkfifo command). To that effect, /var/${myapp}/ipcpipe could be a statically referenced location.
All the servers/clients can open the pipe in read or write, the pipe exists at the OS level. You can even create it independant of the program with mkfifo
Ok, so named pipes aren't deleted when the process terminates. But when do they get deleted? When the system is rebooted?
@AndreasFalkenhahn - you can either explicitly delete them when the last client exits, or from the command line or they will be deleted autmatically on restart - whatever works for your app
|
0

I have used sockets and multicast for that very purpose. This allows distribution of processes among several computers on the same LAN.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.