display a - 1 - b - 2 - c - 3…- z - 26 by creating 2 process and each process run at once with a random sleep of 1 to 5

旧时模样 提交于 2019-12-23 06:19:12

问题


display a1b2c3...z26 by creating 2 process and each process run seperatley with a random sleep of 1 to 5

is there any way to show off the process 1 has to wait for process 2 respectively p1 will generate abcdef... with sleep $[ ( $RANDOM % 5 ) + 1 ]s p2 will generate 12345..with sleep $[ ( $RANDOM % 5 ) + 1 ]s


回答1:


Process 1 ./abc.sh

> cd /home/sanjaisambu/Sanjai/a1b2/ for j in {a..z} do
>         while  [ -f './syncfile' ]
>         do
>         echo 'process 1 waiting'
>         sleep 1
>         done
>         touch ./syncfile
>         echo -n $j  >> a1b2.txt
>         sleep $[ ( $RANDOM % 5 ) + 1 ]s done

Process 2 123.sh

> #!/bin/bash cd /home/sanjaisambu/Sanjai/a1b2/ for j in $(seq 1 26) do
>         while [ ! -f './syncfile' ]
>         do
>         echo 'process 2 is waiting'
>         sleep 1
>         done
>         echo -n - $j -  >> a1b2.txt
>         rm ./syncfile
>         sleep $[ ( $RANDOM % 5 ) + 5 ]s done

When we Execute

./abc.sh & ./123.sh

Output:

a - 1 -b- 2 -c- 3 -d- 4 -e- 5 -f- 6 -g- 7 -h- 8 -i- 9 -j- 10 -k- 11 -l- 12 -m- 13 -n- 14 -o- 15 -p- 16 -q- 17 -r- 18 -s- 19 -t- 20 -u- 21 -v- 22 -w- 23 -x- 24 -y- 25 -z- 26



来源:https://stackoverflow.com/questions/44795576/display-a-1-b-2-c-3-z-26-by-creating-2-process-and-each-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!