race condition Flashcards

(20 cards)

1
Q

What is a race condition?

A

A situation where two or more threads/processes access shared data concurrently and the outcome depends on the order of execution.

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

What causes a race condition?

A

Concurrent access to shared memory without proper synchronization.

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

Why are race conditions problematic?

A

They can lead to unpredictable behavior and bugs that are hard to reproduce.

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

What is a common symptom of a race condition?

A

Inconsistent or incorrect program output.

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

Do race conditions occur in single-threaded programs?

A

No, they typically occur in multi-threaded or multi-process programs.

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

What is shared memory?

A

A memory segment that can be accessed by multiple threads or processes.

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

What synchronization methods can prevent race conditions?

A

Mutexes, semaphores, locks, atomic operations.

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

What programming constructs are used to manage race conditions?

A

Critical sections, mutual exclusion mechanisms.

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

Can read operations cause race conditions?

A

Yes, if a read is made before a write completes, it may read stale data.

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

Are race conditions deterministic?

A

No, they are non-deterministic and depend on thread scheduling.

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

What is a critical section?

A

A part of the program where shared memory is accessed and which must not be concurrently executed by more than one thread.

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

How does a mutex help prevent race conditions?

A

It ensures that only one thread can enter the critical section at a time.

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

What is the difference between a race condition and a deadlock?

A

A race condition is about incorrect data due to timing; a deadlock is when threads wait indefinitely for each other.

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

What is atomicity and how does it relate to race conditions?

A

Atomicity ensures operations are completed without interruption, preventing partial updates that cause race conditions.

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

Can compilers introduce race conditions during optimization?

A

Yes, compiler optimizations can reorder instructions in ways that cause race conditions if memory barriers aren’t used.

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

Explain how the ‘volatile’ keyword affects race conditions in C/C++.

A

It tells the compiler not to optimize reads/writes to the variable, but it does NOT prevent race conditions; synchronization is still needed.

17
Q

Describe a real-world example of a race condition in an OS context.

A

Multiple threads writing to a log file without a lock could interleave output and corrupt the log.

18
Q

How can race conditions be detected during development?

A

Using tools like ThreadSanitizer, Helgrind, or static code analysis.

19
Q

Why are race conditions difficult to debug?

A

Because they are timing-dependent and may not occur consistently.

20
Q

How do memory barriers help in preventing race conditions?

A

They prevent the CPU/compiler from reordering memory operations, ensuring proper sequence of actions.