1

How can I wait for multiple instance of same program to finish ? Below is my scenario any suggestion or pointers ?

I need to restart a running C process. After googling for long time, I figured out restarting can only done by fork and exec(I need the new instance to have a different pid than the original one hence using only exec wont work). Below is the sequence i did.

Shell 1:
Bash script
1. Start the first instance(./test.exe lets say pid 100)
2. Wait for it complete(pid 100) <<< Here need to wait for all instances of test.exe to complete

Shell 2:
1. Send a signal to above process(pid-100)
2. In signal handler had fork(new pid 200) a new process with exec command(./test.exe --restart) and kill parent (pid 100)

Now my question is that how can wait for all instances of test.exe to complete in shell1's bash script ?(basically have to wait until pid 200 is completed) With my current approach shell1's bash script exits as soon as I send signal to kill pid 100

Update:

Actually I am looking for some bash/unix command to wait for all instances of test.exe is finished. Something like - 'wait $(pgrep -f test.exe)'

Hipster1206
  • 411
  • 6
  • 13
  • 1
    I flagged this as too broad since there are so many options of so many different layers and you dont even specified you want it to get solved in the not provided C code (there for example you could do interprocess communication or let one process invoke the others and keep track of them) Or in you script. So this need clarification for not getting too wide speculating answers. – dhein Aug 06 '15 at 09:59

1 Answers1

0

Basically you are looking for inter-process synchronisation mechanisms, i.e. inter-process semaphores and inter-process mutexes.

Two approaches that come to mind are POSIX semaphores and the older System V semaphores. I would recommend the former.

Also check out this SO reply.

Hope this helps :)

Community
  • 1
  • 1
bytefire
  • 4,156
  • 2
  • 27
  • 36