Chapter 4: Threads Flashcards

(148 cards)

1
Q

What is a thread, in the context of operating systems?

A

A thread is a basic unit of CPU utilization within a process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the four components that comprise a thread?

A

A thread ID, a program counter (PC), a set of registers, and a stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What three primary sections of a process do its threads share by default?

A

The code (text) section, the data section, and the heap section.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Besides memory sections, what other operating system resources do threads of the same process share?

A

Other OS resources, such as open files and signals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the key difference between a single-threaded process and a multithreaded process in terms of components?

A

A multithreaded process has multiple sets of registers, stacks, and program counters, one for each thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the ‘Responsiveness’ benefit of multithreading?

A

It may allow an interactive application to continue running even if part of it is blocked or performing a lengthy operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do threads within a process share resources by default?

A

Threads share the memory and resources of the process to which they belong automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In contrast to threads, what techniques must processes use to share resources?

A

Processes must use techniques explicitly arranged by the programmer, such as shared memory and message passing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the ‘Cost’ benefit of using threads compared to processes?

A

It is more economical to create and context-switch threads because they share resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does thread creation compare to process creation in terms of time and memory?

A

In general, thread creation consumes less time and memory than process creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does context switching between threads compare to context switching between processes?

A

Context switching is typically faster between threads than between processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the ‘Speed’ benefit of multithreading, especially on a multiprocessor architecture?

A

Threads may run in parallel on different processing cores, increasing performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many processors can a single-threaded process utilize, regardless of how many are available?

A

A single-threaded process can run on only one processor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A recent trend in system design is to place multiple computing cores on a single processing chip, which leads to _____ systems.

A

multicore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does multithreaded programming relate to the efficient use of multi-core systems?

A

It provides a mechanism for more efficient use of these systems by allowing parallel execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

On a system with a single computing core, what does concurrency mean for the execution of threads?

A

It means the execution of the threads will be interleaved over time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

On a system with multiple cores, what does concurrency mean for the execution of threads?

A

It means that some threads can run in parallel, with the system assigning separate threads to each core.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does it mean for a system to be ‘concurrent’?

A

A concurrent system supports more than one task by allowing all tasks to make progress.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does it mean for a system to be ‘parallel’?

A

A parallel system can perform more than one task simultaneously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the relationship between parallelism and concurrency?

A

Parallelism is a special case of concurrency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Are all parallel systems concurrent? Why or why not?

A

Yes, because by performing tasks simultaneously, they allow multiple tasks to make progress.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Are all concurrent systems parallel? Why or why not?

A

No, as some may achieve concurrency by switching between tasks without executing them at the same time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How did single-core computer systems provide an ‘illusion of parallelism’?

A

CPU schedulers would rapidly switch between processes, allowing each process to make progress concurrently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the ‘Identifying tasks’ challenge in multicore programming?

A

