4 ms·
> So do Cygwin and/or MSYS emulate the fork() Yes. That's one thing we spent considerable engineering effort on in this first version of the Windows Subsystem
by bitcrazed 11y ago
> So do Cygwin and/or MSYS emulate the fork()
Yes. That's one thing we spent considerable engineering effort on in this first version of the Windows Subsystem for Linux: We implement fork in the Windows kernel, along with the other POSIX and Linux syscalls.
This allows us to build a very efficient fork() and expose it to the GNU/Ubuntu user-mode apps via the fork(syscall).
We'll be publishing more details on this very soon.
- vram22 11y agoInteresting! thanks. The original Unix fork() was found to be somewhat expensive in resources (a little surprising since it was the only way to create a child process), later there was vfork() (copy-on-write) (maybe innovated by BSD), and I read Linux's clone() does even better, though not looked into it in detail.
- jmgao 11y agofork would copy the entire address space of the process, which was wasteful when you're usually going to just throw all of that memory away by calling exec. BSD added vfork to optimize the `if (fork() == 0) exec(...)` scenario, by not copying the memory, and just pausing and then borrowing memory from the parent, until exec is called. Modern operating systems use copy on write pages for fork, so instead of copying all of memory, you just need to copy the page tables.
- vram22 11y ago> then borrowing memory from the parent, until exec is called What does that mean?
- the_why_of_y 11y agoIt means that if the child process actually modifies the memory then those modifications will be visible in the parent process, because they're both using the same address space. It's essentially an awful hack that was added to BSD at a time when it didn't yet use copy-on-write for fork() to achieve the same performance with vfork()+exec() that you would get from a CreateProcess()-like API. That's why the child is allowed to do almost nothing: the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before success‐ fully calling _exit(2) or one of the exec(3) family of functions.
- vram22 11y agoThanks.
- JoshTriplett 11y agoIn Linux, fork(), vfork(), and clone() all use the same underlying machinery, with just a few different flags. clone() is the most general, with flags for what to share; those flags in particular include all the Linux namespaces that serve as the basis for containers. fork() just uses clone() with a hardcoded set of flags, and vfork() does the same as fork() except that it doesn't schedule the parent process until the child calls exec.
- vram22 11y agoInteresting, will have to check that out.
- vram22 11y ago>We implement fork in the Windows kernel So will this be only on Windows 10?
- JonathonW 11y agoYes-- the Linux stuff is all coming in the Windows 10 Anniversary update this summer.
- poizan42 11y agoHow does this new fork differ from the already existing NtCreateProcess with a NULL section handle that was used to implement fork in the old SUA/POSIX subsystem?