Lecture 07 - Need to finish the bits on semaphores Flashcards

1
Q

What are the three main ways in which a multi-threaded program can go wrong?

A
  • Deadlock
  • Race Conditions
  • Starvation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the name given to source code that leads to a race condition?

A

A critical section

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

What is a race condition?

A

When two or more threads are simultaneously reading/working on the same data which can result in data becoming inconsistent, or overwritten.

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

Name two tools used to safeguard critical sections.

A

Mutexes and semaphores

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

What is a mutex?

A

A variable which can be locked.

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

How many threads can lock a mutex at once?

A

One

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

What happens when a thread tries to lock a mutex which is already locked by another thread?

A

It blocks until the mutex becomes unlocked.

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

What type is used to store mutexes?

A

pthread_mutex_t

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

What function initialises a mutex?

A

pthread_mutex_init

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

What is the function prototype for pthread_mutex_init?

A

int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr);

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

How do you initialise a GLOBAL mutex?

A

pthread_mutex_t m = PTHREAD_MUTEX_INITIALISER;

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

What function destroys a mutex?

A

pthread_mutex_destroy

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

What is the function prototype for pthread_mutex_destroy?

A

int pthread_mutex_destroy(pthread_mutext_t* mutex);

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

What function is used to lock a mutex?

A

pthread_mutex_lock

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

What is the function prototype for pthread_mutex_lock?

A

int pthread_mutex_lock(pthread_mutex_t* mutex);

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

What function is used to unlock a mutex?

A

pthread_mutex_unlock

17
Q

What is the function prototype for pthread_mutex_unlock?

A

int pthread_mutex_unlock(pthread_mutex_t* mutex);

18
Q

What function locks a mutex only if it’s available?

A

trylock

19
Q

What does trylock return if the mutex was successfully locked?

A

0

20
Q

What does trylock return if the mutex was invalid?

A

EINVAL

21
Q

What does trylock return if the mutex was busy?

A

EBUSY

22
Q

What causes deadlock?

A

When two or more threads are waiting on each other’s resources.