0% found this document useful (0 votes)
42 views3 pages

Multithreading Concepts and Examples

This document contains questions about multithreading concepts including differences between user-level and kernel-level threads, context switching between kernel threads, resources used for thread creation, binding real-time threads, circumstances for better performance with multithreading, shared program state across threads, achieving parallelism on single-processor and multiprocessor systems, modeling processes and threads in the kernel, output from a Pthreads program, and performance implications of thread allocation scenarios. It also provides programming problems involving calculating statistics, generating the Fibonacci sequence, and outputting prime numbers using multithreading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Multithreading Concepts and Examples

This document contains questions about multithreading concepts including differences between user-level and kernel-level threads, context switching between kernel threads, resources used for thread creation, binding real-time threads, circumstances for better performance with multithreading, shared program state across threads, achieving parallelism on single-processor and multiprocessor systems, modeling processes and threads in the kernel, output from a Pthreads program, and performance implications of thread allocation scenarios. It also provides programming problems involving calculating statistics, generating the Fibonacci sequence, and outputting prime numbers using multithreading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CHAPTER 2

2.2. THREAD
1 Provide two programming examples in which multithreading provides better performance
than a single-threaded solution.

2 What are two differences between user-level threads and kernel-level threads? Under
what circumstances is one type better than the other?

3 Describe the actions taken by a kernel to context-switch between kernel- level threads.

4 What resources are used when a thread is created? How do they differ from those used
when a process is created?

5 Assume that an operating system maps user-level threads to the kernel using the many-
to-many model and that the mapping is done through LWPs. Furthermore, the system
allows developers to create real-time threads for use in real-time systems. Is it necessary
to bind a real-time thread to an LWP? Explain.

6 Provide two programming examples in which multithreading does not provide better
performance than a single-threaded solution.

7 Under what circumstances does a multithreaded solution using multi-ple kernel threads
provide better performance than a single-threadedsolution on a single-processor system?

8 Which of the following components of program state are shared across threads in a
multithreaded process?
a. Register values
b. Heap memory
c. Global variables
d. Stack memory

9 Can a multithreaded solution using multiple user-level threads achieve better


performance on a multiprocessor system than on a single-processor system?

10 In Chapter 3, we discussed Google’s Chrome browser and its practice of opening each
new website in a separate process. Would the same benefits have been achieved if instead
Chrome had been designed to open each new website in a separate thread? Explain.

11 Is it possible to have concurrency but not parallelism? Explain.

12 Using Amdahl’s Law, calculate the speedup gain of an application that has a 60 percent
parallel component for (a) two processing cores and (b) four processing cores.

13 Determine if the following problems exhibit task or data parallelism:


• The multithreaded statistical program described in Exercise 21
• The multithreaded Sudoku validator described in Project 1 in this chapter
• The multithreaded sorting program described in Project 2 in this chapter
• The multithreaded web server described in Section 1

14 A system with two dual-core processors has four processors available for scheduling. A
CPU-intensive application is running on this system.
All input is performed at program start-up, when a single file must be opened. Similarly, all
output is performed just before the program terminates, when the program results must be
written to a single file. Between startup and termination, the program is entirely CPU-
bound. Your task is to improve the performance of this application by multithreading it. The
application runs on a system that uses the one-to-one threading model (each user thread
maps to a kernel thread).
• How many threadswill you create to performthe input and output?
Explain.
• How many threads will you create for the CPU-intensive portion of the application?
Explain.

15 Consider the following code segment:


pid t pid;
pid = fork();
if (pid == 0) { /* child process */
fork();
thread create( . . .);
}
fork();
a. How many unique processes are created?
b. How many unique threads are created?

16 As described in Section 7.2, Linux does not distinguish between processes and threads.
Instead, Linux treats both in the same way, allowing a task to bemore akin to a process or a
thread depending on the set of flags passed to the clone() systemcall. However, other
operating systems, such as Windows, treat processes and threads differently.
Typically, such systems use a notation in which the data structure for a process contains
pointers to the separate threads belonging to the process. Contrast these two approaches
for modeling processes and threads within the kernel.

17 The program shown in Figure 16 uses the Pthreads [Link] be the output from
the program at LINE C and LINE P?

18 Consider a multicore system and a multithreaded program written using the many-to-
many threading model. Let the number of user-level threads in the program be greater than
the number of processing cores in the system. Discuss the performance implications of the
following scenarios.
a. The number of kernel threads allocated to the program is less than the number of
processing cores.
b. The number of kernel threads allocated to the program is equal to the number of
processing cores.
c. The number of kernel threads allocated to the program is greater than the number of
processing cores but less than the number of user-level threads.

