Re: Catching Break?

From: Ian T Zimmerman (itz@transbay.net)
Date: Thu Jan 28 1999 - 18:33:20 MET


Date: Thu, 28 Jan 1999 09:33:20 -0800
Message-Id: <199901281733.JAA05913@kronstadt.transbay.net>
From: Ian T Zimmerman <itz@transbay.net>
To: caml-list@inria.fr
Subject: Re: Catching Break?

> Date: Thu, 28 Jan 1999 12:14:11 +0100
> From: Xavier Leroy <Xavier.Leroy@inria.fr>
> X-Gnus-Article-Number: 122 Thu Jan 28 07:47:34 1999
>
> > let suicide() =
> > begin Unix.kill (Unix.getpid()) Sys.sigint; 1 end
> > let id x = x
> > let suicidal = try
> > (suicide(), id 0)
> > with Sys.break -> (0, 0)

> First of all, that should be Sys.Break (an exception constructor),
> not Sys.break. It doesn't work because of the right-to-left
> evaluation order (id 0 is evaluated before suicide()).
>
> > let suicidal = try
> > id (suicide())
> > with Sys.break -> 0
> Works fine here (with Sys.Break of course).
>
> One more comment: rather than sending yourself a signal, then turn
> the signal into an exception via the signal handler (as
> Sys.catch_break does), why not throw the exception directly?
> E.g. just raise Sys.Break when your program wishes to commit
> suicide. Exceptions are much nicer for synchronous notification;
> signals are a pain and only required for asynchronous notification
> (such as the user pressing ctrl-C).

Ok, I see that I'll have to start posting my real code, because I
still can't make it work despite (imperfect) analogy with the example.

let remove_pair prefix pid =
  let spid = string_of_int pid in
  let iname = prefix ^ spid ^ ".i"
  and oname = prefix ^ spid ^ ".o" in
  Sys.remove iname; Sys.remove oname

let open_pair prefix pid respond =
  let spid = string_of_int pid in
  let iname = prefix ^ spid ^ ".i"
  and oname = prefix ^ spid ^ ".o"
  and iflags = [Unix.O_RDONLY ; Unix.O_NONBLOCK]
  and oflags = [Unix.O_WRONLY] in
  let id x = x in
  Sys.catch_break true;
  let (ind, outd) = try if respond then
    let outd = Unix.openfile iname oflags 0 in
    let ind = Unix.openfile oname iflags 0 in
    id (ind, outd)
  else
    let ind = Unix.openfile iname iflags 0 in
    let outd = Unix.openfile oname oflags 0 in (* HERE! *)
    id (ind, outd)
  with Sys.Break ->
    if not respond then remove_pair prefix pid;
    Sys.catch_break false; exit 0;
    assert false in
  Sys.catch_break false; (ind, outd)

The files referred to by [io]name are named pipes; the write open
hangs until the other side is opened, which may never happen. So as
to have a chance to clean up (namely remove the pipes), I must allow
_and_ catch Control-C as I'm trying to. But it doesn't work; if I hit
Control-C while blocked in the open marked, I never get into the with
handler.

-- 
Ian T Zimmerman                        <itz@transbay.net>
I came to the conclusion that what was wrong about the guillotine
was that the condemned man had no chance at all, absolutely none.
Albert Camus, _The Outsider_



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:18 MET