Threads Flashcards

1
Q

What is multithreading?

A

The ability of an OS to support multiple,
concurrent paths of execution within a single process.

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

What does a thread contain?

A
  1. Thread ID
  2. Process Counter
  3. Register Stack
  4. Stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do all threads in the same process share?

A
  1. Code Section
  2. Data Section - objects, open files, network connections, etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the benefits of threads?

A
  1. Responsiveness
  2. Resource sharing
  3. Cost
  4. Scalability
  5. Reduces programming complexity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Parallelism vs Concurrency

A

Parallelism - A system is parallel if it can perform more than one task at a time

Concurrency - A concurrent system supports more than one task by allowing all the tasks to make progress

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

What are the challenges to parallel programming

A
  1. Pressure placed on system designers and application
    programmers to better use multiple cores
  2. System designers must write scheduling algorithms that use multiple processing core to allow parallel execution
  3. Challenge to modify existing programs
  4. Challenge to design new programs that are
    multithreaded
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Further challenges to parallel programming

A
  1. Identifying tasks: which areas of an application can be
    divided into separate, concurrent (and ideally independent) tasks?
  2. Balance: how do we balance the tasks on the multiple cores to achieve maximum efficiency?
  3. Data splitting: how can data sets be split for processing in parallel?
  4. Data dependency: Certain tasks will rely on data from
    another. In this case, how do we synchronise the access to this data?
  5. Testing and debugging: Due to complexities of concurrent and parallel execution (multiple pathways etc.), how do we debug such an application?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the model of multithreading?

A
  1. User threading which is above the kernel
  2. Kernel threading which is managed by the OS

There are 3 types of relationships between user and kernel threading
1. Many-to-one
2. One-to-one
3. Many-to-many

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

Many-to-one Model

A

Maps many user-level threads to one
kernel thread

Efficient

But, entire process will block if a
thread makes a blocking system call

lacks parallelism on multicore
systems

Systems need ability to take advantage of multiple cores

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

One-to-one Model

A

Maps each user thread to a kernel thread

It is more concurrent as it overcomes blocking issue

Allows for parallelism

But, for each user thread, there has to be a kernel thread

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

Many-to-many

A

A software developer can create as many user threads as necessary i.e. a Thread Pool

Corresponding kernel threads can run in parallel on a multiprocessor

If a thread blocks, kernel can schedule another for execution

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

How do we use threads?

A
  1. Through a thread library:
    • POSIX Pthreads (user/kernel level)
    • Windows (kernel)
    • Java Threads (on top of Windows/Pthreads)
  2. API for creating and managing threads
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Java Threads

A

Fundamental model of program execution in a Java program

Java API provides a rich feature set for the creation and management of threads

All programs consist of at least a single thread of control - main()

Java threads available on any system that contains a JVM

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

Which method for creating Java threads is better?

A

Implementing the Runnable interface is preferred over extending Thread class

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

What is Memory Consistency?

A

Memory consistency occurs when different threads have inconsistent views of the same data

Therefore, we need a happens-before relationship
which is to guarantee that memory writes by one statement are visible to another

To create a happens-before relationship, we can synchronize

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

Synchronized Methods

A

Two invocations of a synchronized method on the
same object cannot interleave

All other threads are blocked until first thread is done

When a synchronized method exits, it automatically
establishes a happens-before relationship with any
subsequent invocation of a synchronized method on
the same object

17
Q

Locks

A

Synchronization built around the notion of intrinsic
locks

Enforce exclusive access

Establish happen-before relationships

Thread needs to acquire lock before it can do
anything

18
Q

Synchronized Statements

A

Alternatively, synchronized statements must specify
the object that provides the lock

Used where execution can be interleaved - for example, where two different variables are being updated

private Object lock = new Object(); - object created purely to act as a lock in a synchronize statement.

Use with care - ensure it is absolutely safe to all
access to fields to interleave

19
Q

Kernel vs User Thread

A

Kernel Threads are managed by the operating system.

User threads are managed by the user, they are above the operating system and do not directly use the CPU as Kernel Threads do. User Threads make use of Kernel Threads.