Processes vs Threads

source: https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/ProcVThreads.html

Each process has it own unique virtual memory space, this includes code segment, heap, stack, …
Every process begins by executing the instructions contained usually define in main() , the %pc register contains a pointer to the next instruction to be executed. This single, logical sequence of executing instructions within a process is known as a thread of execution, which we typically just call a thread.

💡 A thread: A coherent and independent execution sequence of software instructions.

Multithreading**

just like the thread that start executing main() , a process cloud have multiple threads each one has it entry point. To maintain the logical flow of these additional threads, each thread is assigned a separate stack. However, all of the other segments of memory, including the code, global data, heap, and kernel, are shared.

💡 rocesses act as containers for resource ownership. As the process runs, it may request access to files stored on the disk, open network connections, or request the creation of a new window on the desktop. All of these resources are allocated to the process, not individual threads.

The main distinction between a thread switch and a process switch is
that during a thread switch, the virtual memory space remains the same,
while it does not during a process switch.
Both types involve handing control over to the operating system kernel
to perform the context switch. The process of switching in and out of
the OS kernel along with the cost of switching out the registers is the
largest fixed cost of performing a context switch.

A more fuzzy cost is that a context switch messes with the processors
cacheing mechanisms. Basically, when you context switch, all of the
memory addresses that the processor "remembers" in its cache effectively
become useless. The one big distinction here is that when you change
virtual memory spaces, the processor's Translation Lookaside Buffer
(TLB) or equivalent gets flushed making memory accesses much more
expensive for a while. This does not happen during a thread switch.

💡 At the same time, the lack of isolation between threads in a single process can also lead to some disadvantages. If one thread crashes due to a segmentation fault or other error, all other threads and the entire process are killed. This situation can lead to data and system corruption if the other threads were in the middle of some important task.

image 4.png

image.png

Thread Models

Thread per connection model

image.png

Event-driven threading model

image.png

💡 you should always try the Event-driven threading model first, if that does not work well then try the older model the Thread per connection

image.png

image.png