Vous êtes sur la page 1sur 4

Kernel Synchronization

1. What is a deadlock? a. A deadlock is a condition involving one or more threads of execution and one or more resources, such that each thread is waiting for one of the resources, but all the resources are already held. 2. What is a self deadlock? a. If a thread of execution attempts to acquire a lock that it already holds is in self deadlock. 3. What is a ABBA deadlock? a. Consider two threads thread1 and thread2 acquire locks A and B respectively. Then if Thread1 tries to acquire lock B and Thread2 tries to acquire lack A, each is waiting for the other to release the lock and neither will ever release the lock they are initially holding. This type of deadlock is called ABBA deadlock. ABBA deadlock is also called deadly embrace. 4. What is a deadly embrace? a. ABBA deadlock is called deadly embrace. 5. How deadlocks can be avoided? a. Although it is difficult to prove that code is free deadlocks. Following rules are helpful in avoiding deadlocks. i. Lock ordering is vital. Nested locks must always obtained in the same order. ii. Document the lock ordering. iii. Do not double acquire the same lock iv. Prevent starvation. v. Design for simplicity 6. What is a Spinlock? a. A spin lock is synchronization object that can be held at most by one thread of execution. 7. If data is shared between two tasklets then data must be protected by a spin lock. 8. Softirqs are capable of running simultaneously, therefore if data is shared between two softirqs then it needs to be protected using a spin lock. 9. Spin locks are not recursive. 10. Spin locks are architecture dependent 11. Spin locks are used in interrupt handlers 12. What are Reader-writer spin locks? a. Reader-writer spin locks provide separate reader writer variants of the lock. One or more readers can hold the reader lock concurrently. Witer locks can utmost be held by one writer with no concurrent readers. 13. Reader writer spin lock is initialized via rwlock_t mr_rwlock RW_LOCK_UNLOCKED 14. What happens when following code is executed? read_lock (&mr_rwlock);

write_lock (&mr_rwlock); a. It will deadlock as the writer lock will spin, waiting for all readers to release the lock In linux in reader-writer spin locks, readers are favored over writers. What is a semaphore? a. Semaphore is a synchronization object that is capable of going to sleep when the lock it is trying to acquire is already held by another task. i. When a process holding the semaphore is released, one of the tasks on the wait queue will be awakened up so that it can acquire the semaphore. Discuss sleeping behaviour of semaphores? a. Seeping behaviour of semaphores is as below. i. Because the contending tasks sleep while waiting for the clock to become available, semaphores are well suited to lock that can be held a long time. ii. Semaphores are not optimal for that are held for very short periods because the overhead of sleeping can overweigh the total lock hold time. iii. Because a thread of execution sleeps on lock contention, semaphore are not suitable for use in interrupt handlers. They can be used only in process context. iv. A task can sleep while holding the semaphore because it will not deadlock when another process acquires the semaphore. As it will also sleep because the semaphore is already under contention. v. A task cannot hold a spin lock while it acquires a semaphore because the task may have to sleep if the semaphore is under contention. Decision between semaphore and spin lock ideally should be based on lock hold time. Semaphores do not disable kernel preemption while code holding spin locks can preempt kernel. Semaphore do not adversely affect scheduling latency. Can semaphores can be held by multiple tasks? a. Yes, multiple tasks can simultaneously hold the lock. It can be set during declaration time. This value is called usage count or simply count. What is a binary semaphore? a. If usage count is set as one, then it is called a binary semaphore. This is also called mutex as it enforces mutual exclusion. What is a counting semaphore? a. If the usage count is initialized to a non zero value greater than one, then it is called a counting semaphore. What is a down () method? a. down () method is used to acquire a semaphore by decrementing the count by one. What is a up () method? a. up () method is used to release a semaphore and count is incremented by one. What is a mutex? a. Mutex is a binary semaphore. Semaphore is statically declared using a. static DECLARE_SEMAPHORE_GENERIC (name, count); i. name is the variable name and count is usage count.

15. 16.

17.

18. 19. 20. 21.

22.

23.

24. 25. 26. 27.

28. Mutex is created using a. static DECLARE_MUTEX (name); 29. To initialize a dynamically created semaphore use a. sema_init (sem, count); 30. To initialize a dynamically created mutex use a. Init_MUTEX (sem); 31. How to debug a spinlock? a. Spin locks can be debugged using CONFIG_DEBUG_SPINLOCK configurable option i. It checks uninitialized spin locks ii. It checks unlocking a lock that is not yet held. 32. Spinlocks can be used in interrupt handlers, why? a. Spin locks cannot be put to sleep. 33. Why semaphores cant be used in interrupt handlers? a. Semaphores sleep 34. What is double-acquire deadlock? a. If a spin lock is used in interrupt handler, it is possible to interrupt kernel code while the lock is held and attempt to reacquire the lock. The interrupt handler spins, waiting for the lock to become available. The lock holder will not run until the interrupt handler completes. This is double-acquire deadlock. i. If the lock is used in interrupt handler, local interrupts must be disabled on the current processor before obtaining the lock. 35. How many threads can lock spinlock at a given point in time? a. One thread of execution. 36. What are reader writer semaphores? a. All reader-writer semaphores are mutexes. Any number of readers can hold semaphore as long as there are no writers concurrently. b. A writer with no readers can acquire writer variant of the lock. c. All reader-writer locks use uninterruptible sleep, sot here is only version of each down() 37. What a downgrade_writer () is used for? a. To convert an acquired write lock to a read lock. 38. What is a condition variable? a. Condition variable is a synchronization mechanism used to synchronize two tasks in the kernel, when one task is needs to signal the other that an event has occurred. One task waits on condition variable while the other can perform some work. 39. What is BKL? a. Big Kernel Lock (BKL) is a global spin lock. 40. What are the properties of BKL? a. A task can sleep while holding the BKL. Lock is automatically dropped when the task is unscheduled and reacquired when the task is rescheduled. b. BKL is a recursive lock i. It means a single process can acquire the lock multiple times and not deadlock as it would with spin lock. c. BKL can be used only in process context. 41. What is a seq lock?

a. Seq lock is used for reading and writing shared data. It is introduced in 2.6 kernel. i. Seq lock work by maintaining a sequence counter. Whenever the data in question is written to, a lock is obtained and sequence number is incremented. Seq locks favor writers over readers. 42. What is a Barrier? a. Barriers are instructions to compiler not to reorder instructions around a given point.

Vous aimerez peut-être aussi