Concurrency Flashcards

1
Q

What is concurrency in computer systems?

A

Concurrency means running multiple tasks at once to make better use of resources and speed things up

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

How does concurrency differ from parallelism?

A

Concurrency is managing multiple tasks at once (either by switching between them or running together), while parallelism is actually doing them at the same time using multiple cores

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

What is a critical section?

A

A part of code that uses shared stuff, where only one thread should run it at a time to stop mistakes

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

Define race condition.

A

When many threads change the same data at once, it can mess things up and give wrong result

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

What are the three conditions that must be met for a race condition to occur?

A

Shared resource: Multiple threads are working with the same data.

At least one write: One thread is changing the data.

No synchronization: Threads don’t work together well, so they end up clashing and causing problems

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

What is mutual exclusion?

A

One thread or process can access the critical section at a time

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

What is a mutex?

A

A tool that lets only one thread access a resource at a time, preventing conflicts

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

What are the main problems of concurrent systems?

A
  • Race conditions
  • Deadlocks
  • Starvation
  • Livelocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define deadlock.

A

Deadlock happens when threads wait on each other forever, and nothing moves forward

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

What are the four necessary conditions for deadlock?

A
  • Mutual Exclusion
  • Hold and Wait
  • No Preemption
  • Circular Wait
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a semaphore?

A

Controls how many threads can access a resource. It uses a counter to keep track

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

What’s the difference between binary and counting semaphores?

A

Binary Semaphore:
➤ Only 0 or 1
➤ Like a light switch (on/off)
➤ Used for mutual exclusion

Counting Semaphore:
➤ 0 or more
➤ Like tickets to a show – limited number
➤ Used for limiting access to a resource

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

What is starvation?

A

A thread waits forever as others keep getting the resource

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

What is a livelock?

A

Processes keep responding to each other but never finish

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

What is the difference between preemptive and non-preemptive scheduling?

A

Preemptive: The OS can stop a running program and switch to another.

Non-preemptive: A program keeps running until it finishes or waits.

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

What is Peterson’s Algorithm used for?

A

A software way for two programs to safely share a resource by taking turns

17
Q

What are the three approaches to avoid race conditions?

A

Atomic operations: Do something in one go, no interruptions.

Locks or semaphores: Use a key to control access.

Concurrent data structures: Use special tools designed for safe sharing.

18
Q

How does the lock mechanism work in thread synchronization?

A

Lets one thread in, and keeps others out until it’s done

19
Q

What is context switching?

A

Saving the CPU’s work so it can pick up right where it left off later

20
Q

What are the performance trade-offs of concurrency?

A

Pros:

Better CPU use

Faster response time

Cons:

More complex syncing

Harder to debug

Risk of deadlocks