Web viewFUTEX(Fast Userspace muTEX) 10. CHAPTER NO. 1. INTRODUCTION
CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O...
-
Upload
gwendolyn-porter -
Category
Documents
-
view
222 -
download
0
description
Transcript of CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O...
![Page 1: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/1.jpg)
CS 2200
Presentation 18bMUTEX
![Page 2: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/2.jpg)
Questions?
![Page 3: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/3.jpg)
Our Road Map
Processor
Networking
Parallel Systems
I/O Subsystem
Memory Hierarchy
![Page 4: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/4.jpg)
Recall• Various schemes allow multiple processes
in memory at the same time• Virtual memory allowed 2 (or more)
processes to share pages.– e.g. Multiple users of tin sharing pure code
pages• Processes can also share pages of data
where both processes can read and write to memory.
![Page 5: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/5.jpg)
Single Processor Shared FrameP1 Page
Table
P2 Page Table
PhysicalMemory
![Page 6: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/6.jpg)
MultiProcessor (shared frame)
Processor Processor Processor
Memory
![Page 7: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/7.jpg)
Problems?
![Page 8: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/8.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
![Page 9: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/9.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 10: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/10.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 11: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/11.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 12: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/12.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 13: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/13.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 14: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/14.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 15: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/15.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 16: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/16.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 17: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/17.jpg)
Example
Process A
Puts customeraccount numberand amount intomemory.(If Account == 0)
Process B
Gets customeraccount numberand amount frommemory.(If Account != 0)
Sets Account = 0
AccountAmount
Shared memory
lock
![Page 18: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/18.jpg)
Mutex• Mutual Exclusion• Two or more processes need to share an
area in memory• Only one should have access at a time• Designate a single memory location as the
lock• This location can have two values: 0 or 1.• For simplicity assume that there are
constants defined: RED and GREEN
![Page 19: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/19.jpg)
Mutex• If the lock location is GREEN a process
can change it to RED and use the shared memory area.
• When finished it sets the lock back to GREEN and lets another process have a turn.
![Page 20: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/20.jpg)
History
![Page 21: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/21.jpg)
History• E. Dijkstra applied the concept of
semaphores to Computer Science
![Page 22: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/22.jpg)
Semaphoresvoid wait(int *s) { /* P */while (*s <= 0) {
/* spin */}*s = *s - 1;
}
void signal(int *s) { /* V */*s = *s + 1;
}
What is the value of s when locked?Unlocked?
![Page 23: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/23.jpg)
Usage -- Critical Section Mutex/* mutex globally initialized to 1 */
while(1) {wait(&mutex);
/* Critical Section */signal(&mutex);
/* Other stuff */}
While in criticalsection mutex = 0
![Page 24: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/24.jpg)
Counting Semaphores• What happens when mutex variable is
initialized to some value other than 1?
![Page 25: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/25.jpg)
Usage -- Synchronization/* Initialize synch to 0 */
/* Process P2 is supposed to wait */wait(&synch);/* P2 now continues */
/* Process P1 executes this code first */
signal(&synch);
![Page 26: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/26.jpg)
Questions?
![Page 27: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/27.jpg)
An Approachloop:
while(lock == RED) {// spin!
}lock = RED;// Access shared
lock = GREEN;
goto loop;
loop:
while(lock == RED) {// spin!
}lock = RED;// Access shared
lock = GREEN;
goto loop;Work?1 Yes2 No
![Page 28: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/28.jpg)
Need Atomic Operation• Like atomic swap
int swap(mutex *lock, int val);
• Operation: The value in val is stored in the mutex variable lock and the value that was in lock is returned. Both operations are carried out atomically.
![Page 29: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/29.jpg)
How to use...while(swap(&lock, RED) == RED){
// spin}
• Swap puts the value RED into the lock variable.• If the lock variable is RED then no change
occurs.• If the lock variable is GREEN then the lock
variable becomes RED and GREEN is returned!
![Page 30: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/30.jpg)
Using Atomic Swaploop:
while(swap(&lock,RED) == RED)
{// spin!
}
// Access shared herelock = GREEN;
goto loop;
loop:
while(swap(&lock,RED) == RED)
{// spin!
}
// Access shared herelock = GREEN;
goto loop;
![Page 31: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/31.jpg)
Another Problem?
Recall Multiprocessor Cache Coherency
![Page 32: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/32.jpg)
Using Atomic Swaploop:
while(swap(&lock,RED) == RED)
{// spin!
}
// Access shared herelock = GREEN;
goto loop;
loop:
while(swap(&lock,RED) == RED)
{// spin!
}
// Access shared herelock = GREEN;
goto loop;
![Page 33: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/33.jpg)
It works but...• The atomic swap fixes our problem but
creates a new one!
Processor
Cache
Processor
Cache
Processor
Cache
MemoryWhat happens every timewe swap (read & write) tothe lock?
lock
![Page 34: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/34.jpg)
Using Atomic Swap with Cachingloop:do { while(lock == RED) {
// spin }} while(swap(&lock,RED) == RED)// Access shared herelock = GREEN;
goto loop;
loop:do { while(lock == RED) {
// spin }} while(swap(&lock,RED)== RED)// Access shared herelock = GREEN;
goto loop;
![Page 35: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/35.jpg)
Operation
Processor 2Processor 1 Processor 3
while(lock==RED)has lock... while(lock==RED)
invalidateslock = GREEN; invalidates
invalidatesOff doing other stuff swap returns Green
swap returns RED has lock...
while(lock==RED) has lock...
while(lock==RED) has lock...
invalidates lock = GREEN;
swap returns Green Off doing other stuff
has lock...
![Page 36: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/36.jpg)
It works!• The cache hardware is now helping us!
Processor
Cache
Processor
Cache
Processor
Cache
Memorylock
![Page 37: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/37.jpg)
Truth in CS:Decker's mutual exclusion algorithm
shared boolean locked[2] = {false, false};private int my_id; // 0 or 1// do_critical(f): execute f when we have exclusive// access to critical region.void do_critical(VoidFn f) { do { locked[my_id] = false; while(locked[1-my_id]); // spin while the other // has access to CS
locked[my_id] = true; } while(locked[1-my_id]); // now we have the exclusive access to CS f(); locked[my_id] = false;}
![Page 38: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/38.jpg)
Questions?
![Page 39: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.](https://reader034.fdocuments.us/reader034/viewer/2022051200/5a4d1b4a7f8b9ab0599a53ef/html5/thumbnails/39.jpg)