My problem is to get a function that tells me whether or not a
the difference between two 24-hour times is greater than a given
grace period.
Examples
When the grace period is 4 hours and a job started at 4 (24 hour
clock time) then it
needs to be killed when the 24 hour time is 8. If it starts at
11pm or
23 (on the 24 hour clock) then it needs to be stopped at 3am.
Quality Requirement
We do not want to be sued for stopping jobs before the announced time.
But we can not afford to let jobs accumulate.... the system crashes.
Formal Analysis
We will use the C/C++ notation for remainder or modulus:
Use C.expressions. (above) |-(mod): For int i,j>0, i%j::=i - j*(i/j).
We can show that
Net
Given D and g we want to determine if d > g or if d<=g.
We have been told:
So we deduce:
Now
Proof of 5
Let
Proof of 15
Let
Proof of 18
Let
int hangable(int T, int S, int g)
{
if (T-S < 0)
return T-S+24 < g;
else
return T-S < g;
}
Actual Implementation
The "ftp.hangman" program is a shell script:
: Hang up day old FTP sessions. Uses BSD -u option, RTFM
tmp=/tmp/ftp.hang.$$
ps -axu >$tmp
grep "^ftp" <$tmp |
awk '$9!~/:/{print $2}' |
while read pid; do kill -9 $pid; done
rm $tmp
This works because the set of running jobs is listed in tmp by the ps -axu command. The first "word" on a line is the owner of the job, and so grep "^ftp" extracts only the FTP processes. The ps -axu format puts the time or month as the 9th word on the line and the process Id as the second word. Hence the awk command selects the 24 or more hour old processes and outputs their process Identifiers. This set of Ids (the old ftp processes) are read in, one at a time by the while - do - done loop and killed.
Clearly the only change is check for the presence of a ":" in the 9th word, extract the hour (when it started) and compare:
awk '$9!~/:/{print $2}
$9~/:/{S=substr($9,0,2);
if(S-T>=g || S-T<0 && (S-T+24)>g)print $2;
}' g=$grace T=`date +%H`
Testing
There is a hopeful theory that a program that is properly developed
does not need testing. Given the unprovable state of most software
tools this is a dangerous theory. In this case testing showed up
a bug caused by the weak typing in 'awk'. The correct statement
to select outstanding jobs is:
if(S-T>=0+g || S-T<0 && (S-T+24)>0+g)print $2;since g is interpreted as a string and so lexographic rather than numeric ordering is used in the comparisons.
Conclusion
Notice that the condition was not wrong. It is the coding that
fell into a well known and avoidable trap.
. . . . . . . . . ( end of section What jobs have been running too long) <<Contents | End>>
Notes
(ei): The "existential instanciation" rule of inference
[ ei in logic_2_Proofs ]
(eg): The "existential generalization" rule of inference
[ eg in logic_2_Proofs ]