- What are resources? What kinds of resources are there?
- How are these resources managed? Who is managing the resources?
- When do we need to manage the resources?
- What are the potential issues in the context of resource management?
<br/>
<v-click>
### Possible Answers
- Hardware (physical), or any piece of information (virtual).
- Concurrency vs Parallelism
- limited/unlimited
- Concurrent programming vs parallel programming
- preemptable/nonpreemptable
- Programming with shared memory vs message passing
- Allocation/acquisition <-> Deallocation/release, by programmers or by the OS
- When resources are limited
- Priories, Performance, Fairness, Deadlocks
</v-click>
---
---
title: Recap II
title: Recap II
layout: center
---
---
### Deadlocks
# How to Design Parallel Programs/Applications?
- What are deadlocks?
- What are the conditions for resource deadlocks?
- How do we handle deadlocks? What are the differences between those approaches?
<br/>
<v-click>
<v-click>
### Possible Answers
Using Foster's Design Methodology.
- Permanent blocking of a set of processes that compete for system resources
- Conditions for deadlocks (1-3: Possibility, 1-4: Existence)
- 1. Mutual exclusion on resources
- 2. Hold-and-wait
- 3. No preemption
- 4. Circular wait
- Prevention, Avoidance, Detection, Resolution (Differences as the names suggest)
</v-click>
</v-click>
---
title:Recap III
---
### Deadlock Avoidance - The Banker's Algorithm
**Idee**: Do not grant a resource request if this allocation might lead to deadlock (unsafe situation).
### Exercises
There are 5 threads and 4 types of resources in the system. $G$ represents the total requests for resources. $B$ represents the current allocation of resources. $v$ represents the total amount of resource in the system.
$$
G = \begin{pmatrix}
2 & 1 & 0 & 0 \\
5 & 4 & 2 & 1 \\
6 & 6 & 3 & 4 \\
5 & 3 & 1 & 3 \\
5 & 6 & 2 & 3
\end{pmatrix}
B = \begin{pmatrix}
2 & 0 & 0 & 0 \\
3 & 3 & 2 & 1 \\
1 & 1 & 1 & 1 \\
1 & 0 & 0 & 1 \\
1 & 2 & 0 & 2
\end{pmatrix}
v = \begin{pmatrix} 9 & 6 & 3 & 5 \end{pmatrix}
$$
- Use the Banker's algorithm to decide if the situation is safe. Write down every intermediate steps.
- Change the vector $v$ in such way that a safe situation becomes unsafe, vice versa. Write down every intermediate steps as well.
---
title: Recap III
layout: center
---
# Discussion: Restrictions of Deadlock Avoidance
<br/>
<v-clicks>
<v-clicks>
-Knowledge about number of threads and their maximum resource requirements in advance
-**Partitioning**: The process of dividing the computation and data into pieces.
-Fixed number of resources
-**Communication**: The process of determining how tasks will communicate with each other, distinguishing between local communication and global communication.
-No process may exit while holding resources.
-**Agglomeration**: The process of grouping tasks into larger tasks to improve performance or simplify programming.
-Not applicable to dynamic systems
-**Mapping**: The process of assigning tasks to physical processor.
</v-clicks>
</v-clicks>
---
---
title: Recap IV
title: Recap III
layout: center
---
---
# Dining Philosophers Problem
# OpenMP
What are the potential issues here if we want to solve this problem?
An API for Writing Multithreaded Applications.
<v-clicks>
- A set of compiler directives and library routines
- Greatly simplifies writing multi-threaded programs in C/C++, Fortran
- Deadlock
- Standardized
- Fairness
- Performance
</v-clicks>
---
title: Recap V
layout: center
---
# Discussion: Mutex, Semaphore and Monitor
<br/>
- What are the differences?
### Assumptions
- What do they have in common?
---
GNU GCC or Clang is already available on your machine.
title: Recap V
layout: center
---
# Programming Exercises
Details about OpenMP support in compilers can be found with the following links:
1. Fix the deadlock bug in `exercises/deadlock`
-[GNU](https://gcc.gnu.org/projects/gomp/)
2. Implement semaphore using conditional variables in `exercises/semaphore`