Threads
A thread is defined as a segment of a process.
A process can have multiple threads, and we can define thread as a basic unit of CPU utilization; it comprises of:
- Thread ID,
- PC
- Registers
- Stack
Since threads of one process operate in the same address space, heaps are shared.
Benefits of MT¶
- Responsiveness. If one thread of the program is blocked, other threads can continue to provide user interaction.
- Multiprocessor architecture => parallel programming
- Easy resource sharing and communication
- Cheaper than multiprocessing (context switch between threads is more light-weight)
Multiprocessing | Multithreading |
---|---|
concurrency and protection | Only concurrency |
IPC is expensive (need svc and context switch) | Thread switching cheaper since same addr space |
parallel process exec is always available on a multicore architecture | Parallel thread execution depends on the type of threads and may not be available |
synchronisaiton by svc and kernel | Synchronisation by dev |
Managed by the kernel scheduler (full ctxs + svc + flush mmu) | Managed by thread scheduler (ctxs light, only regs and stack, may not need svc) |
Types of threads¶
User Level Thread¶
green threads
- scheduled by runtime libs or venv, not kernel. Kernel is unaware of their existance!
- managed in user space, cheaper to (de)alloc
- can run on any system, take up thread data structure
- simple management done in user space, no svc, efficient
- scheduling may be bad
- what is used in Java/C (pthreads)
Kernel Level Thread¶
- scheduled by kernel, shares data with kernel but has own stack/regs. lightweight proc
- managed in kernel space, take up more res to (de)alloc
- need kernel support, take up kernel data structure
- significant overhead and kernel complexity, ctxs as expensive as svc.
- scheduling is efficiently done by kernel itself
- may not be associated with a processes
- implement bkg tasks in kerel, (async handling or waiting for event)
- kernel can use them to service multiple SVCs concurrently
- Need a TCB
Thread Mapping¶
- kernel level thread: virtual processor
- user level thread: thread
Many to One¶
Many user threads are mapped to one kernel thread
Pros:
- Thread man done by lib in user mode => efficient
- As many threads as devs want
Cons:
- EVERY user thread is blocked if ANY thread makes a blocking svc since kernel is unaware of user level threads
- Multiple threads unable to access the kernel at the same time, not actually concurrent even on multicore architectures
One to One¶
One user thread mapped to one kernel thread
Pros:
- Blocking svc on one thread doesn't affect the others
- True concurrency
Cons:
- Overhead related to creating a kernel thread
- Number of threads limited
Many to Many¶
Best of both worlds, multiplexed many user level threads to equal or lesser number of kernel threads.
- Users can create as many threads as they like
- As many concurrent threads as virtual cores
A variation of many to many is a two level model, where some threads may be mapped to one-one with kernel threads. Kernel may allocate more kernel threads to a single process on demand.
Hyper Threading¶
- A single CPU appears as two (or more) logical CPUs to the kernel.
- physical cores split into multiple virtual cores
- increases efficiency as the kernel assumes that there are two independent CPUs for utilisation
-
The idea is similar to pipelining/context switching on the hardware level
-
Data parallelism: Perform the same operation on different cores on subsets of the same dataset.
- Task parallelism: Delegation, may or may not be operating on the same dataset
Amdahl's Law¶
if \(\alpha\) is the fraction of the program that must be executed serially, then the maximum speedup that can be gained when running this program with \(N\) processors is:
Appendix¶
Daemons¶
- Bkg process that performs a specific function or system task.
- User mode.
-
System programs that are kept out of the kernel
-
Generally persistent
- No controlling terminal, (controlling
tty
process group (tpgid
)-1
) - Parent init
- Own process group
id
and sessionid
Linux Startup¶
- BIOS -> Bootloader -> OS -> setup system functions that are crucial for hardware and memory paging, perform the majority of system setups pertaining to interrupts, memory management, device, and driver initialization
idle
andinit
user processes,init
starts more daemons- Display Manager (explorer.exe) is a daemon -> Lock screen -> session (set of programs, UI elements, etc which form a complete desktop env)