Lecture 4 - Multi-Threading Flashcards

1
Q

Do most modern applications use multi-threading?

A

Yes.

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

What’s the purpose of multiple threads within an application?

A

Multiple tasks can operate at the same time. (Like chrome browser)

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

Why use threads rather than create a new process?

A

Process creating is heavy weight.
Thread creation is super light weight.
Simplify code, increase efficiency.

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

What’s one advantage of processes over threads?

A

Processes are more isolated. If they crash, they don’t impact the other processes.

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

Do threads share the same address space?

A

Yes.

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

What do threads share?

A

The code, the data, and the files.

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

What do threads not share?

A

Registers and Stacks.

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

What are 4 benefits of threads?

A
  1. Responsiveness: may allow continued execution if part of process is blocked, especially important for user interfaces.
  2. Resource Sharing: threads share resources of process, easier than shared memory or message passing.
  3. Economy: cheaper than process creation, thread switching lower overhead than context switching.
  4. Scalability: process can take advantage of multiprocessor architectures
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What the challenge that multiprocessor systems put on programmers?

A
Dividing activities
Balance
Data slitting
Data dependency
Testing and debugging
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define Parallelism.

A

Implies that a system can perform more than one task simultaneously.

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

Define Concurrency.

A

Creating the illusion of parallelism with a single processor.

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

Compare data parallelism vs task parallelism.

A

Data parallelism – distributes subsets of the same data across multiple cores, same operation on each.
Task parallelism – distributing threads across cores, each thread performing unique operation

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

Define Amdahl’s Law

A

Identifies performance gains from adding additional cores to an application that has both serial and parallel portions
speedup <= 1/ (S+ (1-S)/N)

Resulting improvement from an enhancement is “limited” by the fraction of the task that can be improved.

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

Explain what the Serial part and Parallel part of a process is.

A

Serial part = Can’t be threaded

Parallel = Can

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

What’s the difference between User and Kernel threads?

A

User threads - management done by user-level threads library (GNU Pth), they are super fast.
Kernel threads - Supported by the Kernel (POSIX PThreads)

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

Describe the Many-to-One multithreading model

A

Many user-level threads mapped to single kernel thread

One thread blocking causes all to block

Multiple threads may not run in parallel on multicore system because only one may be in kernel at a time

Used by few systems (Solaris Green, GNU Portable)

17
Q

What’s the best multi-threading model?

A

Many-to-Many

18
Q

Whats a Pthread? Is it user-level or kernel-level?

A

A Posix standard specification for threads (not implementation)
Can be both user and kernel-level.

19
Q

4 ways a thread terminates

A

A thread terminates for the following:

  • The start() function performs a return
  • Thread calls a pthread_exit() function
  • Thread is cancelled using pthread_cancel()
  • Any thread calls exit() or main thread returns
20
Q

How can you check if two threads are the same?

A

Compare their IDs with pthread_equal(pthread_t t1, pthread_t t2);

21
Q

How do we join a terminated thread?

A

pthread_join()

22
Q

Why do we need to use pthread_mutex_lock/unlock?

A

To protect shared variables. You have to use pthread_mutex_lock to lock the variables before accessing it to have exclusive variables. This enforces the other threads to wait for access. Otherwise the variables can become corrupt.

23
Q

What’s the difference between asynchronous cancellation and deferred cancellation for threads?

A
  • Asynchronous cancellation: terminates the target thread immediately
  • Deferred cancellation: allows the target thread to periodically check if it should be cancelled at cancellation points. (Best practice)
24
Q

4 Differences between Processes and Threads

A

Processes

  • > Create a new address space at creation
  • > Allocate resources at creation
  • > Need IPC to share data
  • > Deeper isolation for security and fault tolerance

Threads

  • > Same address space
  • > Quicker creation times –actual times depend on kernel versus user threads
  • > Sharing through shared memory
  • > Fault sharing between all threads within a process
25
Q

A application is 75% parallel and 25% serial. What’s the speedup that can be achieved by going from 1 to 2 cores?

A

1.6

26
Q

Describe a One-to-One multithreaded relationship

A
  • > Each user-level thread maps to kernel thread
  • > Creating a user-level thread creates a kernel thread
  • > More concurrency than many-to-one
  • > Number of threads per process sometimes restricted due to overhead
  • > Examples: Windows, Linux, Solaris 9 and later
27
Q

Describe a Many-To-Many multithreading model

A
  • > Allows many user level threads to be mapped to many kernel threads
  • > Allows the operating system to create a sufficient number of kernel threads
28
Q

Define Thread library

+ 2 primary implementation methods

A

Provides programmer with API for creating and managing threads
Two primary ways of implementing
1. Library entirely in user space
2. Kernel-level library supported by the OS

29
Q

Does each thread have a specific stack?

A

Yes. This is the threads local storage.

30
Q

How do you create a pthread? Where does the thread continue? What about main?

A

int pthread_create(pthread_t *thread, const pthread_attr_t *atr, void (start) (void *), void *arg);

The thread continues at start()

The main continues after the statement.

31
Q

How can a thread optain its own id?

A

using pthread_self()

32
Q

What happens if you don’t join a thread that hasn’t been detached?

A

a “zombie” thread will be created.

33
Q

How do we detach a thread?
Why would we want to do this?
Is it possible to join a detached thread?

A

int pthread_detach(pthread_t thread);

Default –a thread is joinable –another thread is going to retrieve the return state

If no thread is interested in joining we need to detach the thread

No, it is not possible to join to a detached thread

34
Q

Define Fork-Join Parallelism in threads

A

Multiple threads (tasks) are forked, and then joined.

35
Q

What’s the purpose of Thread Attributes?

A

Attributes can be used to set properties of threads –such as detached

36
Q

Define Thread Cancellation

+ 2 general approaches

A

Terminating a thread before it has finished with
pthread_cancel(tid)

Asynchronous cancellation
-> terminates the target thread immediately
Deferred cancellation
-> allows the target thread to periodically check if it should be cancelled

37
Q

What are three modes threads can be in for cancellation? What’s the default type? How is thread cancellation handled in Linux?

A

Off
-> cancellation remains pending until thread enables it
Deferred (default)
-> cancellation only occurs when thread reaches cancellation point (thread_testcancel()), then cleanup handler is evoked
Asynchronous
-> thread cancelled immediately

Thread cancellation is handled through signals in Linux