It involves examining applications to find areas that can be divided into separate, concurrent tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the 'Balance' challenge in multicore programming?
Programmers must ensure that each parallel task involves a similar amount of work.
26
What is the 'Data splitting' challenge in multicore programming?
The data accessed by the tasks must be divided to be used by separate cores.
27
What is the 'Data dependency' challenge in multicore programming?
Programmers must ensure that the execution of tasks is synchronized to accommodate dependencies on data from other tasks.
28
What is the 'Testing and debugging' challenge in multicore programming?
Testing and debugging multithreaded programs is inherently more difficult than for single-threaded programs.
29
Threads supported above the kernel and managed without kernel support are called _____.
user threads
30
Threads supported and managed directly by the operating system are called _____.
kernel threads
31
What are the three common models for establishing a relationship between user threads and kernel threads?
The many-to-one model, the one-to-one model, and the many-to-many model.
32
Which multithreading model maps multiple user-level threads to a single kernel thread?
The many-to-one model.
33
In the many-to-one model, where is thread management performed?
Thread management is done by the thread library in user space.
34
What is a major drawback of the many-to-one model if one thread makes a blocking system call?
The entire process will be blocked.
35
Why can't multiple threads run in parallel on multicore systems using the many-to-one model?
Because only one thread can access the kernel at a time.
36
Which multithreading model maps each user thread to its own kernel thread?
The one-to-one model.
37
What is an advantage of the one-to-one model regarding blocking system calls?
It allows another thread to run when one thread makes a blocking system call.
38
What is an advantage of the one-to-one model for multicore systems?
It allows multiple threads to run in parallel on multicore systems.
39
What is the main drawback of the one-to-one model?
Creating a user thread requires creating a corresponding kernel thread, and a large number of kernel threads may worsen system performance.
40
Which two major operating system families implement the one-to-one model?
Linux and the family of Windows operating systems.
41
Which multithreading model multiplexes multiple user-level threads to a smaller or equal number of kernel threads?
The many-to-many model.
42
In the many-to-many model, what can the number of kernel threads depend on?
It may depend on either a particular application or a particular machine (e.g., number of cores).
43
How does the many-to-many model handle blocking system calls?
When a thread performs a blocking system call, the kernel can schedule another thread for execution.
44
A key feature of the many-to-many model is that developers can create as many _____ as necessary, and the corresponding kernel threads can run in parallel.
user threads
45
Despite its flexibility, what is a primary practical challenge with the many-to-many model?
It is difficult to implement in practice.
46
Why has limiting the number of kernel threads become less important in modern systems?
Because of the increasing number of processing cores appearing on most systems.
47
Due to implementation difficulty and the rise of multicore processors, which multithreading model is now used by most operating systems?
The one-to-one model.
48
What is the purpose of a thread library?
It provides the programmer with an API for creating and managing threads.
49
What are the two primary ways of implementing a thread library?
As a user-level library or a kernel-level library.
50
A thread library implemented entirely in user space with no kernel support is called a _____.
user-level library
51
What happens when a function is invoked in a user-level thread library?
It results in a local function call in user space and not a system call.
52
A thread library supported directly by the operating system is called a _____.
kernel-level library
53
What typically happens when a function is invoked in a kernel-level thread library's API?
It typically results in a system call to the kernel.
54
What are the three mainstream thread libraries in use today?
POSIX Pthreads, Windows, and Java.
55
How is the Pthreads library implemented on Linux systems?
It is implemented as a kernel-level library.
56
How is the Windows thread library implemented?
It is a kernel-level library available on Windows systems.
57
How is the Java thread API generally implemented on a host operating system?
It is implemented using a thread library available on the host system, like Pthreads or the Windows library.
58
On a Windows system, Java threads are typically implemented using which library?
The Windows thread library.
59
On UNIX, Linux, and macOS systems, Java threads are typically implemented using which library?
Pthreads.
60
What is the general strategy for creating multiple threads known as 'asynchronous threading'?
The parent creates a child thread and resumes its execution, so that the parent and child execute concurrently and independently.
61
What is a typical characteristic of data sharing in asynchronous threading?
Because the threads are independent, there is typically little data sharing between them.
62
What is a common use case for asynchronous threading?
It is commonly used for designing responsive user interfaces.
63
What is the general strategy for creating multiple threads known as 'synchronous threading'?
The parent thread creates one or more children and then must wait for all of its children to terminate before it resumes.
64
In synchronous threading, the parent cannot continue until the work performed by its _____ has been completed.
children (threads)
65
What is a typical characteristic of data sharing in synchronous threading?
It typically involves significant data sharing among threads.
66
FREE CARD Doing
67
What does Pthreads refer to?
It refers to the API for thread creation and synchronization defined in the POSIX standard (IEEE 1003.1c).
68
Is Pthreads an implementation or a specification?
Pthreads is a specification for thread behavior, not a direct implementation.
69
Who is responsible for implementing the Pthreads specification?
Operating-system designers may implement the specification in any manner they prefer.
70
What types of systems typically implement the Pthreads specification?
Most are UNIX-type systems, including Linux and macOS.
71
The mathematical function $sum = \sum_{i=1}^{N} i$ calculates the summation of a series of what kind of integers?
It calculates the summation of a series of non-negative integers.
72
In the provided Pthreads example, how is the upper bound of the summation entered into the program?
It is entered via the command line when the program is executed.
73
In a Pthreads-based program, what is each thread associated with?
Each thread is associated with a specific function.
74
When a Pthreads program begins, which function does the initial thread start executing?
The initial thread is associated with the `main()` function and starts executing there.
75
In the example program, the thread corresponding to `main()` creates a second thread associated with which function?
The second thread is associated with the function `runner()`.
76
In the Pthreads summation example, what global data is shared between the `main` thread and the `runner` thread?
The global integer variable `sum` is shared by both threads.
77
In Pthreads, what data type is used for the thread identifier?
The data type `pthread_t` is used for the thread identifier.
78
Which Pthreads function is used to create a new thread?
The `pthread_create()` function is used to create a new thread.
79
Which Pthreads function is used to make the calling thread wait for another thread to terminate?
The `pthread_join()` function is used to wait for another thread to exit.
80
What is the effect of calling the `pthread_join()` function?
It suspends the execution of the calling thread until the target thread terminates.
81
In the command line, `argv[0]` typically contains the program name, while `argv[1]` contains the _____.
first command-line argument
82
In the context of Pthreads, data declared globally is shared by all _____ belonging to the same process.
threads
83
Which C standard library function converts a string to an integer, as used in `atoi(param)`?
The `atoi()` function converts a string to an integer.
84
How does a thread created with Pthreads typically terminate its own execution?
It calls the function `pthread_exit()`.
85
What command-line option is required to compile a C program involving Pthreads on Linux?
The option `-lpthread` is required for linking the Pthreads library.
86
If a C program using Pthreads is named `MyProgram.c`, what is the full `gcc` compilation command?
`gcc MyProgram.c -lpthread`
87
What does the 'l' in the `-lpthread` compilation flag stand for?
The 'l' is a lowercase L and it stands for 'link'.
88
What is `pid_t` the data type for?
`pid_t` is the data type for process IDs.
89
In the GNU C library, how is the `pid_t` data type implemented?
It is implemented as an `int`.
90
What is `pthread_t` the data type for?
`pthread_t` is the data type for thread IDs.
91
Why shouldn't you assume the underlying type of `pthread_t` is an integer for portable code?
Because it is an implementation-dependent type, and some implementations may use a structure to represent it.
92
How can you create multiple threads in a Pthreads program?
You can use the `pthread_create()` function multiple times.
93
How can you wait for multiple threads to terminate in a Pthreads program?
You can use the `pthread_join()` function multiple times, typically in a loop.
94
What happens if you call `pthread_join()` on a thread that has already terminated?
The `pthread_join()` function returns immediately.
95
What value does `pthread_create()` return on success?
On success, `pthread_create()` returns 0.
96
What does `pthread_create()` return if an error occurs?
On error, it returns an error number.
97
What is Hyperthreading?
It is Intel's simultaneous multithreading (SMT) implementation used to improve parallelization on x86 microprocessors.
98
With hyperthreading, how does one physical core appear to the operating system?
One physical core appears as two virtual cores.
99
What does hyperthreading allow in terms of thread scheduling?
It allows the concurrent scheduling of two threads per physical core.
100
Theoretically, what is the ideal total number of threads a program should create on a standard multicore system?
The total number of threads should be equal to the number of cores of the system.
101
On a system that supports hyperthreading, what is the theoretical ideal number of threads a program should create?
The number of threads should be equal to twice the number of cores.
102
What is a practical way to find the best number of threads for a specific program?
You can try different numbers and measure performance to find the best one.
103
What is the goal of the strategy known as implicit threading?
To transfer the creation and management of threading from application developers to compilers and run-time libraries.
104
Implicit threading is a strategy used to address the difficulties in designing _____ programs.
multithreaded
105
In implicit threading, what must application developers identify instead of threads?
Developers must identify tasks that can run in parallel.
106
A task that can be run in parallel is usually written as a _____.
function
107
In implicit threading, what component maps a task to a separate thread?
The run-time library maps the task to a separate thread.
108
What is the primary advantage for developers when using an implicit threading approach?
Developers only need to identify parallel tasks, while the libraries handle the details of thread creation and management.
109
What is the general idea behind a thread pool?
To create a number of threads at start-up and place them in a pool where they wait for work.
110
When using a thread pool, what happens when the OS receives a computation request?
Instead of creating a new thread, it submits the request to the thread pool.
111
In a thread pool, what happens if a request is submitted and there is an available thread?
The available thread is awakened, and the request is serviced immediately.
112
In a thread pool, what happens if a request is submitted but no threads are available?
The task is queued until a thread becomes free.
113
After a thread from a thread pool completes its service, what does it do?
It returns to the pool and awaits more work.
114
Thread pools work particularly well when the submitted tasks can be executed in what manner?
Asynchronously.
115
What is a benefit of thread pools related to the speed of servicing a request?
Servicing a request with an existing thread is often faster than waiting to create a new thread.
116
How do thread pools benefit systems that cannot support a large number of concurrent threads?
A thread pool limits the number of threads that exist at any one time.
117
What responsibility is transferred from developers to libraries when using a thread pool?
Thread creation and management.
118
List one factor that can be used to heuristically set the number of threads in a thread pool.
The number of CPUs in the system.
119
List a second factor that can be used to heuristically set the number of threads in a thread pool.
The amount of physical memory.
120
List a third factor that can be used to heuristically set the number of threads in a thread pool.
The expected number of concurrent client requests.
121
More sophisticated thread-pool architectures can _____ adjust the number of threads in the pool according to usage patterns.
dynamically
122
What is a benefit of a thread pool that can dynamically adjust its size?
It can have a smaller pool, consuming less memory, when the system load is low.
123
What is an example of a sophisticated thread-pool architecture that can dynamically adjust its size?
Apple's Grand Central Dispatch.
124
What does OpenMP stand for?
Open Multi-Processing.
125
OpenMP is an API for programs written in which three languages?
C, C++, or FORTRAN.
126
OpenMP provides support for parallel programming in _____ -memory environments.
shared
127
How does a developer use OpenMP to make code run in parallel?
The developer inserts compiler directives into the code to identify parallel regions.
128
In OpenMP, _____ are identified as blocks of code that may run in parallel.
parallel regions
129
What is the OpenMP compiler directive used to fork additional threads and define a parallel region?
`#pragma omp parallel`
130
When using `#pragma omp parallel`, how many threads does OpenMP typically create by default?
OpenMP creates as many threads as there are processing cores in the system.
131
On a dual-core system, how many threads would `#pragma omp parallel` create?
Two threads would be created.
132
On a quad-core system, how many threads would `#pragma omp parallel` create?
Four threads would be created.
133
What OpenMP directive is used to parallelize `for` loops?
`#pragma omp parallel for`
134
In the summation example `sum = \sum_{i=1}^{N} i`, what is the value of `sum` if N=5?
The value of sum is 15 (1+2+3+4+5).
135
What is the purpose of the line `int sum; /* this data is shared by the thread(s) */` in the Pthreads example?
It declares a global integer variable named `sum` that will be accessible and modifiable by all threads.
136
In the Pthreads `runner` function, what is the purpose of the line `int i, upper = atoi(param);`?
It declares a loop counter `i` and initializes the `upper` bound by converting the input parameter from a string to an integer.
137
In the Pthreads `runner` function, what is the purpose of the line `sum = 0;`?
It initializes the shared global variable `sum` to zero before starting the summation calculation.
138
What calculation is performed by the loop `for (i = 1; i <= upper; i++) sum += i;`?
It calculates the summation of integers from 1 up to the `upper` bound, storing the result in the shared `sum` variable.
139
In the C code `pthread_create(&tid, NULL, runner, argv[1]);`, what does `argv[1]` represent?
It represents the argument being passed to the new thread's start function (`runner`).
140
What does the `NULL` argument in `pthread_join(tid, NULL);` signify?
It signifies that the main thread is not interested in receiving a return value from the thread it is joining.
141
In the OpenMP example, code outside the `#pragma omp parallel` block is referred to as _____ code.
sequential
142
In the OpenMP example `#pragma omp parallel for`, how is the work of the `for` loop distributed?
The iterations of the loop are divided among the available threads to be executed in parallel.
143
The strategy where developers identify parallel tasks and libraries manage the threads is called _____.
implicit threading
144
In the second Pthreads example, what is the purpose of the variables `ret1` and `ret2`?
They store the return values from the `pthread_create` function calls to check for success (0) or an error.
145
In the second Pthreads example, what does the line `message = (char *) ptr;` do?
It casts the generic `void *` pointer parameter back to its original `char *` (string) type.
146
The code `pthread_join(thread1, NULL); pthread_join(thread2, NULL);` ensures the main program waits for what?
It ensures the main program waits until both `thread1` and `thread2` have completed their execution.
147
Which header file must be included to use Pthreads functions like `pthread_create`?
The `` header file must be included.
148
Which header file must be included to use OpenMP directives and library functions?
The `` header file must be included.