Lecture 6 - Concurrency Intro Flashcards

1
Q

What is concurrency?

A

The ability of different program parts to be executed simultanously.

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

How many ways of having concurrency is there?

A

2

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

What are the 2 ways of having concurrency?

A
  • shared memory (one memory location used by different parts of the program running at the same time)
  • message passing (different memory locations, is more robust)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does shared memory concurrency require?

A

Synchronisation between parts of the prgram which are to run alongside each other.

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

What is parallelism?

A

Simply about making your program faster. There is usually no need to have threads, it just so happens that a program can be run in parallel when there are mutliple processors.

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

How is concurrency different to parallelism?

A

Concurrency is a programming paradigm, where threads are used to get asynchronous events from the enviroment (no waiting on program, other threds can continue) or structuring program as a collection of interacting agents.

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

Concurrency has?

A

different independent processes being executed

  • dealing with lot of things at once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does parallelism have?

A

simulatnous execution of related computations

  • doing lot of things at once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a process?

A

Process is a program with its own memory address space.

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

How many processes can be executed simulatneouly?

A

Many, CPU does it all the time.

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

What is a thread?

A

independent sequence of program instructions. a process can have multiple threads, sharing the same address space.

-> can be executed simultanously
-> essentially a subset of a process

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

Lifecycle of a thread:

A
  • create
  • start (possibly with arguments)
  • each is given an identifier
    -> can be used to kill / pause / interrupt
  • teminated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In shared memory how does communication between threads happen?

A

By modifying the shared memory space.

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

What are POSIX threads?

A

threading implementation in C

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

What flag has to be specified with the clang compiler to compile code with POSIX threads?

A

-lpthread

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

What is the posix thread create method?

A

pthread_create(thread , thread attr ,function , argument to function)

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

What is the posix thread join method?

A

pthread_join(thread, NULL (result))

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

What does pthread_create return when successfully creating a thread?

A

0 - refer to slide 8

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

What is pthread_Join do?
What does it return when successfully

A

Waits for a another thread to terminate before terminating.
Returns 0 when successful.

Refer to slide 9

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

What is assert?

A

Essentially a test, it will raise an exception when not met

21
Q

What is mutual exclusion?

A

A problem in concurrency. 2 threads accessing the same variables in shared memory, leading to inaccurate results.

22
Q

What does our code become if we do not control mutual exclusion?

A

our code becomes non-deterministic. The process is inconsistent

23
Q

What is a critical region?

A

part of the code that updates shared variables or data structures. Mutual exclusion means only one thread ever executes this part of code

24
Q

What is a race condition?

A

when the result of the program depends on the order in which the threads are executed

25
Q

What is a lock?
What is another name for a lock?

A

Lock is something that needs to be obtained in order to execute critical region.

AKA Mutex

26
Q

What must happen to the lock when thread is finished with a critical section?

A

It must be released

27
Q

What are the semantics of acquiring a lock?

A
  • if another thread owns the lock , the requesting thread is blocked until owning thread releases lock
  • if lock is not owned then the requesting thread is granted ownership
28
Q

What happens on released of a lock if there is blocked threads

A

The first blocked thread is automatically granted ownership

29
Q

What do producers and conumers have to do when we have a bounded buffer?

A

The producer has to wait (is blocked) when buffer is full.

The consumer has to wait (is blocked) when buffer is empty.

Consumers and producers have to both work with a a lock to ensure consistency.

When unsafe, there could be gaps left in the buffer, leaving us with an impression that it is full, when it isn’t. etc.

30
Q

Problem with having to wait for items to enter the buffer (consumer) while having the lock/

A

Since consumer is waiting for items to enter the buffer, while holding the lock, the producer is not able to add anything into the buffer. THIS IS DEADLOCK.

31
Q

What is deadlock?

A

When a thread is holding a lock, without making progress, while also blocking other threads.

32
Q

Potential solution to deadlock #1?

A

Releasing the lock when count is zero. This is via polling.

Wastes CPU time and resources.

AKA Busy Waiting

33
Q

What is a pthread_mutex_t?

A

A lock or mutex

34
Q

What is busy waiting?

A

Having a loop to continously check whether a condition has been met (keep having to unlock and lock mutexes).

35
Q

Better solution to busy waiting?

A

Condition variable

Having a variable indicating whether a condition is ready. It must be changed by the thread currently holding the lock.

Implemented with:
pthread_cond_wait(&cv, &m)
pthread_cond_signal(&cv) -> assigns ownership to one of the waiting threads

36
Q

Example of Condition Variable.

A

pthread_mutex_lock(&m);
while (!cond) {
pthread_cond_wait(&cv, &m);}

37
Q

What are condition variables analogoues to?

A

hardware interrupts

38
Q

The bounded buffer problem will have how many condition variables?

A

Two, one for add and one for remove.

39
Q

What is a monitor

A

A class that provides safe access to a shared resource.

40
Q

, Important coordination aspects:

A
  • partitioning =determining which parts of the computation have to seperately evaluated
  • placement = determining where threads should be executed e.g. allocated to least busy core
  • communication = when to communicate and what data to send
  • synchronisation = ensuring threads can cooperate without interference
41
Q

What is a semaphore?

A

-holds an integer counter which provides 2 operations:
- wait, which decrements the counter and blocks
- signal, increments the counter and wakes waiting threds

42
Q

What is a binary semaphone?

A

Semaphore holding 0 or 1.

43
Q

If we use a semaphore, then do we need locks and cvs?

A

No, all handled by a semaphore, as the locks and cvs are built in.

44
Q

What should a semaphonre structure contain?

A

mutex or lock
condition variable
current value
max value / min value

45
Q

What do we need to avoid in concurrency?

A
  • deadlock
  • livelock
  • starvation
46
Q

What is livelock?

A

Threads run, but make no progress.

47
Q

What is starvation?

A

Some threads never making any progress.

48
Q

What do we need to do when programming concurrently?

A

must correctly lock , unlock and wait for safe access (via semaphores or condition variables)