Chapter 4 Threads & Concurrency Flashcards
(87 cards)
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.