CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O...

39
CS 2200 Presentation 18b MUTEX

description

Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy

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.

CS 2200

Presentation 18bMUTEX

Page 2: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.

Questions?

Page 3: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.

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.

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.

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.

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.

Problems?

Page 8: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

History

Page 21: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.

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.

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.

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.

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.

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.

Questions?

Page 27: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Questions?

Page 39: CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.