2

Is it simply because they only need a stack and storage 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 ?

user476033
  • 4,607
  • 8
  • 32
  • 35
  • http://stackoverflow.com/questions/3609469/what-are-the-thread-limitations-when-working-on-linux-compared-to-processes-for-n/3705919#3705919 – Flexo Feb 14 '11 at 11:58

4 Answers4

4

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.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Threads are much lighter in weight than processes? I'd say, in Windows, processes are much heavier than threads. :) – user611775 Feb 14 '11 at 13:44
  • @user I see where you are coming from. On the other hand, once you have got your threads alive then Windows is an extremely rich thread friendly environment. – David Heffernan Feb 14 '11 at 13:51
  • Well, to be honest, I was just comparing calling fork() 100,000 times to pthread_create 100,000 times, and pthread_create() executed about 4 times faster, maybe I am very much mistaken ! :) – user476033 Feb 14 '11 at 15:07
  • 1
    In my experience it is usually possible to write the code so that threads/processes don't need to be spawned very often. For example consider a thread pool. It generally doesn't really matter how long they take to bring to life. – David Heffernan Feb 14 '11 at 15:15
2

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

xanatos
  • 109,618
  • 12
  • 197
  • 280
1

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.

Chris O
  • 5,017
  • 3
  • 35
  • 42
0

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.

Adnan
  • 1