<p>I've heard that creating a new process on a Windows box is more expensive than on Linux.  Is this true?  Can somebody explain the technical reasons for why it's more expensive and provide any historical reasons for the design decisions behind those reasons?</p>

[{'answer_id': 47865, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 5, 'selected': False, 'text': "<p>Unix has a 'fork' system call which 'splits' the current process into two, and gives you a second process that is identical to the first (modulo the return from the fork call).  Since the address space of the new process is already up and running this is should be cheaper than calling 'CreateProcess' in Windows and having it load the exe image, associated dlls, etc.  </p>\n\n<p>In the fork case the OS can use 'copy-on-write' semantics for the memory pages associated with both new processes to ensure that each one gets their own copy of the pages they subsequently modify.</p>\n"}, {'answer_id': 47879, 'author': 'VolkerK', 'author_id': 4833, 'author_profile': 'https://Stackoverflow.com/users/4833', 'pm_score': 4, 'selected': False, 'text': '<p>In addition to the answer of Rob Walker:\nNowadays you have things like the Native POSIX Thread Library - if you want.\nBut for a long time the only way to "delegate" the work in the unix world was to use fork() (and it\'s still prefered in many, many circumstances).\ne.g. some kind of socket server\n<pre>socket_accept()\nfork()\nif (child)\n    handleRequest()\nelse\n    goOnBeingParent()\n</pre>Therefore the implementation of fork had to be fast and lots optimizations have been implemented over time.\nMicrosoft endorsed CreateThread or even fibers instead of creating new processes and usage of interprocess communication. I think it\'s not "fair" to compare CreateProcess to fork since they are not interchangeable. It\'s probably more appropriate to compare fork/exec to CreateProcess.</p>\n'}, {'answer_id': 47964, 'author': 'mweerden', 'author_id': 4285, 'author_profile': 'https://Stackoverflow.com/users/4285', 'pm_score': 4, 'selected': False, 'text': '<p>The key to this matter is the historical usage of both systems, I think. Windows (and DOS before that) have originally been single-user systems for <em>personal</em> computers. As such, these systems typically don\'t have to create a lot of processes all the time; (very) simply put, a process is only created when this one lonely user requests it (and we humans don\'t operate very fast, relatively speaking).</p>\n\n<p>Unix-based systems have originally been multi-user systems and servers. Especially for the latter it is not uncommon to have processes (e.g. mail or http daemons) that split off processes to handle specific jobs (e.g. taking care of one incoming connection). An important factor in doing this is the cheap <code>fork</code> method (that, as mentioned by Rob Walker (<a href="https://stackoverflow.com/questions/47845/why-is-creating-a-new-process-more-expensive-on-windows-than-linux#47865">47865</a>), initially uses the same memory for the newly created process) which is very useful as the new process immediately has all the information it needs.</p>\n\n<p>It is clear that at least historically the need for Unix-based systems to have fast process creation is far greater than for Windows systems. I think this is still the case because Unix-based systems are still very process oriented, while Windows, due to its history, has probably been more thread oriented (threads being useful to make responsive applications).</p>\n\n<p>Disclaimer: I\'m by no means an expert on this matter, so forgive me if I got it wrong.</p>\n'}, {'answer_id': 48244, 'author': 'Johannes Passing', 'author_id': 4372, 'author_profile': 'https://Stackoverflow.com/users/4372', 'pm_score': 6, 'selected': False, 'text': "<p>mweerden: NT has been designed for multi-user from day one, so this is not really a reason. However, you are right about that process creation plays a less important role on NT than on Unix as NT, in contrast to Unix, favors multithreading over multiprocessing.</p>\n\n<p>Rob, it is true that fork is relatively cheap when COW is used, but as a matter of fact, fork is mostly followed by an exec. And an exec has to load all images as well. Discussing the performance of fork therefore is only part of the truth.</p>\n\n<p>When discussing the speed of process creation, it is probably a good idea to distinguish between NT and Windows/Win32. As far as NT (i.e. the kernel itself) goes, I do not think process creation (NtCreateProcess) and thread creation (NtCreateThread) is significantly slower as on the average Unix. There might be a little bit more going on, but I do not see the primary reason for the performance difference here. </p>\n\n<p>If you look at Win32, however, you'll notice that it adds quite a bit of overhead to process creation. For one, it requires the CSRSS to be notified about process creation, which involves LPC. It requires at least kernel32 to be loaded additionally, and it has to perform a number of additional bookkeeping work items to be done before the process is considered to be a full-fledged Win32 process. And let's not forget about all the additional overhead imposed by parsing manifests, checking if the image requires a compatbility shim, checking whether software restriction policies apply, yada yada.</p>\n\n<p>That said, I see the overall slowdown in the sum of all those little things that have to be done in addition to the raw creation of a process, VA space, and initial thread. But as said in the beginning -- due to the favoring of multithreading over multitasking, the only software that is seriously affected by this additional expense is poorly ported Unix software. Although this sitatuion changes when software like Chrome and IE8 suddenly rediscover the benefits of multiprocessing and begin to frequently start up and teardown processes...</p>\n"}, {'answer_id': 48658, 'author': 'gabr', 'author_id': 4997, 'author_profile': 'https://Stackoverflow.com/users/4997', 'pm_score': 1, 'selected': False, 'text': "<p>All that plus there's the fact that on the Win machine most probably an antivirus software will kick in during the CreateProcess... That's usually the biggest slowdown.</p>\n"}, {'answer_id': 50984, 'author': 'Tim Williscroft', 'author_id': 2789, 'author_profile': 'https://Stackoverflow.com/users/2789', 'pm_score': 3, 'selected': False, 'text': "<p>Uh, there seems to be a lot of &quot;it's better this way&quot; sort of justification going on.</p>\n<p>I think people could benefit from reading &quot;Showstopper&quot;; the book about the development of Windows NT.</p>\n<p>The whole reason the services run as DLLs in one process on Windows NT was that they were too slow as separate processes.</p>\n<p>If you got down and dirty you'd find that the library loading strategy is the problem.</p>\n<p>On Unices ( in general) the Shared libraries (DLLs) code segments are actually shared.</p>\n<p>Windows NT loads a copy of the DLL per process, becauase it manipulates the library code segment (and executable code segment) after loading. (Tells it where is your data?)</p>\n<p>This results in code segments in libraries that are not reusable.</p>\n<p>So, the NT process create is actually pretty expensive. And on the down side, it makes DLLs no appreciable saving in memory, but a chance for inter-app dependency problems.</p>\n<p>Sometimes it pays in engineering to step back and say, &quot;now, if we were going to design this to really suck, what would it look like?&quot;</p>\n<p>I worked with an embedded system that was quite temperamental once upon a time, and one day looked at it and realized it was a cavity magnetron, with the electronics in the microwave cavity. We made it much more stable (and less like a microwave) after that.</p>\n"}, {'answer_id': 67293, 'author': 'Chris Smith', 'author_id': 9073, 'author_profile': 'https://Stackoverflow.com/users/9073', 'pm_score': 5, 'selected': False, 'text': '<p>Adding to what JP said: most of the overhead belongs to Win32 startup for the process.</p>\n\n<p>The Windows NT kernel actually does support COW fork. <a href="http://technet.microsoft.com/en-us/interopmigration/bb380242.aspx" rel="noreferrer">SFU</a> (Microsoft\'s UNIX environment for Windows) uses them. However, Win32 does not support fork. SFU processes are not Win32 processes. SFU is orthogonal to Win32: they are both environment subsystems built on the same kernel.</p>\n\n<p>In addition to the out-of-process LPC calls to <code>CSRSS</code>, in XP and later there is an out of process call to the application compatibility engine to find the program in the application compatibility database. This step causes enough overhead that Microsoft provides a group policy option to <a href="http://technet.microsoft.com/en-us/library/cc784527.aspx" rel="noreferrer">disable the compatibility engine on WS2003</a> for performance reasons.</p>\n\n<p>The Win32 runtime libraries (kernel32.dll, etc.) also do a lot of registry reads and initialization on startup that don\'t apply to UNIX, SFU or native processes.</p>\n\n<p>Native processes (with no environment subsystem) are very fast to create. SFU does a lot less than Win32 for process creation, so its processes are also fast to create.</p>\n\n<p>UPDATE FOR 2019: add LXSS: <a href="https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux#Architecture" rel="noreferrer">Windows Subsystem for Linux</a></p>\n\n<p>Replacing SFU for Windows 10 is the LXSS environment subsystem. It is 100% kernel mode and does not require any of that IPC that Win32 continues to have. Syscall for these processes is directed directly to lxss.sys/lxcore.sys, so the fork() or other process creating call only costs 1 system call for the creator, total. <a href="https://blogs.msdn.microsoft.com/wsl/2016/04/22/windows-subsystem-for-linux-overview/" rel="noreferrer">[A data area called the instance] keeps track of all LX processes, threads, and runtime state.</a></p>\n\n<p>LXSS processes are based on native processes, not Win32 processes. All the Win32 specific stuff like the compatibility engine aren\'t engaged at all.</p>\n'}, {'answer_id': 1898429, 'author': 'DigitalRoss', 'author_id': 140740, 'author_profile': 'https://Stackoverflow.com/users/140740', 'pm_score': 4, 'selected': False, 'text': '<h3>The short answer is "software layers and components".</h3>\n\n<p>The windows SW architecture has a couple of additional layers and components that don\'t exist on Unix or are simplified and handled inside the kernel on Unix.</p>\n\n<p>On Unix, fork and exec are direct calls to the kernel.</p>\n\n<p>On Windows, the kernel API is not used directly, there is win32 and certain other components on top of it, so process creation must go through extra layers and then the new process must start up or connect to those layers and components. </p>\n\n<p>For quite some time researchers and corporations have attempted to break up Unix in a vaguely similar way, usually basing their experiments on the <a href="http://en.wikipedia.org/wiki/Mach_kernel" rel="noreferrer">Mach kernel</a>; a well-known example is <a href="http://en.wikipedia.org/wiki/Mac_OS_X" rel="noreferrer">OS X.</a>. Every time they try, though, it gets so slow they end up at least partially merging the pieces back into the kernel either permanently or for production shipments.</p>\n'}, {'answer_id': 4775658, 'author': 'hacksoncode', 'author_id': 586582, 'author_profile': 'https://Stackoverflow.com/users/586582', 'pm_score': 1, 'selected': False, 'text': "<p>It's also worth noting that the security model in Windows is vastly more complicated than in unix-based OSs, which adds a lot of overhead during process creation. Yet another reason why multithreading is preferred to multiprocessing in Windows.</p>\n"}, {'answer_id': 51396188, 'author': 'ctrl-alt-delor', 'author_id': 537980, 'author_profile': 'https://Stackoverflow.com/users/537980', 'pm_score': 2, 'selected': False, 'text': '<p>As there seems to be some justification of MS-Windows in some of the answers e.g.</p>\n<ul>\n<li>“NT kernel and Win32, are not the same thing. If you program to NT kernel then it is not so bad” — True, but unless you are writing a Posix subsystem, then who cares. You will be writing to win32.</li>\n<li>“It is not fair to compare fork, with ProcessCreate, as they do different things, and Windows does not have fork“ — True,\nSo I will compare like with like. However I will also compare fork, because it has many many use cases, such as process isolation (e.g. each tab of a web browser runs in a different process).</li>\n</ul>\n<h1>Now let us look at the facts, what is the difference in performance?</h1>\n<p><em>Data summerised from <a href="http://www.bitsnbites.eu/benchmarking-os-primitives/" rel="nofollow noreferrer">http://www.bitsnbites.eu/benchmarking-os-primitives/</a></em>.<br />\n<em>Because bias is inevitable, when summarising, I did it in favour of MS-Windows</em><br />\n<em>Hardware for most tests i7 8 core 3.2GHz. Except Raspberry-Pi running Gnu/Linux</em></p>\n<p><a href="https://i.stack.imgur.com/PDvtk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PDvtk.png" alt="A comparison of various basic operations, on Gnu/Linux, Apple-Mac, and Microsoft\'s Windows (smaller is better)" /></a></p>\n<p><a href="https://i.stack.imgur.com/eMr1B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eMr1B.png" alt="A comparison of MS-Windows process create vs Linux" /></a></p>\n<p>Notes:\nOn linux, <code>fork</code> is faster that MS-Window\'s preferred method <code>CreateThread</code>.</p>\n<h1>Numbers for process creation type operations (because it is hard to see the value for Linux in the chart).</h1>\n<p>In order of speed, fastest to slowest (numbers are time, small is better).</p>\n<ul>\n<li>Linux CreateThread 12</li>\n<li>Mac CreateThread 15</li>\n<li>Linux Fork 19</li>\n<li>Windows CreateThread 25</li>\n<li>Linux CreateProcess (fork+exec) 45</li>\n<li>Mac Fork 105</li>\n<li>Mac CreateProcess (fork+exec) 453</li>\n<li>Raspberry-Pi CreateProcess (fork+exec) 501</li>\n<li>Windows CreateProcess 787</li>\n<li>Windows CreateProcess With virus scanner 2850</li>\n<li>Windows Fork (simulate with CreateProcess + fixup) grater than 2850</li>\n</ul>\n<h1>Numbers for other measurements</h1>\n<ul>\n<li>Creating a file.\n<ul>\n<li>Linux 13</li>\n<li>Mac 113</li>\n<li>Windows 225</li>\n<li>Raspberry-Pi (with slow SD card) 241</li>\n<li>Windows with defender and virus scanner etc 12950</li>\n</ul>\n</li>\n<li>Allocating memory\n<ul>\n<li>Linux 79</li>\n<li>Windows 93</li>\n<li>Mac 152</li>\n</ul>\n</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/47845', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']