So what does it mean by dual-core? Do quadcore mean my computer has 4 cores? And if so then why does the top/htop command show 8 cores for a quadcore machine? 🤔
If you are confused between CPUs and Cores then you are at the right place. This article addresses all the above questions and helps you to understand the CPU better.
First thing first, what is a CPU?
A CPU a.k.a Central Processing Unit is the hardware that is responsible for executing instructions. So it is the CPU that runs all your word processing applications and music player on the computer.
CPU executes the applications a.k.a Program. The program consists of a set of instructions to perform a specific task. You can run one or more programs on the CPU.
How does the CPU look like under the hood?
A CPU, on a very high level, comprises mainly of the following parts:
- Arithmetic Logic Unit (ALU): ALU performs arithmetic and logical operations like addition, subtraction, bitwise operation, OR, AND, etc. It calculates the result of the operation and passes it to Control Unit.
- Control Unit (CU): CU controls the execution of the instructions. It coordinates between the IO devices, memory, and ALU to complete the execution of any instruction.
- CPU Registers: The registers are memory with fast access in the CPU. They store various information like intermediate calculation results, CPU state, address of the instruction that will be executed next, etc. All in all, the CPU registers store data for the execution of the instructions.
- CPU Cache: CPU cache is a hardware cache that is used for fast access of main memory’s data.
The ALU, CU, Registers, and Cache forms the basic execution unit of any CPU. It is called Core. Cores are responsible for executing the instructions provided to the CPU.
Apart from this, a CPU also contains some communication channel that connects the cores to the outside world, a memory controller (to connect to main memory), I/O controllers (to connect to I/O devices), etc.
Single Vs Multi-Processor System
On a Single-processor system, there is one main CPU that executes processes. Figure 3 above is essentially a single processor system.
Let's say you need a place to stay. So you rent a BHK house. Since you are the only person staying there, you can fully use it to your convenience.
In the same way, a single application can easily run on a single processor system.
Now your friend comes to join you. And you both need to share the house. But there is only one bed and you both need to sleep🙁 ! So you come up with an arrangement where each one of you takes your turn in using the bed 🤓.
This arrangement is Multi-tasking where the multiple applications take their turn in using the CPU for running. For multitasking, the applications are divided into smaller chunks called tasks. By multitasking quickly between different tasks, computers create an illusion that all that tasks are executing in parallel.
So far the living arrangement was working great for you. But now your 6 other friends joins you. The current arrangement is no more optimized to support 8 people.
So you come up with a new approach. You rent a similar house. Now 4 of you stay in House A and the remaining 4 takes House B. This arrangement is called Multi-processor.
A Multi-Processor is a single computer having two or more CPUs in close communication, sharing the system bus and sometimes the clock, memory, and peripheral devices. Below is a multiprocessor system with 2 CPUs:
Even though this solves our purpose but this arrangement is complex and also a bit expensive to maintain 2 houses. Can we find any simpler solution? Umm...We can create more rooms in the same house!! 💡
For CPU, that’s Cores!!
A core is the basic execution unit in a CPU that is capable of executing instructions. A CPU can have one or more cores. More the number of core, more instructions can be executed in parallel.
Introducing Multi-Core System
A multi-core system has 2 or more cores in one CPU. These cores can run independent instructions concurrently without impacting each other. A multi-core system looks like below:
A dual-core machine comes with 2 physical cores and a quad-core machine with 4.
Finally, now you get rid of House B and build 2 rooms in House A. Four of your friends share Room A and the remaining four share room B (Fig 9).
But it is still crowded and you get limited time to use the bed. Can we have some different arrangement to have better living condition? 😅 Of course, we can. After all, the sky is the limit to human innovation!!
You get 2 beds for each room😎. You convert your previous 2 room setup to 4 rooms, each having 2 beds. So now you have a total of 8 beds and all your 8 friends can use it at the same time. Life’s never had been so much easier !!
Say hello to Hyperthreading (HT/HTT) 👋🏻
In HT, 1 Physical Core = 2 Logical Cores / Virtual Cores.
In a Hyper-threaded processor, one CPU core is made in such a way that it can execute two instructions in parallel. This is achieved by having two logical processing units on a single core. The logical processors in a hyper-threaded core share some resources of the core like the caches, ALU, etc. Each logical cores have their execution unit (like some registers to maintain the state of execution) so that they can execute instructions parallelly. From a user point of view, 1 hyperthreaded core is equivalent to having 2 cores.
This answers the question of why a quad-core machine shows 8 cores in the top/htop command.
Is Hyperthreading the same as MultiThreading?
Multi-threading is the ability of the computer to execute multiple instructions in parallel i.e. Parallel Processing. This gives an advantage in improving the throughput and response time of the system.
Multi-threading can be implemented in a non-hyper-threaded system by running different threads on different CPU cores. Hyperthreading is more like hardware multithreading which further supplements a multithreaded application. In short, concurrency does not depend on the hyperthreading feature.
The number of Parallel Threads that can be run in any system = 2 * Number of Physical Cores in a hyperthreaded processor OR it is simply equal to the number of physical cores in a non-hyperthreaded processor.
Commands for Checking CPU Configurations on Linux
- Number of Physical CPU: cat /proc/cpuinfo |grep “physical id”|sort |uniq|wc -l
- To check the number of cores: top then press 1
- Number of Physical Cores: cat /proc/cpuinfo | grep “cpu cores”|uniq
- Number of Logical Cores: cat /proc/cpuinfo |grep “processor”|wc -l
- To check if hyperthreading is enabled or not :
When hyperthreading is enabled, count of Sibling = 2 * Count of Cores
Count of Sibling = cat /proc/cpuinfo |grep “sibling”|uniq
Count of Cores = cat /proc/cpuinfo | grep “cpu cores”|uniq
- To check the pairs of Physical and Logical Cores: cat /proc/cpuinfo | grep -e “core id” -e “processor”
In the above output, you can see that for each unique processor id the core id is repeating indicating that it is a hyperthreading enabled system.
Just like you need to rent a house as per your requirements, you need to find the processor that suits your use case. The architecture of your processor impacts a lot on your system’s performance. To give an example, a hyperthreaded processor can be beneficial in a video processing application where there is a lot of scope for multithreading. But to use the same hyperthreaded processor for running trivial day-to-day applications like notepad or music player can be an overhead. It would be cost-effective as well to go for dual-core machines for smaller use cases.
Hope this article helps you understand the nitty-gritty of CPU. Thank you for reading. And keep exploring. 😃