5 ms·
Face the fact, fork() is fundamentally flawed!
by saynsedit 10y ago
Face the fact, fork() is fundamentally flawed!
- ahoka 10y agoI would like to see the reasons this comment was down-voted. It is true that fork(2) is simple and elegant design, but it involves a huge accidental complexity and hidden implications like in the original article. There is a reason that Linux has clone(2) underneath.
- astrodust 10y agoEvery method has limitations. If you want to call these flaws that's just semantics.
- the_mitsuhiko 10y ago> It is true that fork(2) is simple and elegant design What exactly is simple and elegant about it? Have you looked into how much tooling is necessary everywhere in unix to make it work? It's insane. It has a huge footprint and it does not provide standardized APIs to make it work for non covered cases (the best we have is pthread_atfork which is not portable).
- qznc 10y agoFork is simple, but not easy.
- the_mitsuhiko 10y agoWhat makes for simple? I really need to understand this. I implemented fork many years ago for a university operating system and nothing about either the implementation nor the usage is simple. I use fork on a daily basis and the POSIX version of it is insanely complex for everybody involved.
- qznc 10y ago> I implemented fork many years ago for a university operating system and nothing about either the implementation nor the usage is simple Me too. I found it simple: Create new child process, copy the address space, copy the instruction pointer, let it run. If you want to improve performance via copy-on-write, it gets more complex to implement. If you combine it with other features like threads, mmap, limits, etc use becomes complex, but the mechanism itself is simple. You might be able to design an easy variant of fork, which handles threads, mmap, limits, etc in a sane way, but that would not be simple anymore. Fork is actually a good example, why simple and easy are different concepts and why it is really hard to achieve both.
- the_mitsuhiko 10y ago> Me too. I found it simple: Create new child process, copy the address space, copy the instruction pointer, let it run. Even in the total absence of threads (which you never have on a modern OS) you already fucked up. Because you did not account for file handles, signal handling, pid management etc. fork() even on the simplest real world operating system is a massive pain for the OS.
- lmm 10y ago> What exactly is simple and elegant about it? Spawn-style APIs have to take a billion parameters that are mostly set to defaults, for things like working directory, environment variables, user ID, controlling terminal. Whereas fork+exec means you can express these things in a more compositional, buildery style: fork, change the two things you actually need to change, then exec.
- saynsedit 10y agoMeh, no one ever remembers all those stateful hidden parameters to fork() for process creation. This results in painful bugs later on, possibly affecting security. Speaking from experience. Python says explicit is better than implicit. I agree in this case.
- MichaelGG 10y agoI don't think this makes the case for elegance at all. Global vars set wherever in a program are preferable to explicit params when creating a new process? And there's no way to have default params or anything?
- lmm 10y ago> Global vars set wherever in a program are preferable to explicit params when creating a new process? The outside world is always going to be implicit global mutable state; spawning an arbitrary process to do who-knows-what is inherently that kind of problem. If you could express it as an actual function you wouldn't need to run a separate process. > And there's no way to have default params or anything? Well not in C. You can sort-of do it by having a struct with fields that you fill in where all-bits-zero is the default but that's generally more trouble than it's worth.
- MichaelGG 10y agoWell by that reasoning there's never any need for function purity since you can't eliminate global state on current computer designs. C easily allows you to have multiple functions. exec_with_current_state or something. And a struct is a way, too.
- ahoka 10y agoElegant in a 'clever trick' sense, not as good engineering.
- deleted 10y ago[deleted]