newLISP Fan Club

Forum => newLISP in the real world => Topic started by: kanen on April 12, 2011, 08:14:27 PM

Title: (timer) fails on (fork)
Post by: kanen on April 12, 2011, 08:14:27 PM
Say you have :



;
; a define
(define (foo)
 (println "Oh Yeah.")
)
;
; and another
(define (foobar)
 (dolist (s (sequence 1 10))
   (sleep 1000)
 )
)
;
; and a timer:
(define (ImaTimer)
  (foo)
  (timer 'ImaTimer 1)
)
;
; and, some code:
(println "Setting timer...")
(ImaTimer)
(println "Forking...")
(fork (foobar))
;(foobar)
(exit)


The problem is, the timer never fires in the forked process.



But, if you do not fork, changing (fork (foobar)) to just (foobar) the timer works fine.
Title: Re: (timer) fails on (fork)
Post by: Lutz on April 12, 2011, 09:32:02 PM
The timer is cancelled from a SIGALRM generated by 'sleep' in the forked child process. If you  replace the 'sleep' the timer keeps on going.


; a define
(define (foo)
   (println "Oh Yeah.")
)
;
; and another
(define (foobar)
(dolist (s (sequence 1 10))
   (set 'start (time-of-day))
   (while (< (time-of-day) (+ start 1000)))
   (println "foobar")
)
)
;
; and a timer:
(define (ImaTimer)
  (foo)
  (timer 'ImaTimer 1)
)
;
; and, some code:
(println "Setting timer...")
(ImaTimer)
(println "Forking...")
(fork (foobar))






~> newlisp timer
Setting timer...
Oh Yeah.
Forking...
newLISP v.10.3.0 on OSX IPv4/6 UTF-8, execute 'newlisp -h' for more info.

> Oh Yeah.
foobar
Oh Yeah.
foobar
Oh Yeah.
foobar
foobar
Oh Yeah.
foobar
Oh Yeah.
foobar
Oh Yeah.
foobar
Oh Yeah.
Title: Re: (timer) fails on (fork)
Post by: kanen on April 12, 2011, 10:49:25 PM
Would anything else cause this?



I have removed every (sleep) from my code and I still have the timer failing when I fork.
Title: Re: (timer) fails on (fork)
Post by: Lutz on April 13, 2011, 07:27:11 AM
newLISP uses the same fork(), setitimer() and getitimer() functions on all UNIX platforms. Perhaps there are some specifics, how fork() and setitimer() interact on your platform, e.g. some resource contention between the process scheduler and the timer.
Title: Re: (timer) fails on (fork)
Post by: kanen on April 18, 2011, 10:15:01 PM
Or... a stray (sleep) call in one of the twenty modules.



Not that I'd ever admit it. :)


Quote from: "Lutz"newLISP uses the same fork(), setitimer() and getitimer() functions on all UNIX platforms. Perhaps there are some specifics, how fork() and setitimer() interact on your platform, e.g. some resource contention between the process scheduler and the timer.