Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hope you can help me with this, I've searched a lot and I got really confused of what to do here.

I'm building a program in C and I need to run it multiple time as I need.

So it's going to be like below,

process 1 handle all rows on DB table test where process_flag=1
process 2 handle all rows on DB table test where process_flag=2
process 3 handle all rows on DB table test where process_flag=3
and so on

How can I make the processes aware of each other, so if process 3 goes down then processes 2 & 3 start working on process_flag=3?

share|improve this question
This would get voted down as an answer because it doesn't fit within the context of the question. Process monitoring/recovery is built into Erlang. And your scenario sounds like it fits perfectly with what Erlang does. – Michael Brown Mar 24 '12 at 15:50
"How can I make the processes aware of each other, so if process 3 goes down then processes 2 & 3 start working on process_flag=3?" Do you mean 1 & 2 start working on 3 (because process 3 is dead)? – jmq Mar 24 '12 at 17:27
yes because process 3 is dead – poly Mar 24 '12 at 22:54
up vote 1 down vote accepted

The general design of such architecture is inter-process communication.

They are based mostly on:

  • shared memory,
  • message queues,
  • semaphores,
  • files,
  • underlying specific OS mechanisms such as Unix signals or Apple events.
share|improve this answer

The best solution might be to keep all the processes unaware of each other and use a separate watchdog/supervising process to monitor them and automatically restart ones that go down. Examples are Supervisor or perp.

If you want it to behave exactly as you say, you could have each process store their pid in the database (or in a file), or send a heartbeat periodically (storing time of last activity in the database, or touching a file on disk). The table/file would also contain the process_flag. Then in between jobs the other processes could check this and see if any of the pids are for no longer running processes, or if the heartbeat hasn't been updated in a while. Then they could "claim" that process_flag and update the row/file atomically.

But... if you do all that you're basically making each process a process monitor. So to keep it simple you could split out that monitoring code into a separate process. But then you've basically written a replacement for one of the existing tools you could have been using.

A concern people have about using a watchdog is what to do if it crashes itself. One solution is to have a cron that periodically starts it up if it's no longer running.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.