answersLogoWhite

0

What is spinlock?

Updated: 12/20/2022
User Avatar

Wiki User

16y ago

Best Answer

Semaphores are a useful tool for mutual exclusion, but they are not the only such tool provided by the kernel. Instead, most locking is implemented with a mechanism called a spinlock. Unlike semaphores, spinlocks may be used in code that cannot sleep, such as interrupt handlers. When properly used, spinlocks offer higher performance than semaphores in general. They do, however, bring a different set of constraints on their use.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is spinlock?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

When was Spinlock SRL created?

Spinlock SRL was created in 2003.


What should a fitness instructor and client check before using free weights?

Check knurled surface for dirt/grip. Check spinlock is tight (weight securely connected to the handle)


Explain why spinlocks are not appropriate for single-processor systems yet are often used in multiprocessor systems?

A spinlock is basically a loop that constantly checks a condition until it is true. Think of a bunch of kids in the back of a car saying "Are we there yet...are we there yet...are we there yet...". This is a poor implementation for almost any setup, since the lock is wasting processor time that could be doing something more efficient than constantly checking a condition over and over. Also, while the spinlock is running on the processor, the system will be completely unresponsive.


How busy waiting occur?

I copied the answer from other place:while any process is in its critical section , any other process that tries to enter its critical section must loop continously in the entry code.this continue looping is clearly a problem in a real multiprogramming system. where a single CPU is shared among many processes. Busy waiting wastes CPU cycles that could have been used by some process in more productive way. This type of semaphore is also called a spinlock.


Why are spin locks inappropriate for uniprocessor systems?

because the process holding the spinlock would do nothing (thus wasting CPU cycles) when it is allocated the CPU. It is better to put it in the sleep state (that is putting the process in the waiting queue). As soon as the process is wakened up (by some other process/thread invoking "signal/wake" on it) it is put in the "ready" queue when it runs as soon as the CPU is assigned to it. on multiprocessor systems, it's generally better to "spin" because it is assumed that the running process on the other processor will release the lock pretty soon (thus it can release the lock and still continue running while your thread/process is getting a hold of the lock). for better explanation check out "Operating System Concepts" by Silberchatz, also Cornell University slides on Operating Systems Course are pretty useful as well.


What is a Mutex?

Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of un-shareable resources by pieces of computer code called critical sections. Examples of such resources are fine-grained flags, counters or queues, used to communicate between code that runs concurrently, such as an application and its interrupt handlers. The problem is acute because a thread can be stopped or started at any time. To illustrate: suppose a section of code is mutating a piece of data over several program steps, when another thread, perhaps triggered by some unpredictable event, starts executing. If this second thread reads from the same piece of data, the data, in the process of being overwritten, is in an inconsistent and unpredictable state. If the second thread tries overwriting that data, the ensuing state will probably be unrecoverable. These critical sections of code accessing shared data must therefore be protected, so that other processes which read from or write to the chunk of data are excluded from running. On a uniprocessor system the common way to achieve mutual exclusion is to disable interrupts for the smallest possible number of instructions that will prevent corruption of the shared data structure, the so-called "critical region". This prevents interrupt code from running in the critical region. Beside this hardware supported solution, some software solutions exist that use "busy-wait" to achieve the goal. Examples of these algorithms include: * Dekker's algorithm * Peterson's algorithm * Lamport's bakery algorithm In a computer in which several processors share memory, an indivisible test-and-set of a flag is used in a tight loop to wait until the other processor clears the flag. The test-and-set performs both operations without releasing the memory bus to another processor. When the code leaves the critical region, it clears the flag. This is called a "spinlock" or "busy-wait." Some computers have similar indivisible multiple-operation instructions for manipulating the linked lists used for event queues and other data structures commonly used in operating systems. Most classical mutual exclusion methods attempt to reduce latency and busy-waits by using queuing and context switches. Some claim that benchmarks indicate that these special algorithms waste more time than they save. Many forms of mutual exclusion have side-effects. For example, classic semaphores permit deadlocks, in which one process gets a semaphore, another process gets a second semaphore, and then both wait forever for the other semaphore to be released. Other common side-effects include starvation, in which a process never gets sufficient resources to run to completion, priority inversion in which a higher priority thread waits for a lower-priority thread, and "high latency" in which response to interrupts is not prompt. Much research is aimed at eliminating the above effects, such as by guaranteeing non-blocking progress. No perfect scheme is known.