Programming Problems
1. Write a multithreaded program that calculates various statistical values for a list of
numbers. This programwill be passed a series of numbers on the command line and will
then create three separate worker threads. One thread will determine the average of the
numbers, the second will determine the maximum value, and the third will determine the
minimum value.
For example, suppose your program is passed the integers
90 81 78 95 79 72 85
The program will report
The average value is 82
The minimum value is 72
The maximum value is 95
The variables representing the average,minimum, and maximumvalues will be stored
globally. The worker threads will set these values, and the parent thread will output the
values once the workers have exited. (We could obviously expand this program by creating
additional threads that determine other statistical values, such as median and standard
deviation.)

2. The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, ....


Formally, it can be expressed as:
fib0 = 0
fib1 = 1
fibn = fibn−1 + fibn−2
Write a multithreaded program that generates the Fibonacci sequence. This program
should work as follows: On the command line, the user will enter the number of Fibonacci
numbers that the program is to generate. The program will then create a separate thread
that will generate the Fibonacci numbers, placing the sequence in data that can be shared
by the threads (an array is probably the most convenient data structure). When the thread
finishes execution, the parent thread will output the sequence generated by the child
thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the
child thread finishes, the parent thread will have to wait for the child thread to finish. Use
the techniques described in Section 4.4 to meet this requirement

3. Write a multithreaded program that outputs prime numbers. This program should work as
follows: The user will run the program and will enter a number on the command line. The
program will then create a separate thread that outputs all the prime numbers less than or
equal to the number entered by the user.

Common questions

Powered by AI

Creating a thread involves allocating resources such as a thread stack, program counter, and thread-specific storage. These threads share the process's resources like memory space and file descriptors. In contrast, creating a process involves a fork, duplicating not only the code but also memory space and system resources, which increases overhead. This makes thread creation generally faster and less resource-intensive than process creation .

Multithreading can improve the performance of compute-bound applications on multicore systems as it allows computations to be performed in parallel across multiple cores. By creating a thread for each core, the workload can be divided, reducing execution time and increasing throughput. This model, often implemented in a one-to-one threading model where each user thread corresponds to a kernel thread, takes full advantage of multicore architectures .

The one-to-one threading model, where each user-level thread corresponds to a kernel-level thread, offers benefits for compute-intensive applications by allowing multiple threads to run in parallel across processors or cores. This model ensures efficient CPU utilization and minimal thread management overhead, as the operating system can directly schedule kernel threads on available processors without the need for user-level scheduling .

Amdahl’s Law calculates the theoretical maximum speedup of a task using parallel processing based on the proportion of the task that can be parallelized. It highlights diminishing returns as more cores are added, emphasizing that the performance gain is limited by the serial portion of the computation. Decisions regarding core numbers should consider this law; excessive cores may not significantly enhance performance past a certain point, especially if the task has a large serial component .

Binding a real-time thread to a specific processor core ensures that the thread receives consistent CPU time without interruption from other processes or threads. This binding (processor affinity) minimizes context switching delays and helps meet real-time scheduling requirements, which are crucial in applications requiring predictable performance and deadlines .

In real-time systems using the many-to-many threading model, binding a real-time thread to a Light Weight Process (LWP) ensures that the real-time thread has a corresponding kernel thread, which allows it to be scheduled according to real-time scheduling policies. This binding is crucial to guarantee the predictability and timing constraints required in real-time applications .

Multithreading can offer improvements on single-processor systems primarily through better resource allocation and responsiveness. By allowing multiple threads to manage different I/O operations or user interactions concurrently, a program can maintain responsiveness while waiting for I/O operations to complete. However, an actual performance boost for CPU-bound tasks is unlikely as tasks must eventually execute serially .

The clone() system call in Linux allows processes to be created with shared resources, effectively blurring the lines between processes and threads. Unlike systems like Windows that differentiate by having processes contain threads, Linux allows for more flexible sharing of memory and execution context. This approach can optimize resource utilization but may complicate the management of concurrency due to less explicit separation between processes and threads .

On a multiprocessor system, a multithreaded program can execute multiple threads truly in parallel, significantly reducing execution time and improving throughput compared to a single-processor system where threads must be executed concurrently but not in parallel. This parallel execution allows efficient utilization of CPU resources, reducing idle time and improving performance for compute-intensive tasks .

Multithreading might not lead to performance gains when the overhead of managing multiple threads outweighs the benefits. This can happen in scenarios where tasks have significant dependencies, require frequent synchronization, or when running on single-processor systems where context switching can introduce delays. Additionally, if the task is I/O-bound, increasing threads might lead to diminished returns due to I/O bottlenecks .

You might also like