Difference between revisions of "Bash processes"
From Teknologisk videncenter
m (→example) |
m (→example) |
||
Line 1: | Line 1: | ||
=example= | =example= | ||
− | See same example in [[ | + | See same example in [[Fork system call#Example of creating several processes|C]] |
<source lang=bash line> | <source lang=bash line> | ||
#!/bin/bash | #!/bin/bash |
Latest revision as of 07:43, 17 December 2022
example
See same example in C
1 #!/bin/bash
2
3 count() {
4 NAME=$1
5 COUNT_TO=$2
6 COUNTER=0
7 echo -e "My name is $NAME my PID is $BASHPID" # $$ does not show subshell PID
8 while test $COUNTER -lt $COUNT_TO
9 do
10 sleep 1
11 let COUNTER=COUNTER+1
12 echo -e "$NAME countet to $COUNTER"
13 done
14 echo -e "$NAME signing off"
15 }
16
17 #Main
18 echo -e "I am the main function and my PID is $BASHPID"
19
20 count "Hans" 5 &
21 count "Ulla" 3 &
22
23 wait # Wait for subshells to finish
24 echo -e "Main thread signing off"
Gives the following output
My name is Hans my PID is 813980
My name is Ulla my PID is 813981
Hans countet to 1
Ulla countet to 1
Ulla countet to 2
Hans countet to 2
Ulla countet to 3
Hans countet to 3
Hans countet to 4
Ulla signing off
Hans countet to 5
Hans signing off
Main thread signing off