what is the difference between child/callback and fork that is used in Perl

95 views
Skip to first unread message

Zeev Atlas

unread,
Jan 12, 2015, 10:41:10 AM1/12/15
to nod...@googlegroups.com

I am new to JavaScript and node.js, but I have a lot of experience in other languages (Perl for example.) Could somebody please explain or point me to documentation about what is the difference between child/callback and fork that is used in Perl. Is there any conceptual difference, any improvement or is it basically one and the same?

Thank you
ZA

Aria Stewart

unread,
Jan 12, 2015, 11:06:28 AM1/12/15
to nod...@googlegroups.com

On Jan 12, 2015, at 10:41 AM, Zeev Atlas <zat...@gmail.com> wrote:

I am new to JavaScript and node.js, but I have a lot of experience in other languages (Perl for example.) Could somebody please explain or point me to documentation about what is the difference between child/callback and fork that is used in Perl. Is there any conceptual difference, any improvement or is it basically one and the same



Perl's fork starts a whole new unix process; communication with the parent is only via a status code.

Callbacks are a much more internal structure; they're like a sub reference in perl, passed to a sub that starts IO to be called when the IO has finished. 

Aria

Ryan Schmidt

unread,
Jan 12, 2015, 12:13:11 PM1/12/15
to nod...@googlegroups.com
On Jan 12, 2015, at 10:05 AM, Aria Stewart wrote:
But of course you *can* start a whole new unix process in node if you want to, using child_process:

http://nodejs.org/api/child_process.html


// ravi

unread,
Jan 12, 2015, 12:15:07 PM1/12/15
to nod...@googlegroups.com
On Jan 12, 2015, at 10:41 AM, Zeev Atlas <zat...@gmail.com> wrote:
>
> I am new to JavaScript and node.js, but I have a lot of experience in other languages (Perl for example.) Could somebody please explain or point me to documentation about what is the difference between child/callback and fork that is used in Perl. Is there any conceptual difference, any improvement or is it basically one and the same?
>

“child” and “callback” are different things. Can you clarify your question a bit?

—ravi



Zeev Atlas

unread,
Jan 12, 2015, 12:29:58 PM1/12/15
to nod...@googlegroups.com
The sample code I've seen is starting child process and giving it a reference to a callback function, so I perceived it as something like a 'fork' plus 'and once you finish, please call back' which is an ingenious idea but conceptually it is a fork, hence my question.
Is there any other use for callback mechanism?
Thanks
ZA 

Aria Stewart

unread,
Jan 12, 2015, 12:35:01 PM1/12/15
to nod...@googlegroups.com

> On Jan 12, 2015, at 12:29 PM, Zeev Atlas <zat...@gmail.com> wrote:
>
> The sample code I've seen is starting child process and giving it a reference to a callback function, so I perceived it as something like a 'fork' plus 'and once you finish, please call back' which is an ingenious idea but conceptually it is a fork, hence my question.
> Is there any other use for callback mechanism?

Yeah. The difference is in the separation of the task: a unix process shares nothing, and returns nothing but a status code. You can do more with plumbing some streams together, but that's about the crux of it.

With callbacks, this is more integrated in the runtime: The operations running and the callbacks all execute in the same process, and can share access to variables.

Conceptually, at 30,000 feet there are a lot of similarities, but as you get closer, the differences become very apparent.

Aria

Ryan Schmidt

unread,
Jan 12, 2015, 1:04:24 PM1/12/15
to nod...@googlegroups.com
On Jan 12, 2015, at 11:29 AM, Zeev Atlas wrote:
>
> The sample code I've seen is starting child process and giving it a reference to a callback function, so I perceived it as something like a 'fork' plus 'and once you finish, please call back' which is an ingenious idea but conceptually it is a fork, hence my question.
> Is there any other use for callback mechanism?

Callbacks are used constantly in node. You probably won't write much code in node that doesn't use a callback in some way. You tell node to do something, and give it a callback function to call when it's done doing it.

Spawning a child process is done only in special situations. For example, maybe you have a command line program you want to run to get its output. So you tell node to spawn the child process and call a callback with the output when it's done running it.

Matt

unread,
Jan 12, 2015, 1:13:48 PM1/12/15
to nod...@googlegroups.com

On Mon, Jan 12, 2015 at 12:34 PM, Aria Stewart <ared...@nbtsc.org> wrote:
The difference is in the separation of the task: a unix process shares nothing, and returns nothing but a status code

That's not true at all of fork though. It shares everything copy-on-write, and so you can communicate with the parent using a shared filehandle (a pipe), and all variable values are shared (effectively copied) at fork time. Perl makes this communication really easy by opening the magic "|-" filename (see the perlipc man page).

In node's child_process this is done slightly differently - it's not a fork, so variables aren't shared, but you do communicate over a pipe (hidden as an EventHandler). If you want to share any variables you need to send them to the child process over this pipe.
Reply all
Reply to author
Forward
0 new messages