Podcast
Questions and Answers
Which of the following is NOT a typical benefit of using multithreading in applications?
Which of the following is NOT a typical benefit of using multithreading in applications?
- Simplified code
- Increased efficiency
- Reduced memory footprint (correct)
- Improved responsiveness
In the context of multithreading, what does concurrency primarily refer to?
In the context of multithreading, what does concurrency primarily refer to?
- The division of a program into multiple, independent processes.
- The ability of a system to perform multiple tasks simultaneously using multiple cores.
- The ability of a single processor to handle multiple tasks making progress. (correct)
- The actual simultaneous execution of multiple tasks.
With respect to parallelism, what is meant by 'data splitting'?
With respect to parallelism, what is meant by 'data splitting'?
- Splitting a large task into subtasks and assigning each to a different thread.
- Dividing the processor workload equally between threads.
- Dividing data into smaller segments for parallel processing. (correct)
- Splitting a code into different functions.
What does Parallelism imply in the context of multi-core processors?
What does Parallelism imply in the context of multi-core processors?
According to Amdahl's Law, what factor has the most disproportionate effect on performance gains when adding additional cores?
According to Amdahl's Law, what factor has the most disproportionate effect on performance gains when adding additional cores?
In the context of threading, what is a 'user thread'?
In the context of threading, what is a 'user thread'?
What is a key difference between kernel threads and user threads?
What is a key difference between kernel threads and user threads?
Which of the following is a characteristic of the many-to-one multithreading model?
Which of the following is a characteristic of the many-to-one multithreading model?
In the context of multithreading models, what is the primary advantage of the one-to-one model?
In the context of multithreading models, what is the primary advantage of the one-to-one model?
Which of the following models allows a user thread to be bound to a kernel thread?
Which of the following models allows a user thread to be bound to a kernel thread?
What is the primary role of a thread library?
What is the primary role of a thread library?
Which of the following statements about Pthreads is correct?
Which of the following statements about Pthreads is correct?
What is the primary advantage of using thread pools?
What is the primary advantage of using thread pools?
In the context of implicit threading, what is the main goal of OpenMP?
In the context of implicit threading, what is the main goal of OpenMP?
What is the primary function of Grand Central Dispatch (GCD) in macOS and iOS?
What is the primary function of Grand Central Dispatch (GCD) in macOS and iOS?
When dealing with multithreaded programs, what is a key consideration regarding the fork()
system call?
When dealing with multithreaded programs, what is a key consideration regarding the fork()
system call?
In the context of signal handling, what is a 'signal handler'?
In the context of signal handling, what is a 'signal handler'?
In multithreaded programs, what is a potential strategy for delivering a signal?
In multithreaded programs, what is a potential strategy for delivering a signal?
What is the primary difference between asynchronous and deferred thread cancellation?
What is the primary difference between asynchronous and deferred thread cancellation?
What is the purpose of thread-local storage (TLS)?
What is the purpose of thread-local storage (TLS)?
What is the role of Scheduler Activations in multithreading?
What is the role of Scheduler Activations in multithreading?
In Windows, what is the 'context' of a thread composed of?
In Windows, what is the 'context' of a thread composed of?
In the Windows operating system, what is the purpose of ETHREAD?
In the Windows operating system, what is the purpose of ETHREAD?
In the Windows operating system, what is contained in the KTHREAD data structure?
In the Windows operating system, what is contained in the KTHREAD data structure?
What is the primary function of the clone()
system call in Linux with regards to threads?
What is the primary function of the clone()
system call in Linux with regards to threads?
In Linux, what does the CLONE_VM
flag signify when used with the clone()
system call?
In Linux, what does the CLONE_VM
flag signify when used with the clone()
system call?
Which of the following best describes the concept of data parallelism?
Which of the following best describes the concept of data parallelism?
What is the key benefit of using threads over processes?
What is the key benefit of using threads over processes?
In the context of concurrency, what does balance refer to?
In the context of concurrency, what does balance refer to?
Assume an application is 80% parallel and 20% serial. According to Amdahl's Law, what is the maximum speedup achievable with an infinite number of cores?
Assume an application is 80% parallel and 20% serial. According to Amdahl's Law, what is the maximum speedup achievable with an infinite number of cores?
Which of the following is NOT a valid thread library?
Which of the following is NOT a valid thread library?
Which of the following is a disadvantage of using thread pools?
Which of the following is a disadvantage of using thread pools?
Which of the following problems are introduced in multithreaded programming?
Which of the following problems are introduced in multithreaded programming?
For multithreaded programming, what is the goal of concurrency?
For multithreaded programming, what is the goal of concurrency?
Which of the following is not a goal of multithreaded programming?
Which of the following is not a goal of multithreaded programming?
Which of the following is not a strategy of implicit threading?
Which of the following is not a strategy of implicit threading?
Flashcards
What is a Thread?
What is a Thread?
A fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Multithreaded Applications
Multithreaded Applications
Modern applications use multiple threads to perform various tasks.
Responsiveness (Threads)
Responsiveness (Threads)
Continued execution if part of a process is blocked; important for user interfaces.
Resource Sharing (Threads)
Resource Sharing (Threads)
Signup and view all the flashcards
Economy (Threads)
Economy (Threads)
Signup and view all the flashcards
Scalability (Threads)
Scalability (Threads)
Signup and view all the flashcards
Multicore Programming Challenges
Multicore Programming Challenges
Signup and view all the flashcards
Data Parallelism
Data Parallelism
Signup and view all the flashcards
Task Parallelism
Task Parallelism
Signup and view all the flashcards
User Threads
User Threads
Signup and view all the flashcards
Kernel Threads
Kernel Threads
Signup and view all the flashcards
Many-to-One Model
Many-to-One Model
Signup and view all the flashcards
One-to-One Model
One-to-One Model
Signup and view all the flashcards
Many-to-Many Model
Many-to-Many Model
Signup and view all the flashcards
Two-level Model
Two-level Model
Signup and view all the flashcards
Thread Library
Thread Library
Signup and view all the flashcards
Pthreads
Pthreads
Signup and view all the flashcards
Implicit Threading
Implicit Threading
Signup and view all the flashcards
Thread Pools
Thread Pools
Signup and view all the flashcards
OpenMP
OpenMP
Signup and view all the flashcards
Grand Central Dispatch
Grand Central Dispatch
Signup and view all the flashcards
Serial Dispatch Queue
Serial Dispatch Queue
Signup and view all the flashcards
Concurrent Dispatch Queue
Concurrent Dispatch Queue
Signup and view all the flashcards
Threading Issues
Threading Issues
Signup and view all the flashcards
Signals
Signals
Signup and view all the flashcards
Thread Cancellation
Thread Cancellation
Signup and view all the flashcards
Asynchronous Cancellation
Asynchronous Cancellation
Signup and view all the flashcards
Deferred Cancellation
Deferred Cancellation
Signup and view all the flashcards
Thread-Local Storage (TLS)
Thread-Local Storage (TLS)
Signup and view all the flashcards
Scheduler Activations
Scheduler Activations
Signup and view all the flashcards
Lightweight Process (LWP)
Lightweight Process (LWP)
Signup and view all the flashcards
Upcalls
Upcalls
Signup and view all the flashcards
Linux Threads as Tasks
Linux Threads as Tasks
Signup and view all the flashcards
clone() System Call
clone() System Call
Signup and view all the flashcards
CLONE_FILES
CLONE_FILES
Signup and view all the flashcards
CLONE_FS
CLONE_FS
Signup and view all the flashcards
CLONE SIGHAND
CLONE SIGHAND
Signup and view all the flashcards
CLONE_VM
CLONE_VM
Signup and view all the flashcards
Study Notes
Threads
- Threads are a fundamental unit of CPU utilization in multithreaded computer systems.
- The use of threads simplifies code and increases efficiency.
- Kernels are generally multithreaded
Objectives of Thread
- Introduce the concept of a thread
- Discuss the APIs for Pthreads, Windows, and Java thread libraries.
- Explore strategies for implicit threading.
- Examine issues related to multithreaded programming.
- Cover operating system support for threads in Windows and Linux.
Motivation
- Most modern applications are multithreaded.
- Threads run within an application to implement multiple tasks.
- Process creation is heavy-weight while thread creation is light-weight.
Multithreaded Server Architecture
- A client sends a request to the server
- The server creates a new thread to handle the request.
- The server resumes listening for additional client requests.
Benefits of Threads
- Responsiveness: Continued execution if part of the process is blocked.
- Resource Sharing: Threads share resources, easier than shared memory or message passing.
- Economy: Thread creation is cheaper than process creation.
- Scalability: Processes can take advantage of multiprocessor architectures.
Multicore Programming
- Multicore or multiprocessor systems put pressure on programmers to utilize resources in optimal way.
- Challenges of multicore programming include dividing activities, balancing workloads, splitting data, handling data dependencies, and testing/debugging.
- Parallelism: A system performs more than one task simultaneously.
- Concurrency: More than one task makes progress.
- Concurrency can be achieved via a single processor/core or scheduler.
- Data parallelism distributes subsets of the same data across multiple cores, applying the same operation on each.
- Task parallelism distributes threads across cores, each performing a unique operation.
- CPUs have cores as well as hardware threads.
- Oracle SPARC T4 has 8 cores and 8 hardware threads per core.
Amdahl’s Law
- Amdahl's Law identifies performance gains from adding cores to an application with serial and parallel components.
- Performance is limited by serial sections of code
- The formula for speedup is: speedup ≤ 1 / (S + (1-S)/N), where S is the serial portion and N is the number of cores.
User Threads vs Kernel Threads
- User threads are managed by a user-level threads library.
- Three primary thread libraries are POSIX Pthreads, Windows threads, and Java threads
- Kernel threads are supported by the Kernel.
- Examples of systems that use kernel threads are Windows, Solaris, and Linux.
Multithreading Models
- Many-to-One: Many user-level threads mapped to a single kernel thread.
- One-to-One: Each user-level thread maps to a kernel thread.
- Many-to-Many: Allows many user-level threads to be mapped to many kernel threads.
- Two-Level: Similar to Many-to-Many model, but allows a user thread to be bound to kernel thread
Thread Libraries
- A thread library provides an API for creating and managing threads.
- Libraries can be entirely in user space or supported by the OS kernel.
Pthreads
- Pthreads is a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.
- It specifies the behavior of the thread library, while the implementation is up to the library developer.
- Pthreads is common in UNIX operating systems like Solaris, Linux, and Mac OS X.
Implicit Threading
- The number of threads increase, program correctness becomes more difficult with explicit threads.
- Creation and management of threads is done by compilers and run-time libraries rather than programmers.
- Methods: Thread Pools, OpenMP and Grand Central Dispatch.
- Other options include Microsoft Threading Building Blocks (TBB), java.util.concurrent package.
Thread Pools
- Thread pools create a number of threads that await work.
- The advantages of using thread pools are that it is usually slightly faster and limit the number of threads.
- Task scheduling becomes more flexible and tasks can be scheduled to run periodically.
- Windows API supports thread pools.
OpenMP
- OpenMP provides support for parallel programming in shared-memory environments.
- It identifies parallel regions, which are blocks of code that can run in parallel.
- Directives are used to create as many threads as there are cores, and code can direct loops to run in parallel.
Grand Central Dispatch
- Grand Central Dispatch (GCD) is an Apple technology for Mac OS X and iOS operating systems.
- GCD includes extensions to C, C++, an API, and a run-time library.
- It allows identification of parallel sections and manages threading details.
- Blocks, which contain code, are placed in dispatch queues and assigned to available threads from a thread pool when they are removed from the queue.
Types of Grand Central Dispatch
- Serial – blocks removed in FIFO order, queue is per process, called main queue
- Programmer can create queues within program
- Concurrent – removed in FIFO order but several may be removed at the same time
- 3 System wide queues with priorities low, default, high
Threading Issues
- Semantics of
fork()
andexec()
system calls. - Signal handling is the delivery of signals to threads.
- Thread cancellation is terminating a thread before it completes, achieved through asynchronous or deferred cancellation.
- Thread-local storage allows each thread to have its own copy of data and is different from local variables
- Scheduler Activations.
Semantics of Fork and Exec
- Fork duplicates only the calling thread or all threads in the program.
- Some UNIX systems have two version of fork
- Exec usually works as normal - replace the running process including all threads
Signal Handling
- Signals are used in UNIX systems to notify a process that a particular event has occurred.
- A signal handler is used to process signals.
- A signal is generated by an event and delivered to a process.
- The signal is be handled by default (kernel-defined) or user-defined logic.
- Every signal has a default handler that the kernel runs when handling the signal
- A user-defined signal handler can override default
- For single-threaded processes, the signal is generally delivered to the process.
Where Signals are Delivered
- Multi-threaded signal delivery options:
- To the thread to which it applies
- To every thread in the process
- To certain threads in the process
- Assigning a specific thread to receive all signals for the process
Thread Cancellation
- Terminating of a thread before it has finished
- Asynchronous cancellation terminates the target thread immediately.
- Deferred cancellation allows the thread to periodically check if it should be cancelled.
Thread Cancellation requests
- Involves invoking a thread cancellation, actual cancellation depends on the thread state.
- If thread has cancellation disabled, cancellation remains pending until the thread enables it.
- Default type is deferred
- Cancellation only occurs when the thread reaches cancellation point.
On Linux systems
- Thread cancellation is handled through signals
Thread Local Storage
- Thread-local storage (TLS) allows each thread to have its own copy of data.
- Useful when there is no control over the thread creation process (i.e., when using a thread pool)
- It is different from local variables, which are visible only during single function invocation. TLS is visible across functions invocations
- TLS is unique to each thread
Scheduler Activations
- Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application
- Typically use an intermediate data structure between user and kernel threads - lightweight process(LWP)
- Appears to be a virtual processor on which process can schedule user thread to run
- Each LWP attached to kernel thread
- Scheduler activations provide upcalls - a communication mechanism from the kernel to the upcall handler in the thread library
- This communication allows an application to maintain the correct number kernel threads
Operating System Examples
- Windows Threads and Linux Threads
Windows Threads
- Windows implements the Windows API – primary API for Win 98, Win NT, Win 2000, Win XP, and Win 7
- Implements the one-to-one mapping, kernel-level.
- Each thread contains: a thread ID, a register set representing the state of the processor, separate user and kernel stacks, and a private data storage.
- Thread-Local Storage (TLS) is used by run-time and dynamic-link libraries (DLLs).
- The register set, stacks, and private storage area are known as the context of the thread. The primary data structures of a thread include:
- ETHREAD (executive thread block) includes pointer to process to which thread belongs and to KTHREAD, in kernel space
- KTHREAD (kernel thread block) scheduling and synchronization info, kernel-mode stack, pointer to TEB, in kernel space
- TEB (thread environment block) thread id, user-mode stack, thread-local storage, in user space
Linux Threads
- Linux refers to them as tasks rather than threads.
- Thread creation is done through
clone()
system call clone()
allows a child task to share the address space of the parent task (process).- Used flags control behavior.
struct task_struct
points to process data structures (shared or unique).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.