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