2

I was going through the Robert Love Book and was bit confused about this line. What does it mean by code holding semaphore can be preempted?

If an interrupt occurs accessing the same variable which the user-space application has while it is executing the code in critical section then the user-space application can be preempted?

If my above understanding is true then there is no other alternative than spin-locks to disable an interrupt whenever an user-space application is in critical section?

So what is the use of semaphore in the context of OS? Interrupts might occur anytime when the user application is in critical section and in-order to avoid interrupt intervention we need to use spin-locks all the time.

Smita
  • 103
  • 7
  • The problem with your question is that we do not know the context. What kind of semaphore? It is possible for a kernel to implement a semaphore so that the process holding it cannot be preempted. Or it a semaphore could be implement so that the process holding it could not be preempted. – user3344003 Nov 02 '18 at 22:32
  • Yes, but according to my understanding we can't implement a semaphore which cannot be preempted right when the interrupt occurs? – Smita Nov 02 '18 at 22:59
  • You could block preemption as part of grabbing the semaphore. That's not normal but it could be done. – user3344003 Nov 03 '18 at 00:21

1 Answers1

2

What does it mean by code holding semaphore can be preempted?
It means that a process that is currently running in its critical section holding some lock for the purpose of synchronization can be preempted. Ideally interrupts have the highest priority, so unless you disable the interrupt on that processor core, the running process can be preempted and that might happen while the process is in its critical section.

While there are multiple spin_lock_XXX apis to disable interrupts, you might want to use the spin_lock_irqsave as it saves the interrupt flags on that core and restores them while releasing the lock.

golimoli
  • 143
  • 9