Lecture 5 - Threads Flashcards
What is a thread?
A small unit of execution within a process
Multiple threads can execute concurrently (using timeslicing like multiprogramming) doing different jobs within the process
How are threads created?
CreateThread(fn, arg0, arg1, …);
What two concepts does process abstraction combine?
Concurrency - each process sequential execution of stream of instructions
Protection - each process has its own address space
What do threads do, compared to processes?
Separate the concepts of concurrency from protection:
Sequential stream of instructions, like a process
SAME address space within process
Can communicate by updating memory - don’t need to send messages
Can execute on different cores - true parallelism
What is the difference between a single threaded and multithreaded process?
Single threaded - one thread - one set of registers, one stack
Multithreaded - multiple threads - a set of registers and stack each
What do threads offer that processes dont?
More efficient - less overhead to create
Sharing memory is easy/automatic
Can take advantage of multiple CPUs within the one process
Cheap context switching
What does the thread control block contain?
thread specific info: stack pointer, program counter, state, register values, pointer to pcb etc.
What library is used in C to get at threads?
pthreads
What function from pthreads can be used to create a thread?
pthread_create
What is the difference between a kernel thread and user-level thread?
Kernel thread - supported by OS, scheduling and synchronisation controlled by OS
User-level - supported by user level code, e.g. thread library - handles scheduling, creation, etc. kernel just sees the one process
What is the downside of kernel threads?
a bit expensive - each thread operation is a syscall
What is the downside of user threads/
If any thread in a process has to wait for the kernel, every other thread does too