-1

--> Re-editing my question. I thought to picture my understanding. Here is the picture. Please correct me here. By task, I mean process only. A picture is worth a thousand words.

enter image description here

enter image description here

What will happen in the multi-processor, if the third process wants to acquire the lock. Since, it is two processor. The third processor may try to acquire the lock on CPU A since CPU B is busy polling. This can lead to a problem. Is my understanding correct? So, while using spin lock - one should ensure that the number of process contending for the critical region should not be greater than the number of CPU's available in the system? Also, if my system is uniprocessor, I shouldn't use spin lock at all. As it is compiled off? Is my understanding correct? We do not use sleep inside spinlocks is that because we don't want the code to do pre-empted during sleep when actually inside the spinlock () - which disables pre-emption. But pre-emption can occur if the code execution time exceeds the time slice intended for it. Thus, my question is basic - should we use a short length of code inside critical region? Because a long execution code can cause also pre-emption as the sleep would cause?

dexterous
  • 6,422
  • 12
  • 51
  • 99
  • Spin locks disable pre-emption. If CPU A has the spinlock and sleeps/schedules and CPU B wants it, then CPU B has to wait for CPU A to finish it's business. This is turning an SMP system into a *uni-processor* system; you are right it will not dead lock. It will just make thing highly in-efficient as there is no gaurentee that scheduling won't take a long time to return control. – artless noise Apr 29 '14 at 20:58
  • Why there is no problem in multiprocessor? – dexterous Apr 30 '14 at 01:40
  • There is a problem for both SMP and uniprocessor. On SMP, you make it a *uniprocessor*; I call that a problem. See: [Spinlock wiki](http://en.wikipedia.org/wiki/Spinlock), you have some conceptual issues which are difficult to over come. – artless noise Apr 30 '14 at 14:40

1 Answers1

1

The confusion is because you looking everything from "process context" only and totally forget Intr context, premption

http://www.makelinux.net/ldd3/chp-5-sect

Sasi V
  • 1,074
  • 9
  • 15
  • http://stackoverflow.com/questions/817059/what-is-preemption-what-is-a-preemtible-kernel-what-is-it-good-for. Refer this for what is premption and what is premptive-kernel? – Sasi V Apr 30 '14 at 07:12
  • On uniprocessor machines, the locks compile away and do not exist; they simply act as markers to disable and enable kernel preemption. If kernel preempt is turned off, the locks compile away entirely – Sasi V Apr 30 '14 at 07:12