Why is creating a new process more expensive on Windows than Linux?

后端 未结 10 665
情深已故
情深已故 2020-11-27 11:35

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 expen

相关标签:
10条回答
  • 2020-11-27 12:06

    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.

    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.

    0 讨论(0)
  • 2020-11-27 12:13

    As there seems to be some justification of MS-Windows in some of the answers e.g.

    • “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.
    • “It is not fair to compare fork, with ProcessCreate, as they do different things, and Windows does not have fork“ — True, So 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).

    Now let us look at the facts, what is the difference in performance?

    Data summerised from http://www.bitsnbites.eu/benchmarking-os-primitives/.
    Because bias is inevitable, when summarising, I did it in favour of MS-Windows
    Hardware for most tests i7 8 core 3.2GHz. Except Raspberry-Pi running Gnu/Linux

    Notes: On linux, fork is faster that MS-Window's preferred method CreateThread.

    Numbers for process creation type operations (because it is hard to see the value for Linux in the chart).

    In order of speed, fastest to slowest (numbers are time, small is better).

    • Linux CreateThread 12
    • Mac CreateThread 15
    • Linux Fork 19
    • Windows CreateThread 25
    • Linux CreateProcess (fork+exec) 45
    • Mac Fork 105
    • Mac CreateProcess (fork+exec) 453
    • Raspberry-Pi CreateProcess (fork+exec) 501
    • Windows CreateProcess 787
    • Windows CreateProcess With virus scanner 2850
    • Windows Fork (simulate with CreateProcess + fixup) grater than 2850

    Numbers for other measurements

    • Creating a file.
      • Linux 13
      • Mac 113
      • Windows 225
      • Raspberry-Pi (with slow SD card) 241
      • Windows with defender and virus scanner etc 12950
    • Allocating memory
      • Linux 79
      • Windows 93
      • Mac 152
    0 讨论(0)
  • 2020-11-27 12:14

    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 personal 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).

    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 fork method (that, as mentioned by Rob Walker (47865), 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.

    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).

    Disclaimer: I'm by no means an expert on this matter, so forgive me if I got it wrong.

    0 讨论(0)
  • 2020-11-27 12:16

    In addition to the answer of Rob Walker: Nowadays you have things like the Native POSIX Thread Library - if you want. But 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). e.g. some kind of socket server

    socket_accept()
    fork()
    if (child)
        handleRequest()
    else
        goOnBeingParent()
    
    Therefore the implementation of fork had to be fast and lots optimizations have been implemented over time. Microsoft 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.

    0 讨论(0)
  • 2020-11-27 12:18

    Adding to what JP said: most of the overhead belongs to Win32 startup for the process.

    The Windows NT kernel actually does support COW fork. SFU (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.

    In addition to the out-of-process LPC calls to CSRSS, 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 disable the compatibility engine on WS2003 for performance reasons.

    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.

    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.

    UPDATE FOR 2019: add LXSS: Windows Subsystem for Linux

    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 data area called the instance] keeps track of all LX processes, threads, and runtime state.

    LXSS processes are based on native processes, not Win32 processes. All the Win32 specific stuff like the compatibility engine aren't engaged at all.

    0 讨论(0)
  • 2020-11-27 12:18

    The short answer is "software layers and components".

    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.

    On Unix, fork and exec are direct calls to the kernel.

    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.

    For quite some time researchers and corporations have attempted to break up Unix in a vaguely similar way, usually basing their experiments on the Mach kernel; a well-known example is OS X.. 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.

    0 讨论(0)
提交回复
热议问题