0

I have a producer process that will update a shared variable and updated value of shared variable will be read by many consumer processes.

Producer can only update shared variable if all the consumers have read it. To implement this, I need to know number of consumer processes, which can change dynamically. What is the best way to achieve this?

I can try maintaining count of consumer processes by incrementing a shared variable when consumer process is initialized and decrement when consumer process is closed.

However how to take care of scenario, if consumer process is killed or crashes. Is there a way to find number of processes subscribed to a shared memory?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Snakeeye
  • 31
  • 3
  • 1
    The problem here is that if you ask two C++ developers "what is the best way to achieve X" you will get three different answers. – Sam Varshavchik Feb 09 '20 at 18:47
  • 1
    Perhaps use a watchdog / controller service or, an elected leader / quorum in an peer-only model with heartbeats. Or consider it an unrecoverable error if a process dies unexpectedly.. might be also able to setup error handlers to do “just enough” work on a critical abort in certain cases. – user2864740 Feb 09 '20 at 18:55
  • I understand you wanted to keep question brief. But it is too open-ended to work with. Usually one creates systems like this with discipline, particularly how the consumer processes are started. For example, in windows if a "leader" process invokes CreateProcess on each, it can track them with ease. See GetExitCodeProcess. – WilliamClements Feb 20 '20 at 04:40

0 Answers0