(timer) fails on (fork)

Started by kanen, April 12, 2011, 08:14:27 PM

Previous topic - Next topic

kanen

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.
. Kanen Flowers http://kanen.me[/url] .

Lutz

#1
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.

kanen

#2
Would anything else cause this?



I have removed every (sleep) from my code and I still have the timer failing when I fork.
. Kanen Flowers http://kanen.me[/url] .

Lutz

#3
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.

kanen

#4
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.
. Kanen Flowers http://kanen.me[/url] .