This commit is contained in:
olli 2024-06-24 23:22:24 +02:00
parent f73f0ea572
commit 567a3ae1a9
2 changed files with 33 additions and 5 deletions

View File

@ -2,9 +2,19 @@ function g_calc {
unset g_calc_result
local g_calc_jobs g_rnd
local g_bc_running=true
mapfile -t g_calc_jobs < <(jobs -r)
g_calc_jobs_v=${g_calc_jobs[*]}
[[ $g_calc_jobs_v =~ bc ]] || unset g_bc_running
# Use bc in backround for multiple bc's running much faster
if [ -z "${g_bc_running}" ]
then
g_kill_all_background_jobs bc-ql
g_kill_all_background_jobs sed
g_rnd=$$
mkdir -p ${g_tmp}/${g_rnd}
[ -p ${g_tmp}/${g_rnd}/bc-in ] || mkfifo ${g_tmp}/${g_rnd}/bc-in
@ -33,7 +43,7 @@ function g_calc {
( exec 4<&- ) 2>/dev/null
( exec 5<&- ) 2>/dev/null
( exec 6<&- ) 2>/dev/null
unset g_bc_running
#unset g_bc_running
return 1
fi
@ -47,7 +57,7 @@ function g_calc {
( exec 4<&- ) 2>/dev/null
( exec 5<&- ) 2>/dev/null
( exec 6<&- ) 2>/dev/null
unset g_bc_running
#unset g_bc_running
return 1
fi

View File

@ -1,6 +1,24 @@
function g_kill_all_background_jobs {
local g_pids
readarray g_pids < <(jobs -p)
[ -n "${g_pids[0]}" ] && kill -9 ${g_pids[*]} >/dev/null 2>&1
[ -z "$1" ] &&
sleep 0.1
g_array "$(jobs -p)" g_pids
if [ -n "${g_pids[0]}" ]
then
if [ -z "$1" ]
then
kill -9 ${g_pids[*]} >/dev/null 2>&1
else
local g_cmdline=$1
local g_proc
local g_pid
for g_pid in "${g_pids[@]}"
do
echo $g_pid
readarray g_proc < "/proc/$g_pid/cmdline"
[ "$g_proc" = "$g_cmdline" ] && kill -9 $g_pid >/dev/null 2>&1
done
fi
fi
return 0
}