10/17/16 1 Concurrency: Locks Questions answered in this lecture : Review threads and mutual exclusion for critical sections How can locks be used to protect shared data structures such as linked lists ? Can locks be implemented by disabling interrupts ? Can locks be implemented with loads and stores ? Can locks be implemented with atomic hardware instructions ? Are spinlocks a good idea? UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department CS 537 Introduction to Operating Systems Andrea C. Arpaci-Dusseau Remzi H. Arpaci-Dusseau Announcements P2: Due this Friday à Extension to Sunday evening… • Test scripts and handin directories available • Purpose of graph is to demonstrate scheduler is working correctly 1 st Exam: Congratulations for completing! • Grades posted to Learn@UW : Average around 80% 90% and up: A 85 - 90: AB 80 - 85: B 70 - 80: BC 60 - 70: C Below 60: D • Return individual sheets in discussion section • Exam with answers will be posted to course web page soon… Read as we go along! • Chapter 28
24
Embed
CS 537 Andrea C. Arpaci-Dusseau Introduction to Operating ...pages.cs.wisc.edu/~dusseau/Classes/CS537/Fall2016/Slides/lecture10... · Can locks be implemented with atomic hardware
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
10/17/16
1
Concurrency:Locks
Questions answered in this lecture:
Review threads and mutual exclusion for critical sections
How can locks be used to protect shared data structures such as linked lists?
Can locks be implemented by disabling interrupts?
Can locks be implemented with loads and stores?
Can locks be implemented with atomic hardware instructions?
Are spinlocks a good idea?
UNIVERSITY of WISCONSIN-MADISONComputer Sciences Department
CS 537Introduction to Operating Systems
Andrea C. Arpaci-DusseauRemzi H. Arpaci-Dusseau
AnnouncementsP2: Due this Friday à Extension to Sunday evening…• Test scripts and handin directories available• Purpose of graph is to demonstrate scheduler is working correctly
1st Exam: Congratulations for completing!• Grades posted to Learn@UW : Average around 80%
90% and up: A85 - 90: AB80 - 85: B70 - 80: BC60 - 70: CBelow 60: D
• Return individual sheets in discussion section
• Exam with answers will be posted to course web page soon…
Read as we go along!• Chapter 28
10/17/16
2
CPU 1 CPU 2runningthread 1
runningthread 2
RAMPageDir A
PageDir B…PTBRPTBR
CODE HEAPVirt Mem(PageDir B)
IP IPSP SP
Review:Which registers store the same/different values across threads?
CPU 1 CPU 2runningthread 1
runningthread 2
RAMPageDir A
PageDir B…PTBRPTBR
CODE HEAPVirt Mem(PageDir B)
IP IPSP SP
STACK 1 STACK 2
All general purpose registers are virtualized à each thread given impression of own copy
10/17/16
3
Review: What is needed for CORRECTNESS?
Balance = balance + 1;
Instructions accessing shared memory must execute as uninterruptable group • Need group of assembly instructions to be atomic
mov 0x123, %eaxadd %0x1, %eaxmov %eax, 0x123
critical section
More general:Need mutual exclusion for critical sections• if process A is in critical section C, process B can’t
(okay if other processes do unrelated work)
Other Examples
Consider multi-threaded applications that do more than increment shared balance
Multi-threaded application with shared linked-list• All concurrent:
Mutual exclusion: Enter critical section if and only ifOther thread does not want to enter OROther thread wants to enter, but your turn (only 1 turn)
Progress: Both threads cannot wait forever at while() loopCompletes if other process does not want to enterOther process (matching turn) will eventually finish
Bounded waiting (not shown in examples)Each process waits at most one critical section (because turn given to other)
Problem: doesn’t work on modern hardware(doesn’t provide sequential consistency due to caching)
10/17/16
15
Implementing Synchronization
To implement, need atomic operations
Atomic operation: No other instructions can be interleaved
Examples of atomic operations• Code between interrupts on uniprocessors
• Disable timer interrupts, don’t do any I/O
• Loads and stores of words• Load r1, B
• Store r1, A
• Special hw instructions• Test&Set
• Compare&Swap
xchg: atomic exchange, or test-and-set
// xchg(int *addr, int newval) // ATOMICALLY return what was pointed to by addr // AT THE SAME TIME, store newval into addr
int xchg(int *addr, int newval) {int old = *addr;*addr = newval;return old;
}Need hardware supportstatic inline uintxchg(volatile unsigned int *addr, unsigned int newval) {