Operating System - HP-UX
1837738 Members
3752 Online
110118 Solutions
New Discussion

Re: return value of background process

 
Praveen Bezawada
Respected Contributor

return value of background process

Hi
I have a C program which return a non zero value. When I run a.out in a shell program in the background and do echo $? I get 0 and not the return value.
./a.out &
echo $?

my question is how can I get the return value. Is it possible to get the return value if I have the PID of the process.
Thanks in advance
Praveen
5 REPLIES 5
Carlos Fernandez Riera
Honored Contributor

Re: return value of background process

No. You can not get the returned value of a backuground process.... unless you use some trick.

write a a.sh
---
a.out
echo $? > /tmp/$$.return
---

a.sh &
read STATUS < /tmp/$$.return
rm /tmp/$$.return
unsupported
Bruce Regittko_1
Esteemed Contributor

Re: return value of background process

Hi,

If you are running the c program in the background, $? will not contain the exit status of the program. You can get the pid of the last background job from the $! variable but I don't know of a way to get the exit status of that process.

Do you have access to the c source code? If so, you can save the exit status in a file or something similar. Just make sure that the process has terminated before the script tries to access the exit status if you have saved it in a file.

--Bruce
www.stratech.com/training
CHRIS_ANORUO
Honored Contributor

Re: return value of background process

Use "fg" to bring the process to the foreground and $* to get the return values.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Dan Hetzel
Honored Contributor

Re: return value of background process

Hi Praveen,

You won't be able to get that value unless you
call your c program from within a shell script, check the $? variable within the same shell script and run the whole stuff in the background.

like this:

#!/usr/bin/sh
a.out # your c program
echo $? > /tmp/myreturn$$
# the return value of the a.out prog

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Carlos Fernandez Riera
Honored Contributor

Re: return value of background process

There is a little error in my previous response


write a a.sh
---
a.out
echo $? > /tmp/$$.return
---

a.sh &
LASTPID=$!
while [ ! -f $LASTPID.return]
do
#...
sleep 5
done
read STATUS < /tmp/$LASTPID.return
rm /tmp/$LASTPID.return

Sorry.
unsupported