0

I read that in spinlock, process keeps on waiting for the lock continuously in a loop until it receives signal(lock) or release(lock) from other process. This causes busy waiting and hence CPU cycles are wasted.

My doubt is if CPU is continuously checking the value of lock variable in loop, then how will some other process completely execute critical section and release the lock as executing critical section also needs CPU and CPU is busy checking the value of lock in other process ?

2

Even when one process is spinning on a lock, the task scheduler is still active and will schedule other tasks. On a single-CPU system, the spinning task will be suspended, and another task run in its place; on a multi-CPU system, the exact behaviour will depend on the number of running tasks.

  • If spinning task is suspended and other task is scheduled to be run by CPU then how does this cause busy waiting as CPU cycles are not wasted? – Zephyr Oct 31 '17 at 8:56
  • 1
    They’re wasted as long as the spinning task is scheduled. The spinning task isn’t necessarily suspended immediately, and if the other tasks don’t free the lock very quickly, the spinning task will be rescheduled and run again, before being suspended again etc. – Stephen Kitt Oct 31 '17 at 8:59
  • Suppose we have 2 processes P1 and P2. If process P1 is in critical section and gets preempted by high priority process P2. Now if P2 uses spinlock then it will be keep on waiting for the lock(as P1 is in critical section). Now the CPU cannot even switch to P1 as it has lower priority than P2. Now what will happen in this case ? Here all CPU cycles will be wasted for P2 forever. – Zephyr Oct 31 '17 at 9:16
  • That’s dealt with by raising the priorities of processes holding locks. – Stephen Kitt Oct 31 '17 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.