useful at_exit idiom

A

ara.t.howard

i'm constantly struggling with the fact that children need to use exit! to
prevent exit handlers registered in the parent being called twice, for example:


harp:~ > cat a.rb
at_exit{ puts 'parent' }

fork{
at_exit{ puts 'child' }
} and Process.wait


harp:~ > ruby a.rb
child
parent
parent


arggh. this is a neat work-around:


harp:~ > cat a.rb
at_exit{ puts 'parent' }

fork{
at_exit{ exit! }
at_exit{ puts 'child' }
} and Process.wait


harp:~ > ruby a.rb
child
parent


i'm sure i'm not the first one to think of this, but it sure is handy to
separate the parent's handlers from the child's... anyone see issues with
this?

-a
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: useful at_exit idiom"
on Tue, 10 Oct 2006 13:21:21 +0900, (e-mail address removed) writes:
|

|i'm constantly struggling with the fact that children need to use exit! to
|prevent exit handlers registered in the parent being called twice,

|i'm sure i'm not the first one to think of this, but it sure is handy to
|separate the parent's handlers from the child's... anyone see issues with
|this?

linux man atexit(3) says:

When a child process is created via fork(), it inherits copies
of the its parents registrations. Upon a successful call to one
of the exec() functions, all registrations are removed.

so I think this is expected behavior for most of the cases, since Ruby
inherits many behaviors from C libraries.

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top