Need help on Assigning a Array variable from Background Functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on Assigning a Array variable from Background Functions
# 1  
Old 07-23-2014
Need help on Assigning a Array variable from Background Functions

I have a question on how can I assign a output of a function to a variable which is executed in background.

Here is my example

Code:
$ cat sample_program.sh
#!/bin/ksh

exec_func () {
sleep 1
v=`expr $1 + 100`
print $v
}

export OUT_ARR
date
for i in 1 2
do
OUT_ARR[$i]=`exec_func $i` &
done

wait
date

print ${OUT_ARR[@]}

$ ksh -x sample_program.sh
+ export OUT_ARR
+ date
Tue Jul 22 21:31:39 PDT 2014
+ exec_func 1
+ sleep 1
+ exec_func 2
+ sleep 1
+ wait
+ expr 1 + 100
+ v=101
+ print 101
+ OUT_ARR[1]=101
+ expr 2 + 100
+ v=102
+ print 102
+ OUT_ARR[2]=102
+ date
Tue Jul 22 21:31:40 PDT 2014
+ print

$

From the above program and output you can see that finally I was not able to print the value of the array variable OUT_ARR.

I am able to do the same if I am not executing the function in background.
See below execution

Code:
$ cat sample_program.sh
#!/bin/ksh

exec_func () {
sleep 1
v=`expr $1 + 100`
print $v
}

export OUT_ARR
date
for i in 1 2
do
OUT_ARR[$i]=`exec_func $i`
done

wait
date

print ${OUT_ARR[@]}

$ ksh -x sample_program.sh
+ export OUT_ARR
+ date
Tue Jul 22 21:33:29 PDT 2014
+ exec_func 1
+ sleep 1
+ expr 1 + 100
+ v=101
+ print 101
+ OUT_ARR[1]=101
+ exec_func 2
+ sleep 1
+ expr 2 + 100
+ v=102
+ print 102
+ OUT_ARR[2]=102
+ wait
+ date
Tue Jul 22 21:33:31 PDT 2014
+ print 101 102
101 102
$

See this time I am able to see the output but the time taken is more. I want to execute the functions in Background so that the time taken to complete the loop is less.

Any help is greatly appreciated.

Thanks in Advance!

Mohan Kumar CS

---------- Post updated at 11:54 PM ---------- Previous update was at 11:40 PM ----------

I guess i found the answer.

i just changed the way i pushed the function execution within the back quotes.

Code:
OUT_ARR[$i]=`exec_func $i &`

This made the magic
Here is the output

Code:
$ cat sample_program.sh
#!/bin/ksh

exec_func () {
sleep 1
v=`expr $1 + 100`
print $v
}

export OUT_ARR
date
for i in 1 2
do
OUT_ARR[$i]=`exec_func $i &`
done

wait
date

print ${OUT_ARR[@]}

$ ksh -x sample_program.sh
+ export OUT_ARR
+ date
Tue Jul 22 21:52:59 PDT 2014
+ exec_func 1
+ sleep 1
+ expr 1 + 100
+ v=101
+ print 101
+ OUT_ARR[1]=101
+ exec_func 2
+ sleep 1
+ expr 2 + 100
+ v=102
+ print 102
+ OUT_ARR[2]=102
+ wait
+ date
Tue Jul 22 21:53:01 PDT 2014
+ print 101 102
101 102
$

# 2  
Old 07-23-2014
Starting a pipeline in the background creates a new shell execution environment. Variables set in that new shell execution environment are not visible in the parent (foreground) shell execution environment.

There are several ways things like this can be done. One way that works if the values you want to produce in the background are always less than or equal to 255, is:
Code:
#!/bin/ksh
exec_func() {
	sleep 1
	printf 'exec_func exiting with %d\n' "$(( $1 + 100 ))"
	exit $(( $1 + 100 ))
}

date
for i in 1 2
do	exec_func $i&
	pid[i]=$!
done
for i in 1 2
do	wait ${pid[i]}
	OUT_ARR[i]=$?
done
date
print ${OUT_ARR[@]}

which produces the output:
Code:
Tue Jul 22 22:27:03 PDT 2014
exec_func exiting with 102
exec_func exiting with 101
Tue Jul 22 22:27:04 PDT 2014
101 102

but the order of the two lines in red may vary from run to run (and may be intermixed on a multi-processor system).
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning DOM Object value to an array

Hi, I have the following code that makes use of a URL that I store in a variable then create a document object below to work on it. $dom = new DOMDocument; @$dom->loadHTML($html); $links = $dom->getElementsByTagName('a'); $links = $dom->getElementsByTagName('a'); ... (0 Replies)
Discussion started by: mojoman
0 Replies

2. Shell Programming and Scripting

Assigning * as value in a ksh array

I want to extract each and single character from a password string and put it in an array. I tried this : set -A password "echo $passwd | awk '{for (i=1; i<=length($1); i++) printf "%s ",substr($1,i,1)}'` It's working as long that the password string doesn't contains any * I tried a few... (5 Replies)
Discussion started by: ce9888
5 Replies

3. Shell Programming and Scripting

How to run multiple functions in Background in UNIX Shell Scripting?

Hi, I am using ksh , i have requirement to run 4 functions in background , 4 functions call are available in a case that case is also in function, i need to execute 1st function it should run in background and return to case and next i will call 2nd function it should run in background and... (8 Replies)
Discussion started by: karthikram
8 Replies

4. Emergency UNIX and Linux Support

Assigning zero to element of ksh array.

set -A matched #find referenced files. for i in ${file_names_html} do counter_j=0 for j in ${file_names_minus_index} do match=`cat $i | grep... (1 Reply)
Discussion started by: robin_simple
1 Replies

5. Shell Programming and Scripting

background functions doesn't display their output

hello there, there's a function, in my shell script, i'd like to run in the background. here's an example: log() { local SELF=${0##*/} tty -s && echo ": $*" } some_func() { # do something log "This text is not displayed in the terminal's output when running in the... (1 Reply)
Discussion started by: Shedon
1 Replies

6. Programming

[ C ] multidemensional array pass to functions

Please excuse my ineptitude for a bit as I've been spoiled for the past few months with only writing perl code instead of C. So ok, I've been thinking about some code to change the crc32 values that are held within central directory headers of zip files. Because I'm lazy I decided to just... (3 Replies)
Discussion started by: VRoemer
3 Replies

7. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

8. Shell Programming and Scripting

Assigning the values to an Array

hi every body, i donot know how to assign a array varible with a file see i having file more file property1 Name property2 Address the above two line are tab Space seperated between the property and its value i want to seperate it and assign to... (1 Reply)
Discussion started by: kkraja
1 Replies

9. Shell Programming and Scripting

perl: Assigning array values..

I have to add a variable value to an array, something like this: ...... @my_array_name = $value_of_this_variable; This doesnt seem to work, any ideas why? Thanks! (4 Replies)
Discussion started by: looza
4 Replies

10. UNIX for Dummies Questions & Answers

Assigning values to an array

The way I've been using arrays currently have been: #!/bin/ksh set -A myArray myArray=value1 myArray=value2 myArray=value3 myArray=value4 Is there a way I can assign values to an array that will automatically place the value into the next element in the array like: myArray=value1... (4 Replies)
Discussion started by: yongho
4 Replies
Login or Register to Ask a Question