40 likes | 211 Vues
This guide delves into critical section tools utilized in operating systems, focusing on hardware interface locks, semaphores, and various locking mechanisms like mutex locks and monitors. We explore how these tools help manage concurrent threads and prevent race conditions when sharing data. Through practical examples such as atomic exchange and fetch-and-increment techniques, we illustrate how to implement effective synchronization strategies. Whether using Java, C#, or customizing your own solution, enhancing your kernel’s role with optimized locking mechanisms is key to efficient data handling.
E N D
Critical Section Tools • (HW interface) locks implemented in ISA • T&S, CAS • (O.S. )Semaphores • Counting and Binary (a.k.a a mutex lock) • (Augmented O.S.) Monitors & conditions • Java, C#, or make your own! • Need only atomic lock ops at the ISA level, implement the rest yourself. • But, as more tools are offered by the kernel, the OS will expand its role here, offering easier-to-use or optimized solutions • Win and Pulse example
Critical Section • A section of code that shares data with other concurrent threads • Sharing of data makes a section critical • Specifically, write contention • In a kernel, many shared data structures • Need to synchronize these to guard against race conditions • OR, make kernel thread scheduling non-preemptive
How to Make an Atomic Exchange • Try: MOVE $s3, $a1 ;move swap val into $s3 • LL $s2, 0($s1) ; load linked • SC $s3,0($s1) ; store conditional • BEQZ $s3, try ;sc returns success in $s3 • MOVE $v1, $s2 ;return load result • ;simple atomic exchange • ; if the contents of $s1 change between the LL and the SC? SC fails and returns 0. • Similar in spirit to return address verification • ; note: context switch between LL and SC? SC fails and returns 0 in $s3 then
Another Lock: fetch-and-increment • Try: LL $t2, 0($t1) • DADDUI $t3, $t2, 1 ;our increment • SC $t3, 0($t1) • BEQZ $t3, try ;branch if store fails