Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You make it sound so smooth and slick, and yes, having the child have all its state all ready and set up sounds like a good thing.

I suspect you are talking of using concurrency within the same program. The competing technology would be shared memory threads, of which I am more familiar. The only downside of this approach compared to fork seems (to me) to be the requirement to lock and signal when accessing shared resources.

Is there a downside to using fork within a process? Shirley if you've got a copy of memory (even if the actual copy is defferred) don't you also inherit the baggage of managing those resources too?

Lotsa questions there. Thanks for your original candid answer.



One thing to remember: when fork() was invented, shared-memory threads basically didn't exist. Everything that would be done with threads today, had to be done with either fork() or asynchronous event loops, because no mainstream OS had threading.

Another thing to remember: there's essentially no difference between a fork() and a thread. On Linux, they're both implemented by the same clone() syscall -- they just specify different flags about what should be shared versus what should be split and copied. Both are extremely cheap, and if you're going to implement threading, you might as well implement fork() because [on hardware with virtual memory] it's almost free.


> I suspect you are talking of using concurrency within the same program.

That's one common use case for 'oldies' :) But there are some others, such as splitting a protocol handler into two layers, all the setup including environment, privileges and so on is the same, then the two layers of the protocol split and start communicating via a pipe. Now it's technically two programs but they happen to live in a single binary image.

> The competing technology would be shared memory threads, of which I am more familiar.

When unix was first developed shared memory was not in the cards, neither were threads. Unix is old, and fork is definitely showing how old. The initial machine that unix was developed on was a PDP-7, here are the specs:

18 bit words

4 K memory standard (magnetic cores, small ferrite beads with a bunch of wires running through them for addressing and a single read/write wire (ok, that's simplified))

16 K words for the machine Unix was written on originally (max was 64K words)

discrete parts only (no integration, just Q,D & R (and some 'C' ;) )

I'm not 100% sure if they already had 'fork' in place on that machine, but the next step up, the PDP-11 definitely had it (that's also the machine that C was born on, the first version of unix was written in assembler!).

> The only downside of this approach compared to fork seems (to me) to be the requirement to lock and signal when accessing shared resources.

They're completely different beasts I think. Fork is a way to guarantee a whole slew of things about the relationship between two processes, threads are essentially parallel executions within the same memory space (or on a single core machine a simulation of parallel execution).

Threads are both a great thing and a nightmare, depending on whether you've just solved the worst race condition bug in your career or whether you're still busy with it :)

Your 'lock and signal' sounds pretty simple, and in theory it is, in practice efficient multi-threading programs are amongst the hardest things to get right that I know of.

> Is there a downside to using fork within a process?

If you need it, you need it. If not don't use it. Like any other tool. Downsides to system calls are not really interesting, they only appear when you use the system calls in ways that they weren't meant to. What's in the core of a unix system is pretty much what needs to be there.

> don't you also inherit the baggage of managing those resources too?

Yes, absolutely. Any memory that was allocated in the parent is also allocated in the child, any file system handles that you have are present in both and so on.

In the case of file system handles there is usually some kind of convention on who gets to 'own' them but you can do interesting things by letting them be owned by both.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: