Difference between revisions of "Bash processes"
From Teknologisk videncenter
(Created page with "=example= <source lang=bash line> #!/bin/bash count() { NAME=$1 COUNT_TO=$2 COUNTER=0 echo -e "My name is $NAME my PID is $BASHPID" # $$ does...") |
(No difference)
|
Revision as of 07:58, 16 December 2022
example
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 -le $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 countet to 4
Ulla signing off
Hans countet to 5
Hans countet to 6
Hans signing off
Main thread signing off