unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Handling POSIX signals (Guile 1.6)
@ 2002-10-06 17:10 Wolfgang Jährling
  2002-10-08  4:23 ` Wolfgang Jaehrling
  0 siblings, 1 reply; 2+ messages in thread
From: Wolfgang Jährling @ 2002-10-06 17:10 UTC (permalink / raw)


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!



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Handling POSIX signals (Guile 1.6)
  2002-10-06 17:10 Handling POSIX signals (Guile 1.6) Wolfgang Jährling
@ 2002-10-08  4:23 ` Wolfgang Jaehrling
  0 siblings, 0 replies; 2+ messages in thread
From: Wolfgang Jaehrling @ 2002-10-08  4:23 UTC (permalink / raw)


On Sun, Oct 06, 2002 at 07:10:32PM +0200, Wolfgang Jaehrling wrote:
> I have a very interesting problem with signals

The solution turned out to be trivial: Catch and ignore the error that
the `waitpid' in the signal handler throws.  That's all.

Cheers,
GNU/Wolfgang


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-10-08  4:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-06 17:10 Handling POSIX signals (Guile 1.6) Wolfgang Jährling
2002-10-08  4:23 ` Wolfgang Jaehrling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).