Currently I do:
while [ -d "/proc/$PID" ]; do
sleep 1
done
To wait for a process to exit. If I would replace it with:
tail --pid=$PID -f /dev/null
Would that be more efficient for the CPU? Or does tail just use the same polling under the hood?
Assuming
$PIDruns for 10 secs ...whileloop conducts 10 tests and invokes thesleepbinary 10 timestailbinary or a singlewaitbuiltinI'd expect the
tailandwaitoperations to be more 'efficient'.Short of reviewing the source code for
tailwe can run some simple tests ...Timings for a 10 sec test:
NOTES:
cygwin/bash v.4.4.12in a Win10 VM running on i7-1260Ptestme.1- 10x runs showed 5x each for 2%CPU and 3%CPU (call it 2.5% CPU)testme.2- 10x runs showed 9x 0%CPU and 1x 1%CPU (call it 0.1% CPU)testme.3- 10x runs showed 9x 0%CPU and 1x 1%CPU (call it 0.1% CPU)testme.3(akawait) is not a valid option if waiting for some other process's child process to completeThe overhead for the
while/sleeploop increases noticeably with time while we see minimal-to-no increases in overhead for thetailandwaitoptions; eg, 60 sec tests: