Why is it faster to create threads than it is to create processes?
Is it simply because they only need a stack and stora开发者_运维百科ge for registers so they are cheap to create ?
Is the fact that threads can share common data, i.e they don't need to use interprocess communication a factor here ? Would this result in less need for protection ?
Or do threads take advantage of multiprocessors better than processes ?
Who says it is? On some operating systems there is little difference. Are you thinking of Windows where threads are much lighter weight than processes?
I suspect you would learn more by consulting this Stack Overflow question.
If we speak of heavy-weight threads (Windows Threads for example), a Process has Threads, and it has at least one thread (the main thread), so clearly it's heavier or at least not-lighter :-) (the sum is always >= the part)
There are many "tables" that a Process must have (the open file table, the table that show how the memory is mapped (LDT, Local Descriptor Table)...). If you create a process all these tables have to be initialized. If you create a thread they don't (because the thread uses the ones of its process). And then a new process has to load again all the DLL, check them for remapping...
From the windows perspective, a process can take longer to create if it is loading many DLLs and also moving them around in memory due to conflicts in the base address. Then see all of the other reasons listed in the link from David Heffernan's answer.
Process switch requires change in CS/DS registers. Change in the value of these registers require fetching a new descriptor from global descriptor table which is actually expensive process in terms of CPU time.
精彩评论