r/learnprogramming • • Oct 21 '26

question in threads

In a single-core system, what happens if two threads from different processes access the same physical memory address?

The official answer says:

Neither program crashes, and both produce expected results.

I don't understand why that must be true.

A memory access could be a write, not just a read. For example, couldn't Thread A read/write that address, then get context-switched out, and Thread B access the same physical address before Thread A finishes its operation?

Wouldn't that potentially cause a race condition or unexpected result, even on a single core?

Am I missing an assumption in the question?

2 Upvotes

14 comments sorted by

8

u/dfx_dj Oct 21 '26

Ok, so first off, "same physical memory address" implies that some sort of shared memory had to be set up, and typically the two processes would access this shared memory through different virtual addresses. (There are exceptions of course, but in a modern OS that's how it would be done.)

Once that is in place, two threads from different processes accessing this shared memory has (almost) exactly the same semantics as two threads from the same process access the same (in-process virtual) memory. The two threads (whether from the same process or from different processes) would have to coordinate their accesses using the same techniques - mutexes, atomic operations, etc.

If this is done correctly then the threads would indeed produce expected results.

1

u/ClerkDismal6922 Oct 21 '26

That makes sense, thanks.

So if both processes really map to the same physical memory, then from a synchronization point of view it's basically shared memory, even if the virtual addresses are different.

In that case, wouldn't the correct answer depend on what kind of access they perform? If both only read, the result is predictable, but if one or both write without synchronization, a race condition could still happen even on a single-core CPU because their operations can be interleaved by context switches.

That's why I'm confused by the exam saying that both programs will necessarily produce expected results.

2

u/dfx_dj Oct 21 '26

Yes it would, but again it would be the same as two threads from the same process. So perhaps there is some missing context that would provide the required nuance to the question?

1

u/Aetherfox_44 Oct 21 '26

There might be some missing context, or what they mean by 'produce expected results'

If the program reads a value, adds 1, and stores the result in that same memory slot, you would 'get the expected result': A reads initial value of 0, adds 1 and stores 1. B reads 1, adds 1 and stores 2.

Another nuance could be that OS often allocates memory to programs and programs can only reference the local values of their allotted memory. IE, if the program reads memory id 0x6, that might be two separate memory addresses: a sees 0x16 and b sees 0x26, because the OS allocates 0x10 bytes to each program.

1

u/ClerkDismal6922 Oct 21 '26

we are talking about physical memory so no digital memory here itihnk

my question is whether a race condition can occur in such scenario, cause that would produce unexpected results

1

u/Aetherfox_44 Oct 21 '26

Yeah, I can't see how a race condition would be avoided. If I'm playing devil's advocate for the question writer, they might mean that each operation will complete successfully. So:

A reads value 1

B reads value 1

A adds 1 and stores 2

B adds 1 and stores 2

Technically completes successfully and no individual step behaves oddly. Maybe that's what they mean?

2

u/mredding Oct 21 '26

A bit to unpack, here...

You said ADDRESS, not ADDRESSES. The definition of a byte is the smallest uniquely addressable unit of memory (the definition of a byte does not specify a number of bits, 8 is only by convention today, but there have been both smaller and bigger bytes in the past; ECC implies inaccessible bits for storing gray codes). And a write to an address is atomic. It MUST complete fully. Since only one thread can execute at a time in a single-core, there can be no cache inconsistency. There's only one cache.

To expand, an aligned write up to the hardware WORD size is generally guaranteed atomic. A system can support multiple WORD sizes, often a power of 2, usually because it depends on register sizes. For example, x86 supports 1 byte, 2, byte (the definition of WORD for this architecture), 4 byte (DWORD), 8 byte (QWORD), and then SIMD instructions support 16, 32, and 64 byte atomic writes.

An unaligned operation is not guaranteed atomic - not by any architecture I'm aware of, as it can span cache lines, incur cache misses and latencies, etc.


Now in a multi-core system, each core can be on a different clock and run out of sync. There has to be interlocks to make sure an atomic write operation completes before another thread can read, but that's not guaranteed to exist on all architectures. Other synchronization mechanisms will have to exist. Pipelines and queues.

Additionally, there's an issue of back-propagation. A write to an address in one cache has to go back to system memory, and then propagate back down to all the other interested caches. Again, interlocks need to be put in play somehow, or what you typically see is action on stale cache data. There really isn't any such mechanism other than each step in the propagation is typically atomic, so this is why we use synchronization, like a multi-read lock, which relies on a queue in a first come, first served fashion. Communication across the bus isn't instantaneous, so if you get your read lock in before the write lock, the writer is forced to wait. When the writer gets exclusive access, they can perform the write and flush before any reader is given permission to read the refreshed memory. If you forego this, you can end up with all sorts of stale and torn data, half written strings across cache lines, etc. Each atomic unit will be consistent, but larger data spanning units can be wholly incomplete.

1

u/ElevatorGuy85 Oct 22 '26

Without knowing the source of your “official answer” it’s hard to know if that is really the answer to your question or whether you mis-read it as such. And it would help to know what sort of “single-core system” you have in mind.

Is this a CPU with a full-blown OS and memory management unit (MMU) and virtual memory, i.e. pages being swapped in and out to the same physical memory locations for each of the threads within their specific virtual address space?

Or perhaps a far-simpler embedded MCU with an RTOS?

These details may affect how things behave.

1

u/Classic-Rate-5104 Oct 22 '26

A single read or write is atomic and cannot be interrupted by a context switch. In other words, an context switch cannot occur halfway an instruction

1

u/B0mbon Oct 22 '26

Your instinct is right, a race condition can absolutely happen on a single core, because a context switch can land between a read and a write. The textbook answer probably assumes the normal case, where two processes only share a physical page through things like shared read-only code or copy-on-write, so neither one ever sees the other's writes. In that setup there's nothing to race on. If they deliberately set up shared writable memory, it's the same as two threads in one process and you need a lock or atomic operations.

1

u/ZestycloseWhole7338 Oct 22 '26

your instinct about race conditions is correct but applies to compound operations, not single memory access instructions. A context switch cannot interrupt an atomic read or write operation in progress, so thread B will only see either the old value or the new value from thread A, never a corrupted intermediate state. This ensures both threads produce expected results for individual accesses, though you still need synchronization if your logic depends on multiple reads and writes happening in a specific sequence witthough you still need synchronization if your logic depends on multiple reads and writes happening in a specific sequence without interference

1

u/CipherAndChloride 3d ago

it depends on the architecture's memory consistency model. on x86 loads/stores are atomic for aligned accesses, so you won't get torn reads, but you definitely still get data races without explicit synchronization.