Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2><em>Rendez-vous</em></h2> <p>If you wanna have two independant process which could communicate, you have to place a <em>rendez-vous</em> somewhere both process can reach.</p> <p>This could be a simple file, a fifo pipe, a unix socket, a TCP socket or maybe else (Rexx port).</p> <h2><a href="/questions/tagged/bash" class="post-tag" title="show questions tagged &#39;bash&#39;" rel="tag">bash</a> and other <a href="/questions/tagged/shell" class="post-tag" title="show questions tagged &#39;shell&#39;" rel="tag">shell</a></h2> <p>Bash don't have a equivalent to rexx port, so there is a little sample, using a rendez-vous file, that work (on my Linux).</p> <p>I'm using <em>shared memory</em> <code>/dev/shm</code>, to reduce disk load.</p> <h3>Simple counter sample</h3> <pre><code>$ back_func() { while :;do echo $(($(&lt;/dev/shm/foo)+1)) &gt;/dev/shm/foo; sleep .3; done; } </code></pre> <p>Let play</p> <pre><code>$ echo 1 &gt;/dev/shm/foo $ back_func &amp; $ echo $(&lt;/dev/shm/foo) 4 $ echo $(&lt;/dev/shm/foo) 21 </code></pre> <p>Than stop now:</p> <pre><code>$ fg back_func ^C </code></pre> <p>or</p> <pre><code>$ kill $! $ [1]+ Terminated back_func </code></pre> <h3>More than one variables</h3> <p>For having many vars, there could by a nice manner:</p> <pre><code>$ back_func() { declare -A MYGLOBAL local vars while :; do ((MYGLOBAL["counter"]++)) IFS=\ / read -a vars &lt;&lt;&lt; "$(&lt;/proc/uptime) $(&lt;/proc/loadavg)" MYGLOBAL["uptime"]=$vars MYGLOBAL["idle"]=${vars[1]} MYGLOBAL["l01m"]=${vars[2]} MYGLOBAL["l05m"]=${vars[3]} MYGLOBAL["l15m"]=${vars[4]} MYGLOBAL["active"]=${vars[5]} MYGLOBAL["procs"]=${vars[6]} MYGLOBAL["lpid"]=${vars[7]} MYGLOBAL["rand"]=$RANDOM MYGLOBAL["crt"]=$SECONDS declare -p MYGLOBAL &gt; /dev/shm/foo sleep 1 done } </code></pre> <p>Then</p> <pre><code>$ back_func &amp; [1] 27429 $ . /dev/shm/foo $ echo ${MYGLOBAL['counter']} 5 $ echo ${MYGLOBAL['lpid']} 27432 </code></pre> <p>and from there, why not:</p> <pre><code>$ dumpMyGlobal() { . /dev/shm/foo printf "%8s " ${!MYGLOBAL[@]} echo printf "%8s " ${MYGLOBAL[@]} echo } $ dumpMyGlobal l15m uptime crt procs lpid active rand idle l05m counter l01m 0.42 13815568.06 95 554 649 1 31135 21437004.95 0.38 73 0.50 $ dumpMyGlobal l15m uptime crt procs lpid active rand idle l05m counter l01m 0.41 13815593.29 120 553 727 2 3849 21437046.41 0.35 98 0.33 </code></pre> <p>or</p> <pre><code>$ dumpMyGlobal() { . /dev/shm/foo sort &lt;( paste &lt;( printf "%-12s\n" ${!MYGLOBAL[@]} ) &lt;(printf "%s\n" ${MYGLOBAL[@]}) ) } $ dumpMyGlobal active 1 counter 297 crt 337 idle 21435798.86 l01m 0.40 l05m 0.44 l15m 0.45 lpid 30418 procs 553 rand 7328 uptime 13814820.80 </code></pre> <h3>Get variable with snapshot</h3> <p>and finally <code>getMyGlobalVar</code> function</p> <pre><code>$ declare -A MYGLOBALLOCK # snapshot variable $ getMyGlobalVar () { local i sync=false [ "$1" == "--sync" ] &amp;&amp; shift &amp;&amp; sync=true if [ -z "${MYGLOBALLOCK[*]}" ] || $sync; then . /dev/shm/foo for i in ${!MYGLOBAL[@]} do MYGLOBALLOCK[$i]=${MYGLOBAL[$i]} done fi echo ${MYGLOBALLOCK[$1]} } </code></pre> <p>will require <code>--sync</code> flag for re-reading <em>rendez-vous</em> in order to let you look about each fields from the same <em>snapshot</em>.</p> <pre><code>$ getMyGlobalVar --sync idle 362084.12 $ getMyGlobalVar idle 362084.12 $ getMyGlobalVar rand 1533 $ getMyGlobalVar rand 1533 $ getMyGlobalVar --sync rand 43256 $ getMyGlobalVar idle 362127.63 </code></pre> <h2>Full useable sample:</h2> <p>There is a full sample: <a href="http://f-hauri.ch/vrac/bash_ipc_demo.txt" rel="noreferrer">bash_ipc_demo</a></p> <p>You could use by:</p> <pre><code>wget http://f-hauri.ch/vrac/bash_ipc_demo source bash_ipc_demo back_func help Usage: bash [-q] [start|stop|restart|status|get|dump|help] back_func status Background loop function is not running. back_func start back_func status Background loop function (19939) is running. </code></pre> <p>From there, if you <code>source bash_ipc_demo</code> in another terminal, you could do the list into them.</p> <p>You could even close the first terminal.</p> <pre><code>back_func dump backFunc_count 13 backFunc_now 2016-04-06 17:03:19 backFunc_pid 19939 backFunc_running yes backFunc_start 2016-04-06 17:03:07 cpu_numcores 2 loadavg_15min 0.44 loadavg_1min 0.66 loadavg_5min 0.54 loadavg_active 1 loadavg_last_pid 20005 loadavg_process 650 random 3714432 uptime_idle 425499.43 uptime_up 495423.53 uptime_usage1sec 9.90 uptime_usage 57.06 uptime_useGraph 57.06 8.91 7.50 6.93 12.00 9.41 7.84 9.90 7.50 11.88 7.92 9.31 9.90 </code></pre> <p>Then, you could get one value</p> <pre><code>back_func get backFunc_pid newVar echo $newVar 19939 </code></pre> <p>or build a <em>last minute</em> graph:</p> <pre><code>back_func get uptime_useGraph vars;path="M0,0";x=0;for y in $vars;do printf -v step "L%.2f,%.2f L%.2f %.2f " $((x++*3)) $y $[3*x] $y path+=" $step" done;path+=" L$((3*x)),0 z";echo "&lt;svg width=\"180\" height=\"100\"&gt; &lt;rect style=\"fill: #DDD;\" width=\"180\" height=\"100\"&gt;&lt;/rect&gt; &lt;path style=\"fill: #08B;\" transform=\"matrix(1,0,0,-1,0,100)\" d=\"$path\"&gt;&lt;/path&gt;&lt;/svg&gt;" | inkscape -z --export-png=lastMinute.png /dev/stdin;eog lastMinute.png ** (inkscape:22147): WARNING **: Format autodetect failed. The file is being opened as SVG. Background RRGGBBAA: ffffff00 Area 0:0:180:100 exported to 180 x 100 pixels (90 dpi) Bitmap saved as: lastMinute.png </code></pre> <p>Then:</p> <pre><code>back_func stop back_func get backFunc_end 2016-04-06 17:16:08 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload