Why is it quicker to switch between threads than to switch between processes and share data between them?

3

1

Why is it quicker to switch between threads than to switch between processes and share data between them? For example, in the apache web server running on ubuntu can use either the Prefork MPM (spawning multiple child processes) or Worker MPM (creating multiple threads for a single process).

JohnMerlino

Posted 2013-07-10T19:31:09.750

Reputation: 257

Context switches between threads are faster than between processes. That is, it's quicker for the OS to stop one thread and start running another than do the same with two processes.

A context switch between processes is heavy. It means storing all processor state (registers, etc) to RAM (at a magic memory location in the user process space actually, guess where!), in practice dirtying all cached memory in the cpu, and reading back the process state for the new process. – Devid – 2013-07-10T21:36:35.033

@JohnMerlino, http://stackoverflow.com/questions/7439608/steps-in-context-switching

– sasha.sochka – 2013-07-10T22:18:17.640

Answers

4

Quickie, incomplete explanation:

In a thread switch, there's a lot less context you need to save/load. Specifically, memory is shared. The kernel doesn't have to do any page outs of dirty pages, and VM tricks to pull in all the memory for a new process (though some specific pages may need to be pulled in). There are other process specific data structures in the kernel (say, the open file descriptor table) that don't need to be swapped out.

As a side effect, you're also much more likely to be able to use what's in the processor's caches at that point. a new process probably needs to start a cold cache.

Yes, there are tools to share memory (IPC shared memory, pipes) between processes, but none are as clean/easy as the common memory in a process. You can't grow a shared memory block like you can with realloc() and such. Anything besides shared memory means you need multiple copies of data structures, one in each process, with tricks to copy changes as needed.

Specifically, apache has multiple models for different OSes. The original model was pre-fork, giving process isolation in exchange for some heavyweight process switching. This worked fine with the UNIXes that were common at the time of it's first writing, where some didn't have threads at all. Windows was so bad with multi-process that apache had to do threading - processes on Windows (at least at the time of apache 1.2/2.0) were too heavyweight. Linux has very light processes, where switching is close to thread switching time, so it stayed pre-Fork usually. Solaris has a complex thread "LWP" model, and does best in a hybrid thread/fork model.

Rich Homolka

Posted 2013-07-10T19:31:09.750

Reputation: 27 121

0

I think if you read this StackOverflow link here you will understand why it is faster to switch between threads than to switch between processes.

Devid

Posted 2013-07-10T19:31:09.750

Reputation: 5 566