●●● ●●●● 6.4 Synchronization Hardware 3:: ●●●0 Solution 1(implement lock by closing interrupts)o Disabling interrupts while a shared variable was being modified o Can be applied to Uni-processor o Non-preemptive kernel takes this approach Not feasible for Multi-processors Disabling interrupts can be time consuming, as the message is passed to all the processors o The message passing delays entry into each critical section, and system efficiency decreases o The effect on a system's clock, if the clock is kept updated by interrupts
27 6.4 Synchronization Hardware ⚫ Solution 1 (implement lock by closing interrupts) ⚫ Disabling interrupts while a shared variable was being modified ⚫ Can be applied to Uni-processor ⚫ Non-preemptive kernel takes this approach ⚫ Not feasible for Multi-processors ⚫ Disabling interrupts can be time consuming, as the message is passed to all the processors ⚫ The message passing delays entry into each critical section, and system efficiency decreases ⚫ The effect on a system’s clock,if the clock is kept updated by interrupts
●●● ●●●● 6.4 Synchronization Hardware 99 ●●●● e Solution 2--special atom instruction ●●●● TestAndseto o Test and modify the content of a word atomically boolean TestAndSet(boolean lock) boolean ry= lock lOck= TRUE return rv: }/将lock置为true并返还原先ock值
28 6.4 Synchronization Hardware ⚫ Solution 2--Special atom instruction ⚫ TestAndSet() ⚫ Test and modify the content of a word atomically boolean TestAndSet (boolean lock) { boolean rv = lock; lock = TRUE; return rv: } //将lock置为true并返还原先lock值
●●● ●●●● 6.4 Synchronization Hardware 3:: ●●●● Process P ●●●● do i while(TestAndSet(lock)); critical section lock= false: remainder section o If two TestAndSet( are executed simultaneously each on a different CPU, they will be run sequentially in some arbitrary order
29 6.4 Synchronization Hardware ⚫ Process Pi do { while (TestAndSet(lock)) ; critical section lock = false; remainder section } ⚫ If two TestAndSet() are executed simultaneously each on a different CPU, they will be run sequentially in some arbitrary order
●●● ●●●● 6.4 Synchronization Hardware 3 :s ●●●● o Swapo ●●●● Swap two variables atomically void Swap(boolean &a, boolean &b)t boolean temp= a a= b b= temp a: lock b: key
30 6.4 Synchronization Hardware ⚫ Swap() ⚫ Swap two variables atomically void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; } a: lock b: key
●●● ●●●● 6.4 Synchronization Hardware 3:: ●●●● e Process p ●●●● do i key= true, while (key = true)Swap(lock, key) critical section lock= false; remainder section o Satisfies mutual exclusion but not the bounded- waiting requirement o If there are many processes waiting for entering Pi may be blocked in TestAnd Set( or Swapo without limit
31 6.4 Synchronization Hardware ⚫ Process Pi do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section } ⚫ Satisfies mutual exclusion, but not the boundedwaiting requirement ⚫ If there are many processes waiting for entering, Pi may be blocked in TestAndSet() or Swap() without limit