Hello!

I solve the problem of synchronizing processes in Linux.

The first and main task . There are two / three processes, they all receive the same messages, approximately at the same time. One of these processes, let's call the lead, the rest - the slave. It is necessary when receiving one of the total abundance of messages, leading the process to set some values ​​for the slave, the slave to suspend until the end of the recording of these values. And then, on the slave, to make a choice between starting the message processing function or continuing without processing.

Those. this is similar to a queue with a “buffer” of 1 or 2 (depending on the number of slave processes). Moreover, the leading process does not process anything, but only issues commands based on the knowledge of how many slave processes are currently processing: give it to 1st, give it to 2nd, reset. The beauty of this method is good when you need to know how many messages were discarded (because the process does not have the ability to listen to messages during processing).

In this case, I would like to use semaphores for organizing locks (i.e., waiting for data availability, but no more ), as well as for transferring these same data. By data, I mean a counter that determines how many processes are free, and which of them are free (for example, a two-bit flag).


The second task is the elimination of the leading process. Now all processes are equivalent in tasks. This gives a gain in processing, since the third process can be involved. But here the question arises how to synchronize processes in order to prevent two or more processes from processing the same message.

To solve this problem, I would use all the same semaphores that allow you to "monitor" the process counter, and now the three-bit flag, which now solves two tasks: the task of selecting a processing process, by zero-bit precedence, and the task of identifying free processes.


But, I ran into difficulties, I can’t fully realize the necessary actions after getting the semaphore identifier in Linux. Using the possibilities of ftok , I get a unique key to my semaphore. Further through sem_get I receive a semaphore itself or I create it.

And then where to look? There is an adequate description of the functions for the implementation of what I need (a detailed description of what the function does, what parameters and what types it accepts, what it returns)? I will not give up on the direction of thoughts in the field of solving the second problem. It is still aggravated by the fact that process locking should be minimal, any delays are fraught. The code should be easy, optimal and fast.

  • one
    And how do the processes get these messages? Is that signals or data in a socket? - avp
  • Let's say it's a black box. We have a code that follows after receiving this message. Therefore, the task is to block the slave processes before receiving the "command" from the master. - Dex
  • Those. The message is received (we will tell the leader) and he (probably through shared memory) should transfer it to any free (waiting on a semaphore)? - avp
  • No, all messages are visible to all processes. It is only necessary to allow its processing to one of the processes. - Dex
  • one
    The fact is that if I understood everything correctly, this can be implemented using semaphores without anything else. Data shared between processes is only two bytes. Here I am interested in the description of functions and a small algorithm for the implementation of the necessary functionality. - Dex

1 answer 1

I will start with the terminology: the name of the dispatcher will suit the leading process well, and the handlers for the rest.

So, I would create a turn for each handler. There you can write pointers to the processed data, or an index.

As a result, problems with synchronization will not be in principle.

The dispatcher reads the incoming one, “shifts” the work to the handlers and monitors the speed of their work.