To see why, assume that A and B are process context threads, and C is an interrupt context thread, all vying to enter the same critical section, as shown in Figure 2.4.įigure 2.4. In this case, you need to disable only interrupts to protect the critical region. Case 2: Process and Interrupt Contexts, UP Machine, No Preemption This is the simplest case and needs no locking, so we won't discuss this further. However, semaphores that permit more than a single holder at a time are rarely used.Ĭase 1: Process Context, UP Machine, No Preemption Semaphores can be configured to allow a predetermined number of threads into the critical section simultaneously. To dynamicallyĭown(&mysem) /* Acquire the semaphore */ ![]() (You will learn more about the constraints of the interrupt context in Chapter 4.) It's illegal to schedule, preempt, or sleep on a wait queue after acquiring a spinlock.īecause mutexes put the calling thread to sleep in the face of contention, you have no choice but to use spinlocks inside interrupt handlers. If the critical section needs to sleep, you have no choice but to use a mutex. In many cases, therefore, it's easy to decide whether to use a spinlock or a mutex: In mutex terms, anything more than two context switches is considered long, because a mutex has to switch out the contending thread to sleep, and switch it back in when it's time to wake it up. Because it's a bad thing to consume processor cycles to spin, mutexes are more suitable than spinlocks to protect critical sections when the estimated wait time is long. In contrast to spinlocks that put threads into a spin if they attempt to enter a busy critical section, mutexes put contending threads to sleep until it's their turn to occupy the critical section. Spin_unlock(&mylock) /* Release the lock */ * contention, spinlock() has to busy-wait. Spinlock_t mylock = SPIN_LOCK_UNLOCKED /* Initialize */ The basic usage of spinlocks is as follows: Note that we use the term thread to refer to a thread of execution, rather than a kernel thread. Any other thread that desires to enter the critical section has to remain spinning at the door until the first thread exits. Let's look at each in turn.Ī spinlock ensures that only a single thread enters a critical section at a time. Spinlocks and mutexes (short for mutual exclusion ) are the two basic mechanisms used to protect critical sections in the kernel. Spinlocks and MutexesĪ code area that accesses shared resources is called a critical section. We start with a simple example and gradually introduce complexities such as interrupts, kernel preemption, and SMP. Let's discuss the basics of protecting shared kernel resources from concurrent access. Because of this, accesses to such data structures have to be serialized. These threads can simultaneously operate on shared kernel data structures. ![]() SMP and kernel preemption are scenarios that generate multiple threads of execution. ![]() With the arrival of multicore laptops, Symmetric Multi Processing (SMP) is no longer confined to the realm of hi-tech users.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |