4 ms·
That's quite a copout with forkall. Presumably you have some idea what your threads are doing if you want to fork the entire process, so don't do it in the mid
by Dylan16807 12y ago
That's quite a copout with forkall. Presumably you have some idea what your threads are doing if you want to fork the entire process, so don't do it in the middle of writing to a file.
- swartkrans 12y agoHow could you know what your threads are doing at the time you call fork? That doesn't sound safe. I write such code in a way that threads communicate with each other when they need to, but if I start assuming what those threads are doing that seems like it could get really complicated really fast.
- thetrb 12y agoSure it can get complicated, but there are also some safe ways to fork. Imagine you fork at the very beginning of your program before starting any threads. No danger in that. So the language could at least allow it (but provide the proper warnings around it).
- Vendan 12y agothe issue arises when the programming language doesn't let you run code before the threads split out, like golang.
- Dylan16807 12y agoI don't know exactly what they're doing, but being able to reason that they are not performing file IO is a vastly easier problem than figuring out a completely safe point to kill them all.
- dietrichepp 12y agoHow many libraries do you use? Are you sure they don't create their own threads? Also, just imagine something like this: file.write(blah); if (has_forked) thread_exit(); file.write(blah2); if (has_forked) thread_exit(); file.write(blah3) if (has_forked) thread_exit(); Note the race conditions in the above code... forkall() is a disaster. Presumably you'd combine it with pthread_atfork(), but it's still a total mess.
- dpark 12y agoDoes forkall exist on Linux? I was under the impression it's a nonstandard Solaris extension.
- Dylan16807 12y agoThat's a much better reason. But such a problem isn't set in stone.
- dpark 12y agoI've always thought fork() was a hack anyway. 99% of the time, fork() is followed by a call to an exec() variant. The actual process forking behavior seems useful in only a small set of cases that could be handled by spawning a process and passing state explicitly. The benefits of fork() don't seem to offset its costs or justify its widespread use. fork() is one of those things that's conceptually quite elegant but in practice has too many problematic edge cases. (Signals seem to fall into the same group.)
- rogerbinns 12y agoSpawning a process by passing state explicitly is what Windows does. The CreateProcess call takes 10 parameters, several of which are structures containing even more parameters - http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms68... I do like that fork, then setup functions, then exec keeps the code simpler. If everything is done as a single call like CreateProcess then it needs to have a way of passing what the setup functions need as data - rather a lot of it.
- dpark 12y agoThat's conflating separate issues. Windows could have a CreateProcessSimple() call that spawns a new process without so many options. CreateProcess is complex not so much because it is one call instead of two, but because it has all options jumbled together. Meanwhile the Linux/Unix decision was to have about a dozen different APIs to do variants of the same thing (fork, vfork, and clone plus execl, execlp, execle, execv, execvp, execvpe, fexecve, posix_spawn, posix_spawnp, and probably more). This isn't really any less complex. The argument complexity of CreateProcess could not possibly derive from merging fork() and exec() because fork() has no arguments.