Reputation: 665
I am reading Operating System Concepts by Galvin. In the semaphore section it says that all the interrupts to the processor must be disabled while modifying the value of semaphore. Why it is required?
Upvotes: 6
Views: 2200
Reputation: 21647
Some systems have uninterruptable instructions for such purposes. Interrupts are not really blocked but rather the interrupt never has a chance to occur during the process.
Some systems can disable groups of interrupts while allowing others to continue.
Other systems require blocking interrupts while modifying such structures as described above.
I also clarify that the LOCK prefix (and its equivalent on other system) prevents modification during the execution of the instruction by another processor.
Upvotes: 0
Reputation: 24887
On many OS, posting a unit to a semaphore is one of those few kernel synchro operations that is supported from interrupt state. That means that a semaphore post or wait call from user code may be interrupted and then reentered by a post from an interrupt driver. A great deal of care must be taken with the semaphore state, (count and queue of waiting threads), to ensure that it remains internally consistent and operates correctly. This may involve disabling all interrupts briefly in order to update counts, indexes and pointers in a secure manner.
It's not just a matter of atomic instructions - a semaphore post can make a waiting thread ready to run or running, and this means moving thread control blocks from the semaphore waiting queue to the ready queues, (maybe with a priority boost), preempting a thread running on another core than the one handling the driver interrupt and dispatching the previously-waiting thread onto that core.
Such complex operations MUST still work correctly if a post/wait operation is allowed to be interrupted and reentered from an interrupt handler, so it's not really surprising that OS devs try to ease the problem by disabling all interrupts around critical sections of synchronization handling.
Upvotes: 0
Reputation: 1125
Interrupts are disabled to prevent scheduler from Un-scheduling the "thread" ( that performs the semaphore operations) while performing semaphore operations.
Upvotes: -1
Reputation: 522264
If processor interrupts were allowed to take place during the modification of a semaphore's value, then it would be possible for this value to end up in an inconsistent state. During an interrupt, a certain set of instructions will execute. These instructions can, in principle, do the same things that any executing code can do. Specifically, the interrupt might use the value of the semaphore in its logic, or copy that value somewhere else. If the value be in an inconsistent state, this can break your code logic.
Upvotes: 2
Reputation: 7374
Modifications to semaphore values must be done atomically. On a single core system, this can be done by disabling interrupts so that the read/modify/write operation(s) to change the semaphore cannot be interrupted.
On a multicore system disabling interrupts is not enough, so multicore capable processors usually have atomic instructions to do the modification. For example, on Intel/AMD processors there is a lock prefix to make an instruction execute atomically.
So Galvin is not completely right.
Upvotes: 1