Confusion in synchronisation

12 views
Skip to first unread message

Rahul Bhattacharya

unread,
Mar 12, 2025, 3:19:40 PMMar 12
to isi-mt...@googlegroups.com
When multiple processes use a shared variable, do the same shared variable loaded in different registers for different processes or in the same register?
Synchronisation doubt.png

Rahul Bhattacharya

unread,
Mar 12, 2025, 3:38:41 PMMar 12
to isi-mt...@googlegroups.com
One question in the sequel: When a process is just finished(terminated) and context switch yet to happen and others are yet to finish, what would be status of the register “R”?

Rahul Bhattacharya

unread,
Mar 13, 2025, 2:34:42 PMMar 13
to isi-mt...@googlegroups.com
Please, let me know where I am wrong 
Scheduling Linux(post midsem).png

Mandar Mitra

unread,
Mar 18, 2025, 12:26:54 AMMar 18
to Rahul Bhattacharya, isi-mt...@googlegroups.com
Sorry for taking so long to respond. Too many things going on..
The shared variable may be loaded in either the same or in different registers. The compiler decides which register to use depending on how many other variables are in "active" use, and how many of them are in registers.

In fact, when different parts of the code within a SINGLE function is compiled, the same variable may be loaded in different registers.

Note that the race condition (the fact that the final value of i depends on order of interleave) may occur, IRRESPECTIVE of whether R is the same register or not.

Mandar Mitra

unread,
Mar 18, 2025, 12:50:51 AMMar 18
to Rahul Bhattacharya, isi-mt...@googlegroups.com
Rahul Bhattacharya wrote (Thu, Mar 13, 2025 at 01:08:26AM +0530):
> One question in the sequel: When a process is just finished(terminated) and
> context switch yet to happen and others are yet to finish, what would be
> status of the register “R”?

Call the just-terminated process P.

R will hold whatever value was last written into it by P. If R is a commonly used register (like ebx, ecx), then that last value was probably written by the kernel while running the code for the exit system call on behalf of P. Since x86 has only 6 general purpose registers, this scenario is the most likely.

In principle, however, it is possible that P wrote some value into R when doing some computation in user mode, and the kernel did not need to use that register during exit. In this case, the last value stored in R by P (when in user mode) will still be stored in R.

Please let me know if this answers your question.

-mandar

Mandar Mitra

unread,
Mar 18, 2025, 3:10:34 AMMar 18
to Rahul Bhattacharya, isi-mt...@googlegroups.com
Rahul Bhattacharya wrote (Fri, Mar 14, 2025 at 12:04:27AM +0530):
> Please, let me know where I am wrong

Your starting assumption is incorrect. Two processes do not share the same registers.

The values stored in the registers are a part of a process' context (also called the hardware register context). Recall from our discussion in the Process chapter that this is the only part of the process context that needs to be explicitly copied to a safe place in memory during a context switch, because the next process (say P') to be given the CPU by the kernel will re-use the hardware registers for its own calculations.

When the context of P' is restored, the hardware registers will be loaded with the values saved in the context of P'.

Does this make sense?
Reply all
Reply to author
Forward
0 new messages