On commence par écrire une fonction qui extrait un code de retour raisonable pour une commande qui se termine éventuellement par un signal, puis une variante de waitpid qui retourne un code de retour plutôt qu'un statut.
      
let signal_retcode = 126 (* Fils a été tué par un signal *)
let return_status = function
  | WEXITED ret -> ret
  | WSIGNALED s ->
      eprintf "Process was killed by signal %d\n" s;
      flush Pervasives.stderr;
      signal_retcode
  | WSTOPPED s ->
      eprintf "Process was stopped by signal %d\n" s;
      flush Pervasives.stderr;
      signal_retcode;;

let wait_for_son pid =
  let pidstatus = Unix.waitpid [Unix.WUNTRACEDpid in
  return_status status;;
La commande command_wait lance un programme et le père attend son retour.
      
let command_wait cmd args =
  match Unix.fork () with
  | 0 -> mon_exec cmd args
  | pid -> wait_for_son pid;;