Slides 7 Flashcards
How do threads compare to processes?
They are more efficient
What are some things threads share and don’t share?
share:
code
data
open files
don’t share:
registers
stack
Program Counters
What are some common thread scenarios?
pipeline
• a task is broken into a series of stages, where output of stage (i) is input
to stage (i+1)
• each stage handled by a different thread
manager/worker (aka master/slave)
• one manager thread assigns work to worker threads • manager thread handles all I/O
• worker threads can be static or dynamic
peer
• all threads work on the same or different tasks in parallel
What is a thread pool?
thread pool ― a soware design paern
• program creates and maintains a pool of worker threads
• pool size can be tuned, eg. to the available computing resources
• when program needs a thread, it is takes one out of the pool
• when thread is done, program returns the thread back to the pool (thread recycling)
Do thread libraries make system calls?
Typically thread libraries contain higher level wrappers for system calls
How do you create a thread?
pthread_create(thread, attr, start_routine, arg)
thread = address of thread
attribute is idk
start_routine = function u want thread to execute
arg = address of argument for function you want thread to run on
What do the pthread exit and join functions do?
pthread_exit(status)
• terminates the current thread, similar to exit(), or you can return
from start_routine
pthread_join(thread, * status)
• blocks the calling thread until the specified thread terminates, similar to
wait()
How does signal handling work with threads?
some signals are thread specific:
• eg.SIGSEGV is delivered to the thread that caused the exception
• pthread_kill(thread_id, signal) is only delivered to the target thread
• most signals are delivered to the process
• only one thread will handle the signal (usually the main thread, but can be arbitrary)
•can change which thread handles which signal using pthread_sigmask()
• example: default behavior of → SIGINT, kills all threads
What are the properties of user level threads?
• threads are implemented entirely in user space
• requires no support from OS → can be used on OSes that don’t support threads
• each process has its own thread table and scheduler
threads usually switch only on I/O requests
• no need to trap into kernel when switching
threads, so they are very efficient
• allows custom management and scheduling
• requires OS to support non-blocking I/O
• each additional thread makes other threads run slower
• some issues with paging
What are the properties of kernel level threads?
one master thread table at the kernel level
• thread creation/deletion/scheduling done in the kernel space
works well when lot of blocking I/O ops needed
• processes with multiple threads run faster
• each thread can get same CPU time
• less efficient, since thread operations
need to trap into the kernel
• increased kernel complexity
What are the pros and cons of user level threads?
Pros: no need for OS support ● fast context switch ● no traps are needed ● customized scheduling
Cons: needs non-blocking system calls ● a thread may run forever ● page faults ● inefficient for threads with many blocking procedure/system calls ● all threads get one time slice
What are the pros and cons of kernel level threads?
Pros:
blocking calls are no problem
● OS aware of all threads → more efficient global scheduling
Cons:
some issues around fork()
● sending signals to threads
What are the 3 thread models?
N:1 (many-to-one) or user-level threads
• many user-level threads per single kernel thread
• thread management is done by the thread library in the user space • E.g., Solaris Green Threads, GNU Portable Threads
• 1:1 (one-to-one) or kernel-level threads
• maps each user thread to a kernel thread
• E.g., Windows NT/XP/2000, Linux, Solaris 9 and later
• M:N (many-to-many) or hybrid user/kernel level threads
• multiplexes many user-level threads to a smaller or equal number of
kernel threads
• eg. Marcel, a multithreading library for HPC