As I have read that for a multi-process application, a single CPU can handle only one task at a time, switching contexts between two processes. In a multi-threaded application, a single CPU can handle multiple threads. I do not understand this. Does the CPU handle one thread at a time if there is only one CPU? If yes, where is the advantage of having multi-threaded application vs multi-process application if CPU can handle one thing at a time.
In a multithreaded process on a single processor, the processor can switch execution resources between threads, resulting in concurrent execution. Concurrency indicates that more than one thread is making progress, but the threads are not actually running simultaneously.
Yes you can do multithreading on a single processor system. In multi-processor system , multiple threads execute , simultaneously on different cores. Eg- If there are two threads and two cores , then each thread would run on individual core.
"Single-threaded" means that we open a single connection and measure the speeds from that. "Multi-threaded" means that we're using multiple connections - usually anywhere from 3 to 8 - at the same time, and measure the total speed across them all.
Single CPU systems use scheduling and can achieve multi-tasking because the time of the processor is time-shared by several processes so allowing each process to advance in parallel. So a process runs for some time and another waiting gets a turn.
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 Pipeline
x 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 2
2 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 Pipeline
x 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 2
x 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 continue
x 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With