Chapter 4 Threads & Concurrency Flashcards
What are threads?
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter (PC), a register set, and a stack.
What do threads share with other threads?
It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files and signals
How many threads does a traditional process have?
One
What is the use of threads?
Multiple threads can perform more than one task at a time.
Examples of Threading
1.) An application that creates photo thumbnails from a collection of images may use a separate thread to generate a thumbnail from each separate
image.
2.) A web browser might have one thread display images or text while another thread retrieves data from the network.
3.) A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background.
Why are threads used?
They can be used to leverage processing capabilities on multicore systems. Such applications can perform several CPU-intensive tasks in
parallel across the multiple computing cores.
Before threads, what method was used to receive multiple requests at the same time?
Process-creation was the method used before threads. It involved creating a new process when a server received a new request. However, this method was deemed too confusing.
The 4 Major Benefits of Threads
Responsiveness
Resource sharing
Economy
Scalability
1st Benefit: Responsiveness
Multithreading an interactive application may allow a
program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.
A single-threaded application would be unresponsive to the user until the operation had been completed. In contrast, if the time-consuming operation is performed in a separate, asynchronous thread, the application remains responsive to the user.
2nd Benefit: Resource Sharing
Processes can share resources only through techniques
such as shared memory and message passing. Such techniques must be explicitly arranged by the programmer.
However, threads share the
memory and the resources of the process to which they belong by default. The benefit of sharing code and data is that it allows an application to
have several different threads of activity within the same address space.
3rd Benefit: Economy
Because threads share the resources of the process to which they belong, it is more economical to create and context-switch threads.
In general, thread creation consumes less time and memory than process
creation. Additionally, context switching is typically faster between threads than between processes.
4th Benefit: Scalability
The benefits of multithreading can be even greater in a multiprocessor architecture, where threads may be running in parallel on different processing cores.
What is a multicore system?
Multiple computing cores on a single processing chip, where each core appears as a separate CPU to the operating system.
How do multithreading and multicore relate?
Multithreaded programming provides a mechanism for more efficient use of the multiple computing cores in the multicore system and improved concurrency.
Concurrency in Single Threaded Programs
On a system with a single computing core,
concurrency merely means that the execution of the threads will be interleaved over time
Concurrency in Multithreaded Programs
On a system with multiple cores, concurrency means that some threads can run in parallel because the system can assign a separate thread to each core
Concurrent System
A concurrent system supports more than one task by allowing all the tasks
to make progress.
Parallel System
A parallel system can perform more than one task simultaneously. Thus, it is possible to have concurrency without parallelism.
Challenges for Programming in Multicores Systems
1.) Identifying Tasks
2.) Balance
3.) Data Splitting
4.) Data Dependency
5.) Testing and Debugging
1st Problem: Identifying Tasks
This involves examining applications to find areas that can be divided into separate, concurrent tasks. Ideally, tasks are independent of one another and thus can run in parallel on individual
cores.
2nd Problem: Balance
While identifying tasks that can run in parallel, programmers must also ensure that the tasks perform equal work of equal value. Some tasks may not contribute as much, so the cost of another core might not be worth it.
3rd Problem: Data Splitting
Just as applications are divided into separate tasks, the data accessed and manipulated by the tasks must be divided to run on separate cores.
4th Problem: Data Dependency
The data accessed by the tasks must be examined for dependencies between two or more tasks. When one task depends on
data from another, programmers must ensure that the execution of the
tasks is synchronized to accommodate the data dependency.
5th Problem: Testing and Debugging
When a program is running in parallel on multiple cores, many different execution paths are possible, making it more difficult for testing and debugging.
What are the two types of parallelism?
Data Parallelism
Task Parallelism
Data Parallelism
Focuses on distributing subsets of the same data across multiple computing cores and performing the same operation on each core.
Data parallelism involves the distribution of data across multiple cores
Data Parallelism Example
Summing the contents of an array of size N. On a single-core system, one thread would simply sum the elements [0]…[N − 1]. On a dual-core system, however, thread A, running on core 0, could sum the
elements [0]…[N∕2 − 1] while thread B, running on core 1, could sum the
elements [N∕2]…[N − 1].
Task Parallelism
Involves distributing not data but tasks (threads) across multiple computing cores. Each thread is performing a unique operation. Different threads may be operating on the same data, or they may be operating on different data.
Task parallelism involves the distribution of tasks across multiple cores
Task Parallelism Example
An example of task parallelism might involve two threads, each performing
a unique statistical operation on the array of elements. The threads again are
operating in parallel on separate computing cores, but each is performing a
unique operation.
User Threads
User threads are supported above the kernel and are managed without kernel support
Kernel Threads
Kernel threads are supported
and managed directly by the operating system.
Many-to-One Model
The many-to-one model maps many user-level threads to one
kernel thread. Thread management is done by the thread library in user space, so it is efficient
However, the entire process will block if a thread makes a blocking system call. Also, because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multicore systems.
Prevents Parallelism.
(Ex.) Green Threads (Early Java)
One-to-One Model
The one-to-one model maps each user thread to a kernel thread. It provides more concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call.
It also allows multiple threads to run in parallel on multiprocessors. The only drawback to this model is that creating a user thread requires creating the corresponding kernel
thread, and a large number of kernel threads may burden the performance of a system
Allows greater concurrencies but too many threads can slow the system down.
(Ex.) Linux Systems
Many-to-Many Model
The many-to-many model multiplexes many user-level threads to a smaller or equal number of kernel threads. The number of kernel threads may be specific to either a particular application or a particular machine.
It is difficult to implement even while being the most flexible.