Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy."— Presentation transcript:

1 CS 2200 Presentation 18b MUTEX

2 Questions?

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

4 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.

5 Single Processor Shared Frame P1 Page Table P2 Page Table Physical Memory

6 MultiProcessor (shared frame) Processor Memory

7 Problems?

8 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory

9 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

10 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

11 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

12 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

13 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

14 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

15 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

16 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

17 Example Process A Puts customer account number and amount into memory. (If Account == 0) Process B Gets customer account number and amount from memory. (If Account != 0) Sets Account = 0 Account Amount Shared memory lock

18 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

19 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.

20 History

21 E. Dijkstra applied the concept of semaphores to Computer Science

22 Semaphores void 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?

23 Usage -- Critical Section Mutex /* mutex globally initialized to 1 */ while(1) { wait(&mutex); /* Critical Section */ signal(&mutex); /* Other stuff */ } While in critical section mutex = 0

24 Counting Semaphores What happens when mutex variable is initialized to some value other than 1?

25 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);

26 Questions?

27 An Approach loop: 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 Yes 2 No

28 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.

29 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!

30 Using Atomic Swap loop: while(swap(&lock,RED) == RED) { // spin! } // Access shared here lock = GREEN; goto loop; loop: while(swap(&lock,RED) == RED) { // spin! } // Access shared here lock = GREEN; goto loop;

31 Real Stuff /* Swaps val and contents pointed to by * lock. Puts result of swap into location * pointed to by out. */ static void exchange( unsigned int *lock, unsigned int val, unsigned int *out) { asm("swap [%i0], %i1"); asm("st %i1, [%i2]"); } /* exchange */

32 Real Stuff /* This is a wrapper to make exchange more * user-friendly! It returns 1 if we didn't * get the lock and 0 if we did. */ static unsigned int test_and_set (unsigned int *lock) { unsigned int retval; exchange(lock, 1, &retval); return retval; } // test_and_set */

33 Another Problem? Recall Multiprocessor Cache Coherency

34 Using Atomic Swap loop: while(swap(&lock,RED) == RED) { // spin! } // Access shared here lock = GREEN; goto loop; loop: while(swap(&lock,RED) == RED) { // spin! } // Access shared here lock = GREEN; goto loop;

35 It works but... The atomic swap fixes our problem but creates a new one! Processor Cache Processor Cache Processor Cache Memory What happens every time we swap (read & write) to the lock? lock

36 Using Atomic Swap with Caching loop: do { while(lock == RED) { // spin } } while(swap(&lock,RED) == RED) // Access shared here lock = GREEN; goto loop; loop: do { while(lock == RED) { // spin } } while(swap(&lock,RED)== RED) // Access shared here lock = GREEN; goto loop;

37 Operation Processor 2Processor 1Processor 3 while(lock==RED)has lock...while(lock==RED) invalidateslock = GREEN;invalidates Off doing other stuffswap 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...

38 It works! The cache hardware is now helping us! Processor Cache Processor Cache Processor Cache Memory lock

39 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; }

40 Questions?

41


Download ppt "CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy."

Similar presentations


Ads by Google