Search code examples
multithreadingmultiprocessingoperating-systemprocessorhyperthreading

Multi Processing vs Multi Threading and Hyper-Threading


What is the difference between thread and process? Threads can use multi core and process use multi core too. Threads shares same memory but process cant share. What if we use shared memory for multi-Processing? Are they same or not? When we use multi-threading or multi-processing?

I want to know about what is hyper-threading? How can two process or thread use same core on same time? There is 1 ALU only in 1 core. It can be impossible.

I research process and threads. Inspect process explorer. If Chrome create new tab, there is 1-5 process created from Chrome. Every process have 3-10 thread. I don't know why. There is only 6 tab on chrome but lots of process there are.

If I inspect a process from random tab. Its look like. Chrome Process Threads


Solution

  • What is the difference between thread and process?

    In a nutshell, a process is a container for all of the resources that are used by one or more threads. Threads execute a program's code. The process contains all of the virtual memory, open file handles, sockets, windows, and other system resources that are used by those threads.

    Every "live" process must have at least one "live" thread. Every thread must belong to exactly one process.


    Threads shares same memory but process cant share

    Threads that belong to the same process automatically share their entire virtual address space. Processes can share parts of their virtual address space in many operating systems (all of the "big" ones, for sure); but it doesn't happen automatically. The cooperating processes must take specific actions—call specific system calls—to create shared memory regions.


    what is hyper-threading?

    If you want to understand hyper-threading, then you must first understand what a thread context is. In short, a thread's context is the set of all of the values that must be loaded into a CPU's hardware registers so that the CPU can start executing the instructions on behalf of that thread. You've probably heard of "context switch." That's when the operating system scheduler

    • Interrupts a CPU that was running thread A,
    • Copies the values from the CPU's context registers into the "saved context record" for thread A,
    • Copies values from the saved context of thread B into the CPU's context registers, and finally
    • Returns from the interrupt, allowing the CPU to work on behalf of thread B.

    A hyper-threaded CPU has two or more complete sets of context registers. That enables it to switch its attention between the instruction streams of two or more threads without any need for all of that interrupting and copying of register values that the Scheduler has to do.

    All by itself, that capability gives us some speedup because, for example, memory reads and writes can take a long time as compared to register-to-register operations. A hyper-threaded CPU that starts a memory fetch for thread A may be able to do some work on behalf of thread B while it awaits the value that thread A wanted to read.


    How can two process or thread use same core on same time? There is 1 ALU only in 1 core.

    Many high-end processors for server, desktop and even mobile systems have so-called superscalar architecture. A single CPU "core" can have several ALUs, and several each of other so-called "functional units." There are various ways that it can make simultaneous use of those functional units including (if it also is a hyper-threaded processor) simultaneously using different ALUs for different threads.


    P.S., I won't pretend to understand the engineering decisions—I am not a computer hardware architect—but, if you're asking why not simply put 2 CPU cores on one chip instead of one core with two sets of registers and two of some functional units? Then it's because somebody who spent a long time studying the problem thought it was the optimum way to get a certain level of overall performance out of a certain size of silicon chip.