index.md (1908B)
1 +++ 2 title = 'Threads' 3 +++ 4 # Threads 5 ## Multithreaded execution: 1 process, N threads of execution 6 7 - lightweight processes, allow space and time efficient parallelism 8 - organised in thread groups, allow simple communication and synchronisation 9 10 ![](68f6da63d8006625153c23148f477ac3.png) 11 12 - threads are in the same address space of a single process 13 - all info exchange done via data shared between threads 14 - threads synchronise via simple primitives 15 - each thread has its own stack, hardware registers, and state 16 - each thread may call any OS-supported syscall on behalf of the process to which it belongs 17 18 ## User threads - pros and cons 19 20 - (+) thread switching time is quicker, as no mode switch 21 - (+) scalability customizability (since no in-kernel management) 22 - (-) parallelism (blocking syscalls are problematic) 23 - (-) transparency (typically needs app cooperation) 24 25 ![](fe2bd8611012458563c85e4d08b7b87f.png) 26 ** 27 ** 28 ## Hybrid implementations of threads: 29 30 - thread multiplexing: one kernel thread runs everything 31 - virtualises N user threads over 1 kernel thread 32 - mitigates transparency and parallelization issues 33 - scheduler activation: scheduler takes care of blocking/unblocking 34 - upcalls notify userland of blocking/unblocking events 35 - userland schedules thread accordingly 36 - pop-up threads: kernel takes care of creating threads 37 - kernel spawns new thread in response to event 38 - thread may run at kernel or user level 39 40 ## POSIX threads (Pthreads) 41 pthread is a library interface. names may not always be the same. 42 43 ![](79ff59028342a53fae33cbd60a02dcf7.png) 44 45 ## Inter-process communication (IPC) 46 47 - Processes need some way to communicate - to share data during execution 48 - No explicit cross-process sharing, data must be normally exchanged between processes 49 - Processes need a way to synchronise 50 - to account for dependencies 51 - to avoid them interfering with each other