Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Unix

Journal SL Baur's Journal: fork(2) and vfork(2) and clone(2), a cautionary tale

"We are at war with System V, we have always have been at war with System V" - George Orwell, paraphrased from 1984.

Selected quotes from the online Perl manual:[0]

$ man peripc
...
sub REAPER {
$waitedpid = wait;
# loathe sysV: it makes us not only reinstate
# the handler, but place it after the wait
...
$SIG{CHLD} = \ # still loathe sysV
...

In the beginning ...
Ken Thompson[1] had a wonderful flash of brilliance, "if I fully swap out a process, change the in-core process id in the kernel structures of the in-core copy, then I have implemented a new process function with only a handful of lines of code". Thus was fork(2) born.

Unix and its descendants have always had the philosophy that process creation should be and is cheap, and it certainly is compared to its competitors. But the main primitive, fork(2) did not scale up too well. What works on a PDP 11/70 may not work so well on a VAX.

Basing the idea that fork(2) is nearly always followed by exec(2), which essentially throws away the entire state of the child process, the inherently sinful vfork(2) was born in Berkeley in a predecessor O/S that not inappropriately uses a demon as its mascot.

vfork(2) got away with being, much, much faster than traditional fork(2) by doing away with much of the copying of process state that fork(2) does. The major drawback is that the child process shares full virtual memory state with the parent. Extremely careful programming was required if one was using the nominally faster vfork(2) over fork(2).

Consider it the predecessor of threads programming on Unix without any of the forethought of modern threads libraries.

For many years, Linus resisted introducing vfork into Linux. Linus being Linus, it was usually, "I'm not about to put that kind of crap into my kernel." Sometime after 2.0, I think in the 2.1 time frame, the VM in Linux had firmed up enough that he could implement a decent vfork using COW (copy on write) for shared writable pages, and thus vfork(2) entered the Linux kernel, *safely*[2].

The mindset that most of us guys who value engineering principles have is that much of the "innovation" brought forward by the BSD guys was a cheap hack. That includes /bin/csh which is perhaps the worst engineered language ever and has caused untold cost in damages to developers writing interactive programs (like Emacs) because users ended up being recommended to put interactive `stty's in their ~/.cshrc that would always execute and puke if sub shells were being invoked from a containing program without a control terminal.

The worst aspects of System V are gone. Sadly, the worst aspects of BSD 2 and BSD 4.x appear that they will live forever.

[0] I hate the way formatting works differently here and sucks harder than in regular mode.

[1] May peace and blessings be upon him.

[2] It did not last long as an independent system call and was fairly quickly replaced with clone(2) that is primitive to both fork(2) and vfork(2).

"And remember: Evil will always prevail, because Good is dumb." -- Spaceballs

Working...