guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Handling POSIX signals (Guile 1.6)


From: Wolfgang Jährling
Subject: Handling POSIX signals (Guile 1.6)
Date: Sun, 6 Oct 2002 19:10:32 +0200 (MEST)

Hi!

I have a very interesting problem with signals (I'm using Guile 1.6 on
GNU/Linux): I want to handle SIGCHLD so that I can respawn several
programs as soon as they terminate, but I also want to execute shell
commands on other places.

I used Guile's (system ...), but the result was that my SIGCHLD
handler got called, but there was no process for (waitpid WAIT_ANY)
anymore, which in my understanding makes sense as Guile runs signal
handlers only in "safe" situations and (system ...) already collects
(i.e. waits for) the child process before the handler gets called.

Here is what I tried so far:

I came up with the idea of using (throw ...) in the SIGCHLD handler
and catching it in my own procedure that otherwise acts like (system
...).  It looks like this:

(define (my-system command)
  (catch 'process-terminated
         (lambda ()
           (let ((pid (primitive-fork)))
             (if (zero? pid)
                 (execl "/bin/sh" "sh" "-c" command)
                 ;; Wait for the SIGCHLD handler to (throw ...).
                 (let loop ()
                   (sleep 1)
                   (loop)))))
         (lambda (key status)
           status)))

The SIGCHLD handler does the following (I snipped a few unimportant
parts and replaced them with (...) here):

(define (respawn-service signum)
  (call/ec (lambda (return)
             (let* ((wait-result (waitpid WAIT_ANY))
                    (pid (car wait-result))
                    (status (cdr wait-result)))
               ;; Try to find what we want to respawn.
               (for-each-service
                (lambda (serv)
                  (and (respawn? serv)
                       (running? serv)
                       (...)
                       ;; We found it.
                       (begin
                         (...)
                         (return #t)))))
               ;; Nothing to respawn, pass it to `dmd-system'.
               (throw 'process-terminated status)))))

This works for the first time, but the handler will not be entered a
second time.  I think the reason for this is obvious: The signal
handler has never actually returned, it has just thrown
'process-terminated to a place outside of the handler.

Any suggestions?

Cheers,
GNU/Wolfgang

-- 
+++ GMX - Mail, Messaging & more  http://www.gmx.net +++
NEU: Mit GMX ins Internet. Günstige DSL- & Modem/ISDN-Tarife!





reply via email to

[Prev in Thread] Current Thread [Next in Thread]