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

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

HW/Study Guide. Synchronization Make sure you understand the HW problems!
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1/25 Concurrency and asynchronous computing How do we deal with evaluation when we have a bunch of processors involved?
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Synchronization Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
The Performance of Spin Lock Alternatives for Shared-Memory Multiprocessors THOMAS E. ANDERSON Presented by Daesung Park.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
1 Lecture 19: Scalable Protocols & Synch Topics: coherence protocols for distributed shared-memory multiprocessors and synchronization (Sections )
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Big Picture Lab 4 Operating Systems C Andras Moritz
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Multiprocessors – Locks
Tutorial 2: Homework 1 and Project 1
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Concurrency 2 CS 2110 – Spring 2016.
CSE 120 Principles of Operating
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Atomic Operations in Hardware
Atomic Operations in Hardware
Lecture 18: Coherence and Synchronization
Chapter 5: Process Synchronization
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Lecture 22 Syed Mansoor Sarwar
Lecture: Coherence and Synchronization
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Implementing Mutual Exclusion
Chapter 6: Process Synchronization
Lecture 21 Syed Mansoor Sarwar
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
Process Synchronization
Lecture 19: Coherence and Synchronization
Lecture 18: Coherence and Synchronization
Process Synchronization
Process/Thread Synchronization (Part 2)
Presentation transcript:

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.

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

MultiProcessor (shared frame) Processor Memory

Problems?

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

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

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

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

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

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

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

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

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

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

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

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.

History

E. Dijkstra applied the concept of semaphores to Computer Science

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?

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

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

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

Questions?

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

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.

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!

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;

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 */

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 */

Another Problem? Recall Multiprocessor Cache Coherency

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;

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

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;

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

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

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

Questions?