Quiz 5 Flashcards
(24 cards)
What is a thread?
A new abstraction for a single running process
What characterizes a multi-threaded program?
It has more than one point of execution
What do multiple threads share in a multi-threaded program?
The same address space
What is needed to store the state of each thread?
One or more thread control blocks (TCBs)
What happens during a context switch between threads?
The register state of the running thread is saved and the register state of the new thread is restored
What does the code segment contain?
Instructions for the program
What does the heap segment contain?
Dynamic data structures and malloc’d data
What does the stack segment contain?
Local variables, arguments to routines, return values, etc.
Why use threads in programming?
To achieve parallelism and improve program speed on modern hardware
What does parallelization refer to?
Transforming a single-threaded program into a multi-threaded program that can run on multiple CPUs
What is a critical section?
A piece of code that accesses a shared variable and must not be concurrently executed by more than one thread
What is a race condition?
A situation where the results depend on the timing of execution of the code
What is the ideal approach for ensuring atomicity in critical sections?
Make the increment operation a single assembly instruction
What is the purpose of locks in multi-threading?
To provide mutual exclusion to a critical section
What function is used to create a thread in C?
pthread_create()
What parameters does pthread_create() take?
thread, attr, start_routine, arg
What is pthread_join() used for?
To wait for a thread to complete
What should you not do when returning a pointer from a thread?
Do not return a pointer to a local variable from a thread that is being terminated
What must be done before using locks?
All locks must be properly initialized
How can locks be initialized dynamically?
Using pthread_mutex_init()
What flag must be added when compiling code that uses pthreads?
-pthread
What is the expected result of incrementing a shared counter in a race condition?
Indeterminate result
Fill in the blank: A multi-threaded program has more than one point of _______.
execution
True or False: The stack of each thread is shared among all threads.
False