Consider this function intended to kill all running instances of a subprocess:
killAllFoobars()
{
pids = getRunningFoobars();
foreach ( pids as p ) {
killOneFoobar(p);
}
return TRUE;
}
Where should I check that in fact all Foobars were killed? Should killOneFoobar() check that its Foobar is now killed? Should killAllFoobars() check that there are no more running Foobars before returning TRUE (seems a bit disingenuous to return TRUE without checking). Should the function calling killAllFoobars() check?
Consider that the check is an expensive operation, so should not be done 'just whenever' but rather only when necessary.
killOneFoobar()that its own Foobar has been killed. ThuskillAllFoobars()will (try to) kill only those Foobars running when the function was called. Please post your comment as an answer so that I might accept it. Thanks! – dotancohen Jan 24 at 14:56return TRUE;is wrong anyway as it's the only return value, so it could be a void function instead. AftergetRunningFoobars()you have zero or more pids. And for each existing pid you should initiate the kill and wait for it's completion (fork or thread?). – ott-- Jan 24 at 15:22