Final Study Flashcards
What is a thread?
In computer science, a thread of execution is the smallest sequence of programmed instructions that a scheduler can manage independently, which is typically a part of the operating system.
What is a “heavy-weight process?”
A normal process under an Operating System (OS) is a “heavy-weight process.” The OS provides an independent address space for each such process to keep different users and services separated. Switching from one such process to another is time- consuming, and this task is performed by the Memory Management Unit (MMU).
Why do we call a thread a “light-weight process (LWP)?”
A thread is called a Light-Weight Process (LWP) because it runs under the address space of a regular (heavy-weight) process, and LWPs under the same process may share, e.g., variables. Switching from one LWP to another is much faster than switching from one heavy-weight process to another because there is less to manage, and the MMU is not involved.
What is the difference between a “Thread” and a “Process”?
Threads within the same process run in shared memory space, while processes run in separate memory spaces. Processes are independent of one another, and they don’t share their codes, data, and OS resources, like processes. As a result, threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like a process, a thread has its program counter (PC), register set, and stack space.
Are there situations that we use “multithreading?”
Multithreading has many advantages, but in the following two cases, multithreading is preferable over a single thread process: A- Processing power: If you have a multi-core computer system, multithreading is preferable. B-Multithreading avoids priority inversion where a low priority activity such as accessing the disk blocks a high priority activity, such as user interface to respond to a request.
What is an example where having a single thread is preferred over multithreading?
If we are waiting for a user response or we are waiting for data to arrive over the network, it is useless to assign several threads waiting for the same thing.
How would a web server act under a multithreading system?
The server listens for a new client to ask for a transaction. Then the server would assign a thread to the requesting client and starts listening for the next client.
What is the difference between running four threads on a single-core processor and running the same number of threads on a double-core processor?
On a single-core processor, all of the threads take a turn in a round-robin fashion. This is known as “concurrency.” On a double core processor, two threads run on one core, and the other two would run on the second core. This parallel running of threads on multiple cores is known as “parallelism.”
What are the four benefits of multithreading?
A- Responsiveness: If a process is divided among multiple threads, then if one part of the process is blocked, the other parts could go on. B- Resource sharing: different threads of a process can share the code and memory of that process. C- Economy: Starting a new thread is much easier and faster than creating a new process. D- Scalability: A multithreaded process runs faster if we transfer it to a hardware platform with more processors.
What are the challenges that programmers face when they design the code for multiprocessors?
A- Dividing activities: finding areas that can be divided into separate and concurrent tasks. B- Balance: programmers must ensure that different tasks are of the same value in terms of complexity and execution time. C- Data splitting: Data should be split, in a balanced manner, among already split concurrent tasks. D- Data dependency: The programmer should make sure that different tasks that are running concurrently do not have data dependence. E- Testing and debugging: Many different execution paths are possible and more complicated than testing single-threaded applications.
What are the two types of parallelism?
A- Data parallelism: Data is divided into subsets, and each subset is sent to different threads. Each thread performs the same operations. B- Task parallelism: The whole data is sent to different threads, and each thread does a separate operation.
How do we compute “speedup” using Amdahl’s law?
Speedup = 1/(S+((1-S)/N)). In this formula, S is the portion of the task that has to be performed serially, and (1-S) is the part of the task that can be distributed on N processors. Speedup indicates how much faster the task is running on these N processors as compared to when it was running serially.
Suppose that 50% of a task can be divided equally among ten threads and each thread will run on a different core. A) What will be the speedup of this multithreading system as compared to running the whole task as a single thread? B) What will be the speedup of part (A) if we could send 90% of the job to ten threads?
What is the upper bound in Amdahl’s law?
upper bound means that no matter how much you increase the number of threads (N), the speedup would not go beyond Speedup = 1/N. For example, if the serial part of a code is 1% at most, the speedup would be at most 1/0.01 or 100, no matter how many processors you use. Hence, if the serial part is 1%, the upper bound of speedup for such a code is 100.
n the context of “Amdahl’s law,” what is the meaning of the “diminishing returns?”
The upper bound of Speedup = 1/S is still an optimistic estimation. When the number of processors and threads increases, the overhead of handling them increases too. Too much increase in the number of threads could cause a loss, and the speedup may fall below 1/S. This is known as a diminishing return, which says that sometimes a smaller number of threads could result in higher performance.
What are the three popular user-level thread libraries?
POSIX pthreads, Windows, and Java.
What is the relationship between user threads and kernel threads?
User threads run within a user process. Kernel threads are used to provide privileged services to processes (such as system calls). The kernel also uses them to keep track of what is running on the system, how much of which resources are allocated to what process, and to schedule them. Hence, we do not need to have a one-to-one relationship between user threads and kernel threads.
A) In the relationship between the user and kernel threads, what is the “many- to-one model?”
B) What is the shortcoming of this model?
A) Before the idea of threads become popular, OS kernels only knew processes. An OS would consider different processes and consider each a separate entity. Each process was assigned a working space and could produce system calls and ask for services. Threading in the user-space was not dealt with by the OS. With User mode threading, support for threads was provided by a programming library, and the thread scheduler was a subroutine in the user program itself. The operating system would see the process, and the process would schedule its threads by itself.
B) If one of the user threads needed a system call, such as a page fault, then the other threads were blocked.
What is the “one-to-one” threading model? What are its advantages and shortcomings?
Each user thread is assigned a kernel thread. Hence, we can achieve more concurrency, and threads can proceed while one thread is blocked. The disadvantage occurs when there are too many user threads, which may burden the performance of the operating system.
What is a “many-to-many” multithreading model?
The OS decides the number of kernel threads, and the user process determines the number of user threads. A process that runs on an eight-core processor would have more kernel threads than the one which runs on a quad-core processor. This model does not suffer from either of the shortcomings of the other two models.
What is pthread?
It is POSIX (portable OS interface) thread library providing programmers with an application program interface (API) for creating and managing threads.
What is synchronous threading?
After creating the threads, the parent has to wait for the children to terminate before it can resume operation.
For thread programming in C or C++ using pthread what header file should be included?
include
What does the following piece of code do?
It uses a function to perform summation. The main program sequentially calls the function.
What does the following piece of code do?
What is the difference in the behavior of the above code in question 28 and the one in question 27?
the program of question 27 uses a function for summation. It is called, and the main program has to wait for it to finish. Then the main program can call it again. The program of question 28 uses threads. The main program creates all threads, and they run concurrently. The order that threads finish their work depends on the complexity of their job.
What does the following instruction do?
pthread_create(id, attributes, function, argument);
It creates a thread with a given id and a set of attributes. The thread will run a function and can carry an argument into that function.
What does the following instruction do?
pthread_join(id, ReturnedValue);
The main process waits for the thread with the given ID to finish and return a variable. If no variable is to be brought back, then NULL is used.
Does Windows support multithreading?
Windowssupports multithreading. It supports single or multiple thread creation and single or multiple thread joining.
What does “detaching” of thread mean?
If pthread_detach() is used inside a thread, it means that as soon as the thread is exited, its resources are released, and the system should not wait for this thread to join with its parent.
What is meant by implicit threading?
It is difficult to create hundreds and thousands of threads by the program. The solution is implicit threading, which transfers the creation and management of threading from application developers to compilers and run-time libraries.
What are some famous “implicit threading” examples?
A) Thread Pools
B) Fork-Join
C) OpenMP
D) Grand Central Dispatch
E) Intel Threading Building Blocks.
How is the OpenMP application programming interface used?
The programmer determines which parts of the program could be implemented in a parallel manner. Then the OpenMP API is used and which has compiler directives and library routines. The compiled version of the program would have multiple threads to perform the code in a parallel manner.
What happens when fork() is used in a program?
It is the UNIX way of creating a new separate duplicate process. The new process consists of a copy of the address space of the original process. Both processes (the parent and the child) continue execution at the instruction after the fork(). fork() returns a code. The child process receives zero from the fork(). The parent process receives a nonzero ID of the child.
When a fork() is executed, do the threads running under the parent process get duplicated?
The threads are not duplicated unless after fork() an exec() instruction is executed.
What is a “signal” in the context of threading?
Signals notify a process of occurrence of an event. A signal can be synchronous or asynchronous. A signal is generated, delivered, and then handled.
What are two examples of synchronous and asynchronous signals?
If an instruction in a thread performs division by zero, then it generates a synchronous signal. When a signal is generated by an event external to a running process, that process receives the signal asynchronously. An example of such a signal includes terminating a process with specific keystrokes (such as ).
Is it possible to create a thread and then cancel it?
Yes. The main thread could cancel the child thread unless the thread has disabled its cancelation. The thread can disable its cancelation capability if it is doing something critical. It is a good practice to enable the cancelation capability after a while and allow the parent to cancel the thread if needed.
In the context of scheduling of the user threads, and assigning each of them to a kernel thread, what is a light-weight process (LWP)?
To the user- thread library, the LWP appears to be a virtual processor on which the application can schedule a user thread to run.
What is a cooperating process, and why is there a potential for data inconsistency?
A cooperating process can affect or be affected by other processes. They use shared memory to share data or to pass messages. The sequence of actions of cooperating processes could result in the wrong content of the shared memory. Wrong content of shared memory is referred to as data inconsistency.
Why is there a need for synchronization?
Data inconsistency requires that the cooperating processes or threads access shared variables in an orderly manner, which is called synchronization.