Page 1 of 1

(timer) fails on (fork)

Posted: Wed Apr 13, 2011 3:14 am
by kanen
Say you have :

Code: Select all

;
; 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.

Re: (timer) fails on (fork)

Posted: Wed Apr 13, 2011 4:32 am
by Lutz
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.

Code: Select all

; 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))

Code: Select all

~> 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.

Re: (timer) fails on (fork)

Posted: Wed Apr 13, 2011 5:49 am
by kanen
Would anything else cause this?

I have removed every (sleep) from my code and I still have the timer failing when I fork.

Re: (timer) fails on (fork)

Posted: Wed Apr 13, 2011 2:27 pm
by 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.

Re: (timer) fails on (fork)

Posted: Tue Apr 19, 2011 5:15 am
by kanen
Or... a stray (sleep) call in one of the twenty modules.

Not that I'd ever admit it. :)
Lutz wrote: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.