We first define a function generating a unique identifier for each process.
let gen_id = let cpt = ref 0 in fun () -> incr cpt; !cpt
We define a global signal to_kill
on which we will send the
id of the processes to kill.
signal to_kill;;
We define a process killable
such that killable
p
associates an id to the process p
given as
argument and executes it. When the id associated to p
is
emitted on to_kill
, the execution of p
is
stopped.
let process killable p = let id = gen_id () in print_endline ("["^(string_of_int id)^"]"); do run p until to_kill(ids) when List.mem id ids done
The identifier id
is generated then the
process p
is executed. If during the execution, the
signal to_kill
is emitted, the variable ids
will be assigned the list of processes to kill, thanks to
the do/until/when
construct. If id
belongs
to this list (List.mem id ids
) then the execution is
stopped otherwise execution continues.