Purdue CS 252 Final Study Guide Exam
With Complete Solutions
thread - ANSWER Path of execution. By default, each program has a main
thread that executes main().
Every thread has its own...
Stack
PC - Program Counter
Set of registers
State
What are some of the application of threads? - ANSWER Threads help
divide up the work, can be used in applications like servers. This leads to a
faster overall completion time if they are doing independent tasks.
What are the advantages of threads? (versus processes) - ANSWER *Fast
thread creation* - creating a new path of execution is easier.
*Fast context switch* - context switch across threads is faster than across
processes.
*Faster communication* - fast & easy to talk between threads. (processes
require pipes, etc).
What are the disadvantages of threads? (versus processes) - ANSWER
*Threads are less robust* - if one thread crashes, the entire application will
crash. If one process crashes, all other processes remain running.
*More sync problems* - Since threads modify the same global variables at
the same time, they may corrupt data structures. Synchronization is
required.
, What is an atomic section of code? - ANSWER A portion of code that only
one thread should execute at a time, while the other threads wait.
Sometimes called a "critical section".
What are mutex locks? - ANSWER Mutex locks are software mechanisms
that enforce atomicity. They enforce mutal exclusion of two or more
sections of code if the same lock is used.
What are the two mutex lock implementations? - ANSWER *Disable
interrupts*: Used only in Kernel mode, so that context switches cannot
happen. May lose interrupts and can be slow.
*Spin locks*: Can be implemented in user space, rely on the test_and_set
assembly instruction (since it is guaranteed atomic).
What are spin locks? - ANSWER A type of lock that rely on the
test_and_set assembly instruction, which is guaranteed to be atomic.
Mutex locks are implemented on top of spin locks.
Spin locks makes the thread "spin", or keep checking a while loop, rather
than putting the thread to sleep.
What are the advantages and disadvantages of spin locks? - ANSWER
*Advantages*
- Does not have any system overhead since the threads are not being put
to sleep
- Easy to implement, can be used in small sections that take a short
amount of time.
*Disadvantages*
- Spin locks do not enforce fairness, whichever thread unlocks it can use it.
There is no FIFO queue.
- Clock cycles are wasted since the threads do not do anything besides
"spin".
What is the difference between calling yield() in a spin lock versus nothing?
- ANSWER If we decide to call pthread_yield(), we are checking the
condition once, then devoting the rest of our clock cycles to other
processes. There is no need for the thread to constantly check the
condition of the lock, it can do so on the next quantum. Thus, if we use the
yield command, the thread that has the lock can complete more work,
With Complete Solutions
thread - ANSWER Path of execution. By default, each program has a main
thread that executes main().
Every thread has its own...
Stack
PC - Program Counter
Set of registers
State
What are some of the application of threads? - ANSWER Threads help
divide up the work, can be used in applications like servers. This leads to a
faster overall completion time if they are doing independent tasks.
What are the advantages of threads? (versus processes) - ANSWER *Fast
thread creation* - creating a new path of execution is easier.
*Fast context switch* - context switch across threads is faster than across
processes.
*Faster communication* - fast & easy to talk between threads. (processes
require pipes, etc).
What are the disadvantages of threads? (versus processes) - ANSWER
*Threads are less robust* - if one thread crashes, the entire application will
crash. If one process crashes, all other processes remain running.
*More sync problems* - Since threads modify the same global variables at
the same time, they may corrupt data structures. Synchronization is
required.
, What is an atomic section of code? - ANSWER A portion of code that only
one thread should execute at a time, while the other threads wait.
Sometimes called a "critical section".
What are mutex locks? - ANSWER Mutex locks are software mechanisms
that enforce atomicity. They enforce mutal exclusion of two or more
sections of code if the same lock is used.
What are the two mutex lock implementations? - ANSWER *Disable
interrupts*: Used only in Kernel mode, so that context switches cannot
happen. May lose interrupts and can be slow.
*Spin locks*: Can be implemented in user space, rely on the test_and_set
assembly instruction (since it is guaranteed atomic).
What are spin locks? - ANSWER A type of lock that rely on the
test_and_set assembly instruction, which is guaranteed to be atomic.
Mutex locks are implemented on top of spin locks.
Spin locks makes the thread "spin", or keep checking a while loop, rather
than putting the thread to sleep.
What are the advantages and disadvantages of spin locks? - ANSWER
*Advantages*
- Does not have any system overhead since the threads are not being put
to sleep
- Easy to implement, can be used in small sections that take a short
amount of time.
*Disadvantages*
- Spin locks do not enforce fairness, whichever thread unlocks it can use it.
There is no FIFO queue.
- Clock cycles are wasted since the threads do not do anything besides
"spin".
What is the difference between calling yield() in a spin lock versus nothing?
- ANSWER If we decide to call pthread_yield(), we are checking the
condition once, then devoting the rest of our clock cycles to other
processes. There is no need for the thread to constantly check the
condition of the lock, it can do so on the next quantum. Thus, if we use the
yield command, the thread that has the lock can complete more work,