Discussion:
[Help-bash] Problems with background jobs
David Niklas
2018-08-29 15:39:32 UTC
Permalink
On Thu, 16 Aug 2018 10:52:32 +0200
Hey! Sorry this is so late, but it seems nobody is helping you and I tend
to be the last to respond.
https://sourceforge.net/p/openocd/mailman/message/36384989/
But the exact problem is not relevant. It is about coordinating
processes with pipes.
1) Create an unnamed pipe, like 'coproc' does, but leaving stdin and
stdout untouched.
mkfifo foo;
2) Start the OpenOCD process as a background job.
openocd --some_option foo &
export OCD="$!";
3) Wait for my OpenOCD script to write to the pipe, so that I know that
OpenOCD is now ready to accept connections.
head -c1 foo &
If OpenOCD dies during initialisation, reading from the pipe will fail,
and the Bash script will exit.
export H="$!";
sleep 15s;
kill $H;
wait $H;

if [ $H -ne 0 ]; then exit; fi
4) Start GDB in the foreground, which connects to OpenOCD. The Bash
script will continue when GDB exits.
gdb -p $OCD;
5) Wait for the OpenOCD background job to finish. We do not want to
leave zombie processes behind.
wait $OCD;
6) Exit with OpenOCD's exit code.
<snip>

exit $?;



I do not have openocd, nor am I aware of how you intend to tell openocd
to access your pipe in order to pipe data so I have not tested all of the
above code. You might have to use a socket instead if you want
bidirectional communication, but I have no experience there so you will
have to wait for someone else to help you.
Even more confusing to me is that you want to run gdb in the foreground
but you also want to wait for the return of openocd!? Both are not
simultaneously possible. Esp. because gdb will prevent openocd from
exiting, or at least that is what happens when my code fouls up. Then I
run the bt command from within gdb.

Sincerely,
David
David Niklas
2018-08-30 20:56:38 UTC
Permalink
On Wed, 29 Aug 2018 11:39:32 -0400
Post by David Niklas
On Thu, 16 Aug 2018 10:52:32 +0200
<snip>
Post by David Niklas
If OpenOCD dies during initialisation, reading from the pipe will
fail, and the Bash script will exit.
export H="$!";
sleep 15s;
kill $H;
wait $H;
if [ $H -ne 0 ]; then exit; fi
<snip>

Oops! That should read:
if [ $? -ne 0 ]; then exit; fi

Sincerely,
David

Loading...