user34537
user34537

Reputation:

Are processes slower then threads

I just wrote a question about caching and it got me thinking. Is using a server process with a strict API any slower then a server lib statically linked (possibly in its own thread)?

If it is slower, how much overhead is there? The OS is linux but most of my development and testing is on windows.

Upvotes: 1

Views: 353

Answers (2)

MarkR
MarkR

Reputation: 63538

It depends.

I don't think context-switching is really any different in Linux between threads and processes. However:

  • Starting a new process (e.g. with posix_spawn or fork/exec) is slower than starting a new thread, because you have to load a new process image, which invokes the dynamic linker, and does other things

  • Multiple process systems can use a lot more memory than multithreaded ones - sometimes - depending on what data structures you share. This of course can impact performance, leaving you less memory for other things.

Processes which "fork but don't exec" are somewhere "in between" processes and threads. However they come with their own problems (for example, many libraries become confused if they share their file descriptors with another process).

Upvotes: 1

blueshift
blueshift

Reputation: 6882

Yes, it is slower as it involves context switches and the extra work of copying data around. This is for example one reason why SQLite is popular.

As for how much overhead, "It depends", but the answer is likely "Not enough to be a problem for you". As always if in doubt the only thing to do is try both ways and benchmark/profile them.

Upvotes: 2

Related Questions