I'm not really getting how are you sure that setpgid kills your process chain, normally setpgid function will join an existing process group or creates a new process group. (And its not C++)
Anyway following alternative script should work
Pass the process id of your chain's parent process, i.e. in your case its the pid of process B
Its recursively loops over the child processes of a given pid and signals each child process to be killed with 9 (SIGKILL) signal, from below-most order.
#!/bin/sh
function getcpid() {
cpids=`pgrep -P $1|xargs`
#echo "cpids=$cpids"
for cpid in $cpids;
do
#echo "$cpid"
getcpid $cpid
kill -9 cpid
done
}
getcpid $1