Lecture 4 - Multi-Threading Flashcards
Do most modern applications use multi-threading?
Yes.
What’s the purpose of multiple threads within an application?
Multiple tasks can operate at the same time. (Like chrome browser)
Why use threads rather than create a new process?
Process creating is heavy weight.
Thread creation is super light weight.
Simplify code, increase efficiency.
What’s one advantage of processes over threads?
Processes are more isolated. If they crash, they don’t impact the other processes.
Do threads share the same address space?
Yes.
What do threads share?
The code, the data, and the files.
What do threads not share?
Registers and Stacks.
What are 4 benefits of threads?
- Responsiveness: may allow continued execution if part of process is blocked, especially important for user interfaces.
- Resource Sharing: threads share resources of process, easier than shared memory or message passing.
- Economy: cheaper than process creation, thread switching lower overhead than context switching.
- Scalability: process can take advantage of multiprocessor architectures
What the challenge that multiprocessor systems put on programmers?
Dividing activities Balance Data slitting Data dependency Testing and debugging
Define Parallelism.
Implies that a system can perform more than one task simultaneously.
Define Concurrency.
Creating the illusion of parallelism with a single processor.
Compare data parallelism vs task parallelism.
Data parallelism – distributes subsets of the same data across multiple cores, same operation on each.
Task parallelism – distributing threads across cores, each thread performing unique operation
Define Amdahl’s Law
Identifies performance gains from adding additional cores to an application that has both serial and parallel portions
speedup <= 1/ (S+ (1-S)/N)
Resulting improvement from an enhancement is “limited” by the fraction of the task that can be improved.
Explain what the Serial part and Parallel part of a process is.
Serial part = Can’t be threaded
Parallel = Can
What’s the difference between User and Kernel threads?
User threads - management done by user-level threads library (GNU Pth), they are super fast.
Kernel threads - Supported by the Kernel (POSIX PThreads)
Describe the Many-to-One multithreading model
Many user-level threads mapped to single kernel thread
One thread blocking causes all to block
Multiple threads may not run in parallel on multicore system because only one may be in kernel at a time
Used by few systems (Solaris Green, GNU Portable)
What’s the best multi-threading model?
Many-to-Many
Whats a Pthread? Is it user-level or kernel-level?
A Posix standard specification for threads (not implementation)
Can be both user and kernel-level.
4 ways a thread terminates
A thread terminates for the following:
- The start() function performs a return
- Thread calls a pthread_exit() function
- Thread is cancelled using pthread_cancel()
- Any thread calls exit() or main thread returns
How can you check if two threads are the same?
Compare their IDs with pthread_equal(pthread_t t1, pthread_t t2);
How do we join a terminated thread?
pthread_join()
Why do we need to use pthread_mutex_lock/unlock?
To protect shared variables. You have to use pthread_mutex_lock to lock the variables before accessing it to have exclusive variables. This enforces the other threads to wait for access. Otherwise the variables can become corrupt.
What’s the difference between asynchronous cancellation and deferred cancellation for threads?
- Asynchronous cancellation: terminates the target thread immediately
- Deferred cancellation: allows the target thread to periodically check if it should be cancelled at cancellation points. (Best practice)
4 Differences between Processes and Threads
Processes
- > Create a new address space at creation
- > Allocate resources at creation
- > Need IPC to share data
- > Deeper isolation for security and fault tolerance
Threads
- > Same address space
- > Quicker creation times –actual times depend on kernel versus user threads
- > Sharing through shared memory
- > Fault sharing between all threads within a process
A application is 75% parallel and 25% serial. What’s the speedup that can be achieved by going from 1 to 2 cores?
1.6
Describe a One-to-One multithreaded relationship
- > Each user-level thread maps to kernel thread
- > Creating a user-level thread creates a kernel thread
- > More concurrency than many-to-one
- > Number of threads per process sometimes restricted due to overhead
- > Examples: Windows, Linux, Solaris 9 and later
Describe a Many-To-Many multithreading model
- > Allows many user level threads to be mapped to many kernel threads
- > Allows the operating system to create a sufficient number of kernel threads
Define Thread library
+ 2 primary implementation methods
Provides programmer with API for creating and managing threads
Two primary ways of implementing
1. Library entirely in user space
2. Kernel-level library supported by the OS
Does each thread have a specific stack?
Yes. This is the threads local storage.
How do you create a pthread? Where does the thread continue? What about main?
int pthread_create(pthread_t *thread, const pthread_attr_t *atr, void (start) (void *), void *arg);
The thread continues at start()
The main continues after the statement.
How can a thread optain its own id?
using pthread_self()
What happens if you don’t join a thread that hasn’t been detached?
a “zombie” thread will be created.
How do we detach a thread?
Why would we want to do this?
Is it possible to join a detached thread?
int pthread_detach(pthread_t thread);
Default –a thread is joinable –another thread is going to retrieve the return state
If no thread is interested in joining we need to detach the thread
No, it is not possible to join to a detached thread
Define Fork-Join Parallelism in threads
Multiple threads (tasks) are forked, and then joined.
What’s the purpose of Thread Attributes?
Attributes can be used to set properties of threads –such as detached
Define Thread Cancellation
+ 2 general approaches
Terminating a thread before it has finished with
pthread_cancel(tid)
Asynchronous cancellation
-> terminates the target thread immediately
Deferred cancellation
-> allows the target thread to periodically check if it should be cancelled
What are three modes threads can be in for cancellation? What’s the default type? How is thread cancellation handled in Linux?
Off
-> cancellation remains pending until thread enables it
Deferred (default)
-> cancellation only occurs when thread reaches cancellation point (thread_testcancel()), then cleanup handler is evoked
Asynchronous
-> thread cancelled immediately
Thread cancellation is handled through signals in Linux