Fork a child process and share parent's memory

1,581 views
Skip to first unread message

Sharon Rosner

unread,
Nov 20, 2011, 1:51:22 AM11/20/11
to nod...@googlegroups.com
Hi everybody

This might be a silly question, but I haven't found anything in the docs. Is there a possibility of forking a child process in node.js so it will have a copy of the parent process memory, i.e. copy-on-write?

Sharon

Ryan Dahl

unread,
Nov 20, 2011, 2:22:19 AM11/20/11
to nod...@googlegroups.com

No

Matt

unread,
Nov 20, 2011, 3:10:20 AM11/20/11
to nod...@googlegroups.com
To be fair, there's nothing stopping you writing an extension that just implements fork() (which would give you what you want, on Unix), but the problems are that some (all?) of the event loops aren't always too happy after fork(), so Node doesn't implement it.

I see no reason someone couldn't put a fork module onto npm, with a big "caveat emptor" sign over it.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Nathan Rajlich

unread,
Nov 20, 2011, 2:10:39 PM11/20/11
to nod...@googlegroups.com

Egor Egorov

unread,
Nov 21, 2011, 2:37:36 PM11/21/11
to nod...@googlegroups.com
I have used my own fork of node-ipc + a very simple fork() implementation. I used unix signals to send information about updated buffers. 

Yes, it is possible and it works. However clumsy. 

Egor Egorov

unread,
Nov 21, 2011, 2:38:10 PM11/21/11
to nod...@googlegroups.com
Depends on the definition. No big deal to use shm for Buffer pointers. 

Ben Noordhuis

unread,
Nov 21, 2011, 2:45:52 PM11/21/11
to nod...@googlegroups.com

That it works is purely accidental - Node is not fork safe.

fork() only forks the calling thread. Node processes file I/O and
other things in worker threads. If you fork() while one of the other
threads holds a crucial lock, you're dead.

Matt

unread,
Nov 21, 2011, 4:57:20 PM11/21/11
to nod...@googlegroups.com
If you're on node 0.4.x though can't you call ev_loop_fork() after forking to re-setup ev?

Ben Noordhuis

unread,
Nov 21, 2011, 5:12:11 PM11/21/11
to nod...@googlegroups.com

No. libeio isn't fork-safe either.

Bert Belder

unread,
Nov 21, 2011, 6:30:38 PM11/21/11
to nodejs
On 21 nov, 23:12, Ben Noordhuis <i...@bnoordhuis.nl> wrote:
> On Mon, Nov 21, 2011 at 22:57, Matt <hel...@gmail.com> wrote:
> > On Mon, Nov 21, 2011 at 2:45 PM, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

>
> >> On Mon, Nov 21, 2011 at 20:37, Egor Egorov <egor.ego...@gmail.com> wrote:
> >> > I have used my own fork of node-ipc + a very simple fork()
> >> > implementation. I
> >> > used unix signals to send information about updated buffers.
> >> > Yes, it is possible and it works. However clumsy.
>
> >> That it works is purely accidental - Node is not fork safe.
>
> >> fork() only forks the calling thread. Node processes file I/O and
> >> other things in worker threads. If you fork() while one of the other
> >> threads holds a crucial lock, you're dead.
>
> > If you're on node 0.4.x though can't you call ev_loop_fork() after forking
> > to re-setup ev?
>
> No. libeio isn't fork-safe either.

If you really want to do it you'll probably have more luck using named
shared memory. See `man 7 shm_overview`. But don't expect anyone to
support it :-)

Matt

unread,
Nov 21, 2011, 11:56:18 PM11/21/11
to nod...@googlegroups.com
For anyone curious in trying this more, the relevant documentation is here: http://pod.tst.eu/http://cvs.schmorp.de/libeio/eio.pod#FORK_SUPPORT

Egor Egorov

unread,
Nov 22, 2011, 6:27:39 AM11/22/11
to nod...@googlegroups.com

Hmm... I think I've seen some "node-fork" modules that also close file i/o threads by calling some internal magic. 

Well, at least there is an official fork/spawn methods in child_process that shows how to fork() correctly. 

What if I also use shm with them? 
Reply all
Reply to author
Forward
0 new messages