bash_script_multithreading

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

bash_script_multithreading [20.07.2015 12:29] – created Pascal Suterbash_script_multithreading [20.07.2015 12:49] (current) Pascal Suter
Line 70: Line 70:
   - however, if the main process is still around and you hit ctrl+c, it will kill also all its sub processes.. if the main process is not around anymore, ctrl+c will have no effect on the sub processes..    - however, if the main process is still around and you hit ctrl+c, it will kill also all its sub processes.. if the main process is not around anymore, ctrl+c will have no effect on the sub processes.. 
   - a variable that is set before the function is run and then changed while the funciton is running will remain its original value inside of the function.. that's why in our output, ''$VALUE'' does not change.. In order to get "live" data into the background function or back from the background function, you need to use some kind of a rendez-vous, a resource both have access too.. the simplest is a file.. if it's only for a small amount of data, you might want to use something in /dev/shm to keep your harddrives free for real data..    - a variable that is set before the function is run and then changed while the funciton is running will remain its original value inside of the function.. that's why in our output, ''$VALUE'' does not change.. In order to get "live" data into the background function or back from the background function, you need to use some kind of a rendez-vous, a resource both have access too.. the simplest is a file.. if it's only for a small amount of data, you might want to use something in /dev/shm to keep your harddrives free for real data.. 
 +
 +===== wait for subprocesses =====
 +if you want to be able to use ctrl+c to abort the execution of all children even after the main process has completed, you need to keep it runnig for as long as the child processes are running.. my solution looks like this: 
 +<code>
 +self=`basename "$0"`
 +children=`ps aux | grep "$self" | grep -v "grep"`
 +while [ `echo "$children" | wc -l` -gt 2 ]; do 
 + sleep 1
 + echo "still have `echo "$children" | wc -l` processes, keep waiting: "
 + echo "$children"
 + children=`ps aux | grep "$self" | grep -v "grep"`
 +done
 +</code>
 +that's the verbouse version that helped me to understand that i will always see 2 processes when i populate my $children variable through an external command.. 
 +
 +the more compact version is this: 
 +<code>
 +self=`basename "$0"`
 +children=`ps aux | grep "$self" | grep -vc "grep"`
 +while [ $children -gt 2 ]; do 
 + sleep 1
 + echo "still have $children processes, keep waiting: "
 + children=`ps aux | grep "$self" | grep -vc "grep"`
 +done
 +</code>
  • bash_script_multithreading.txt
  • Last modified: 20.07.2015 12:49
  • by Pascal Suter