Processes versus Threads
In the old days, each process had exactly one thread of execution, so the processes were planned directly for the kernel (and in these old days only one core was planned). However, in operating systems that support streaming processing (these are almost all modern operating systems), these are threads, not the processes that are planned. Therefore, for the rest of this discussion, we will talk exclusively about threads, and you should understand that every running process has one or more threads of execution.
Parallelism vs concurrency
When two threads run in parallel , they both start simultaneously. For example, if we have two threads, A and B, then their parallel execution will look like this:
CPU 1: A ------------------------->
CPU 2: B ------------------------->
When two threads are running simultaneously , their execution overlaps. Overlapping can occur in one of two ways: either the threads are executed at the same time (that is, in parallel, as indicated above), or their execution is alternated on the processor, for example:
CPU 1: A -----------> B ----------> A -----------> B ----- --- ->
So, for our purposes, parallelism can be considered as a special case of concurrency *
Planning
But we can create a pool of threads (say 30) with a larger number than the number of cores we have (say 4), and run them simultaneously. How is this possible if we have only 4 cores?
In this case, they can start at the same time, because the CPU scheduler gives each of these 30 threads a fraction of the processor time. Some threads will execute in parallel (if you have 4 cores and then four threads will execute in parallel at any time), but all 30 threads will execute at the same time. The reason you can play games or browse the web is because these new threads are added to the thread pool / queue and also give a fraction of the processor time.
Logical and physical cores
According to my current understanding, a kernel can only execute one process at a time
This is not entirely true. Thanks to a very clever equipment design and pipelining that would be too long to enter here (plus I donβt understand this), one physical core can actually execute two completely different execution threads at the same time. Catch this sentence a bit if you need to - it is still blowing in my head.
This amazing feat is called simultaneous multi-threaded (or popular Hyper-Threading, although this is the brand name for a particular instance of such technology). Thus, we have physical cores , which are the actual nuclear cores of the processor, and logical cores , which are the number of cores that the operating system reports about the software. Logical kernels are abstraction. In typical modern Intel processors, each physical core acts as two logical cores.
can someone explain how this works and also recommend you a good read?
I would recommend the concept of an operating system if you really want to understand how processes, threads, and planning work.
- The exact meanings of terms, parallel and parallel, are hotly debated, even here, in our own stack overflow . What these terms mean is highly dependent on the application area.