-
let read_procmem pid =
let cin = open_in (Printf.sprintf "/proc/%i/status" pid) in
let matchline s =
close_in cin;
s
;;
-
+let max_mem = ref 0
let rec monitor pid timeout mem =
let p, s = Unix.waitpid [ Unix.WNOHANG ] pid in
if p == 0 then
- if (Unix.gettimeofday() > timeout || (read_procmem pid) >= mem)
+ let current_mem = read_procmem pid in
+ if current_mem >= !max_mem then max_mem := current_mem;
+ if (Unix.gettimeofday() > timeout || current_mem >= mem)
then Unix.kill pid Sys.sigkill
else
let () = Unix.sleep 1 in
else monitor pid timeout mem
;;
-let () =
+let () =
if Array.length Sys.argv < 4 then exit 1
else
try
let mem = int_of_string Sys.argv.(2) in
let command = Array.sub Sys.argv 3 ((Array.length Sys.argv) - 3) in
run command timeout mem;
+ Printf.printf "Child process used %i kB of memory\n%!" !max_mem;
exit 0
with
_ -> exit 2