How does a single CPU handle Multi-threaded and multi-process applications? How does a single CPU handle Multi-threaded and multi-process applications? multithreading multithreading

How does a single CPU handle Multi-threaded and multi-process applications?


TL;DR

Multithreading on a single core can speed up the application by using thread and instruction level parallelism.

If a single CPU has multiple cores it will run a process on each of the cores. If it does not, it will need to switch between processes on the single core.

Multithreading and multiprocessing can be combined for better results.

Full Explanation

Where multiprocessing systems include multiple complete processing units, multithreading aims to increase utilization of a single core by using thread-level as well as instruction-level parallelism. As the two techniques are complementary, they are sometimes combined in systems with multiple multithreading CPUs and in CPUs with multiple multithreading cores. Multithreading | WIkipedia

Example

A single CPU handles multi-threading in this way.

Let's say that we have two processes A and B which need to run a set of commands. After each command, the threads need the result. Here are the threads and the commands they need to run.

| # | Thread A | Thread B||---|----------|---------|| 1 | 1        | 5       || 2 | 3        | 1       || 3 | Wait     | 3       || 4 | 4        | 2       |

Now lets look at how the CPU would execute those (theoretically)

CPU Starts with thread A

CPU Pipelinex x x x x x (1)1 x x x x x (2) # Thread A, command 1 (1)5 1 x x x x (3) # Thread B, command 1 (5)3 5 1 x x x (4) # Thread A, command 2 (3)1 3 5 1 x x (5) # Thread B, command 2 (1)3 1 3 5 1 x (6) # Thread B, command 3 (3)... A is waiting for result of command 22 3 1 3 5 1 (7) # Thread B, command 4 (2)x 2 3 1 3 5 (8)x x 2 3 1 3 (9)x x x 2 3 1 (10)4 x x x 2 3 (11) # Thread A, command 4... A now has the result and can continue.x 4 x x x 2 (12)x x 4 x x x (13)x x x 4 x x (14)x x x x 4 x (15)x x x x x 4 (16)x x x x x x (17)

This is how that would look without multi threading.

CPU Pipelinex x x x x x (1)1 x x x x x (2) # Thread A, command 1 (1)3 1 x x x x (3) # Thread A, command 2 (3)... A is waiting for result of command 2x 3 1 x x x (4)x x 3 1 x x (5)x x x 3 1 x (6)x x x x 3 1 (7)x x x x x 3 (8)4 x x x x x (9)  # Thread A, command 4 (4)... A now has the result and can continuex 4 x x x x (10)x x 4 x x x (11)x x x 4 x x (12)x x x x 4 x (13)x x x x x 4 (14)5 x x x x x (15) # Thread B, command 1 (5)1 5 x x x x (16) # Thread B, command 2 (1)3 1 5 x x x (17) # Thread B, command 3 (3)2 3 1 5 x x (18) # Thread B, command 4 (2)x 2 3 1 5 x (19)x x 2 3 1 5 (20)x x x 2 3 1 (21)x x x x 2 3 (22)x x x x x 2 (23)x x x x x x (24)

Thus with multithreading the threads would complete after 17 time steps, without it would take 24.

Questions?


Your core question is this: "[W]here is the advantage of having multi-threaded application vs multi-process application if CPU can handle one thing at a time?"

The simple answer is this -- threads share all memory and file descriptors automatically. If you use a multi-process application, you have to manually arrange to share memory or hand off file descriptors where needed. The relative advantages and disadvantages of multi-threading versus multi-process for parallelism are pretty much the same regardless of the number of cores present.

The advantage of multi-process is this same difference argued the other way around. Because processes don't share memory and file descriptors automatically, it's easier to keep things isolated when you don't want them shared.

From a practical standpoint, the answer is typically that the software needed to effectively create multi-process applications just doesn't exist yet. Whereas the software needed to create multi-threaded applications does. There's no good libraries in existence that permit you to have a pool of processes and dispatch tasks to the first available process. There no good way to have in-memory collections of various types that are shared across processes. In contrast, these kinds of tools are widely available for threads.


Yes one CPU can handle many processes or threads. It can do this by stopping one thread, and pausing it and starting another. There are several reasons you might want to do this. First of all many processes will spend some time waiting for external events (Such as a disk or database) while it is waiting then the CPU can work on something else.

Also back in the days when computers were much more expensive it allowed many people to share the use of one computer. When I started my CS Degree the department had one HP Unix server that everyone used. So I could be editing and compiling code, while another user was checking his email and 20 other people worked on this or that.

This has been in use in one form or another since the 60's.