2

I am trying to find a way to do 1-to-many communication between processes in Linux. I found that linux has named pipes, but those won't work with multiple readers (I found on other SO answers that once a reader reads some data, others don't get it); could someones clarify this - I see mixed answers on SO - whether its possible or not.

Also, from my understanding, with sockets, multiple clients can connect to a server, but in my case, I need a process to send out data to multiple processes (kind-of reverse) - like a broadcast.

Could anyone suggest what options are available ?

Edit : Can shared memory be used some how ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jake
  • 16,329
  • 50
  • 126
  • 202

2 Answers2

2

Named pipes indeed are just like a pipe, i. e. they have one input and one output. So you cannot use a single (named) pipe to send information to more than one other process.

But there is nothing against the idea of using one pipe for each other process and filling all pipes with the same information. This will work when using named pipes, (unnamed) pipes, and sockets. But it will in all cases mean that the sender keeps track of all the receivers and sends the information to each of them separately.

If you want to keep the sender uninformed about the list of receivers, then the only way I can think of right now is to use a plain file. Your sender can write it, and the receivers all can read it.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • Can shared memory be used some how ? – Jake Jul 27 '14 at 00:53
  • 1
    @Jake yes, you can put the data in an shm segment, but you still need a way to notify all readers that there's something there to read, which means signals or sockets or pipes or something anyway. If the communication is one way and the data isn't large, there's not much win with shm. – hobbs Jul 27 '14 at 01:26
1

You can use shared memory with read-write semaphores to exchange data from one process to many process. The read-write semaphores can be used to synchronize the data between the processes.

The following is the pseudo code to synchronize communication between one writer thread and 20 reader threads. Write is blocked till all the reader threads read the shared memory.

if it is a writer thread
{
    Take read-write mutex lock
    increment writers++;
    Release read-write mutex lock

    Sem wait for the node-is-empty
    Access the shared memory and fill the empty node

    Take read-write mutext lock
    Decrement writers--; 
    Release read-write mutex lock

    Sem post for the node-is-filled
}
else /* want to read */
{
    Take read-write mutex lock
    if (writers > 0 || readers == 20) //reader default value is 20 reader threads
    {
        Release read-write mutex lock
        sem wait for the node-is-filled
        Take read-write mutex lock
    }

    Release read write mutex lock

    printf("Shared Memory: %d\n", memory);

    Take read-write mutex lock
    readers--;
    if (readers == 0) //wait for all the threads to read the buffer
        sem post for the node-is-empty
    Release read-write mutex lock
}
Ashok Vairavan
  • 1,862
  • 1
  • 15
  • 21