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

linuxprocessthreads

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

Best Answer

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.

Related Question