From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: Caml-list@yquem.inria.fr Delivered-To: Caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 732A3BBCA for ; Thu, 14 Feb 2008 23:19:18 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAPNMtEfa1uFinmdsb2JhbACQQgEBAQEBBgQGBwoYnlsE X-IronPort-AV: E=Sophos;i="4.25,354,1199660400"; d="scan'208";a="8100966" Received: from smtp.syd.people.net.au ([218.214.225.98]) by mail1-smtp-roc.national.inria.fr with SMTP; 14 Feb 2008 23:19:17 +0100 Received: (qmail 32625 invoked from network); 14 Feb 2008 22:19:15 -0000 Received: from unknown (HELO hendrix.mega-nerd.net) (218.214.64.136) by smtp.syd.people.net.au with SMTP; 14 Feb 2008 22:19:15 -0000 Received: from hendrix (hendrix [192.168.200.99]) by hendrix.mega-nerd.net (Postfix) with SMTP id E2058AB1CB for ; Fri, 15 Feb 2008 09:19:11 +1100 (EST) Date: Fri, 15 Feb 2008 09:19:10 +1100 From: Erik de Castro Lopo To: Caml-list@yquem.inria.fr Subject: Threads, Signals and Events Message-Id: <20080215091910.bb982431.mle+ocaml@mega-nerd.com> Organization: Erik Conspiracy Secret Labs X-Mailer: Sylpheed 2.4.5 (GTK+ 2.12.0; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; ocaml:01 syslog:01 syslog:01 sigterm:01 mli:01 cheers:01 brochure:98 threads:01 threads:01 unix:01 writes:01 behaviour:01 back-end:02 kernel:02 hotmail:97 Hi all, I'm doing some systems programming where I need to use threads (standard threads, not cothreads), signals and events. The program has a couple of things to do: a) Start program X, do a wait on its pid and restart X if it dies and log information to syslog. b) Periodically run other system checks and log results to syslog. Certain types of system failures need to be communicated to program X so that it can modify its behaviour. c) Catch SIGHUP, and on reception, kill program X (send SIGHUP first and if that doesn't work SIGTERM and finally SIGKILL) and then exit itself. Since the system checks can take some time (mostly waiting for the kernel to do stuff rather than intensive processing) and program X needs to be restarted immediately if it dies, using threads seems to be an obvious solution. However, reading between the lines of thread.mli suggests that signals should be blocked on all threads but one which catches signals using Thread.wait_signal and then sends events using the Event module to the other threads. This makes for four threads: - Main thread which waits for SIGHUP and sends an Event when it does. - Monitor thread which waits for program X to exit using Thread.wait_pid. - A syscheck thread which pretty much does its thing and log its results by sending an Event to the logger thread. - Logger thread which receives events from the other threads and writes to syslog. The problem with the above is that the monitor thread needs to wait on both a pid and an Event and I can't see any way to do that. Anybody have any suggestions? This is on Linux btw. Cheers, Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- "An older MS internal whitepaper from August 2000 on switching Hotmail, which MS acquired in 1997, from front-end servers running FreeBSD and back-end database servers running Solaris to a whole farm running Win2K, reads like a veritable sales brochure for UNIX" -- http://www.theregister.co.uk/content/4/28226.html