Operating Systems Concurrency ENCE 360.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Chapter 6: Process Synchronization
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.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Operating Systems Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive at.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Operating Systems Process Synchronization (Ch )
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Operating System I Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Chapter 28 Locks Chien-Chung Shen CIS, UD
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
CS703 – Advanced Operating Systems
Process Synchronization
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Atomic Operations in Hardware
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
CSE451 Basic Synchronization Spring 2001
Chapter 6: Process Synchronization
Chapter 26 Concurrency and Thread
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Critical section problem
Concurrency: Mutual Exclusion and Process Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Lecture 20: Synchronization
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Synchronization CSE 2431: Introduction to Operating Systems
Presentation transcript:

Operating Systems Concurrency ENCE 360

MODERN OPERATING SYSTEMS (MOS) Outline Introduction Solutions Classic Problems Chapter 2.3 MODERN OPERATING SYSTEMS (MOS) By Andrew Tanenbaum Chapter 26, 28, 31 OPERATING SYSTEMS: THREE EASY PIECES  By Arpaci-Dusseau and Arpaci-Dusseau

A long time ago, … Remember day 1? 68 prompt% threads-v0 100000 Initial value: 0 Final value: 200000 69 prompt% threads-v0 100000 Final value: 146796 A long time ago, … Remember day 1? Yes, single number, but what if bank account? What if print spooler? What if database? Thread 0 Thread 1 Thread 2 Thread 3 Paycheck Buy fancy new TV Roommate pays rent Buying a video game retrieve balance add 450 to balance store balance subtract 450 from balance add 300 to balance subtract 50 from balance

The Heart of the Problem Display information from object file - machine instructions: objdump –-source thread-v0 [line 415] g_counter++; 400c38: 8b 05 6e 14 20 00 mov 0x201465,%eax # 6020ac <g_counter> 400c3e: 83 c0 01 add $0x1,%eax 400c41: 89 05 65 14 20 00 mov %eax,0x201465 # 6020ac <g_counter> Source code from “-g” flag Address Object code Assembly code Reference location Let’s zoom in …

The Heart of the Problem (Zoom) mov g_counter %eax add 1 %eax mov %eax g_counter mov 0x20146e(%rip),%eax add $0x1,%eax mov %eax,0x201465(%rip) “critical section” Counter is 50. Thread T1 & T2, one processor. WCGW? “race condition” Not 52!

The Heart of the Problem – 3 not 1 mov g_counter %eax add 1 %eax mov %eax g_counter 3 operations instead of 1. What if had: Atomic action – can’t be interrupted  Seems simple. Problem solved! But … what if wanted to “subtract 1”, or “add 10”, or “atomic update of B-tree” Won’t be atomic instructions for everything! memory-add 0x201465 1

The Heart of the Solution Instead, provide synchronization primitives  Programmer can use for atomicity (and more) THE CRUX OF THE PROBLEM: HOW TO PROVIDE SUPPORT FOR SYNCHRONIZATION? What synchronization primitives should be provided? What support needed from hardware to build? How to make correct and efficient? How do programmers use them?

Useful Terms* Critical section – code that access shared resource (e.g., variable or data structure) Race condition – arises when multiple threads/processes simultaneously enter critical section leading to non-deterministic outcome Indeterminant program – program with 1+ race conditions, so output varies run to run Mutual exclusion – method to guarantee only 1 thread/process active in critical section at a time * That all good systems-programmers (you!) should know

Outline Introduction (done) Solutions (next) Classic Problems

Illustration of Critical Region What basic mechanism can stop B from entering critical region when A in? Hint: just need to block access

THE CRUX: HOW TO BUILD A LOCK? How to Use a Lock lock_t mutex; // globally-allocated ’mutex’ … lock(&mutex); x = x + 1; // critical region unlock(&mutex); pthread_mutex_t lock; pthread_mutex_lock(&lock); x = x + 1; // or general CR pthread_mutex_unlock(&lock); See: “thread-v1.c” THE CRUX: HOW TO BUILD A LOCK? How to build efficient lock? What hardware support is needed? What OS support?

Simple Lock Implementation - Disable Interrupts If no interrupts, no race condition void lock() { DisableInterrupts(); } void unlock() { EnableInterrupts(); Time What is the potential problem? Hint: consider all sorts of user programs

Many Problems with Disabling Interrupts in General Privileged operations, so must trust user code But may never unlock! (unintentional or malicious) Does not work for multiprocessors Second processor may still access shared resource When interrupts off, subsequent ones may become lost E.g., disk operations Disk Registers CPU1 CPU2 Mem

Lock Solution, Take 2 int mutex; // 0 -> lock available, 1 -> held void lock(int *mutex) { while (*mutex == 1) // TEST flag ; // spin-wait (do nothing) *mutex = 1; // now SET it! } void unlock(int *mutex) { *mutex = 0; This almost works … but not quite. Why not? Hint, has race condition - Can you spot it?

Lock Solution, Take 2 int mutex; // 0 -> lock available, 1 -> held void lock(int *mutex) { while (*mutex == 1) // TEST flag ; // spin-wait (do nothing) *mutex = 1; // now SET it! } void unlock(int *mutex) { *mutex = 0; This almost works … not quite… If can TEST mutex and SET it in atomic operation, would be ok But … aren’t back to square 1? No! Only need hardware support for 1 operation  build lock primitive

Synchronization Hardware – Test and Set Test-and-Set: returns and modifies atomically int TestAndSet(int *mutex) { int temp; temp = *mutex; *mutex = true; return temp; } Done with hardware support. All modern computers since 1960’s e.g., x86 has compare-and-exchange Others: compare-and-swap, fetch-and-add, … all atomic

Lock Solution, Take 3 Now, what is major remaining shortcoming? int mutex; // 0 -> lock available, 1 -> held void lock(int *mutex) { while (TestAndSet(mutex)) // 1 if held ; // spin-wait (do nothing) // once here, have lock! } void unlock(int *mutex) { *mutex = 0; Note, no need to protect unlock() (Exercise: why not?) Now, what is major remaining shortcoming? Hint: code works, but could be more efficient

Lock Solution, Take 4 int mutex; // 0 -> lock available, 1 -> held void lock(int *mutex) { while (TestAndSet(mutex)) { queueAdd(*mutex); park(); // put process to sleep } void unlock(int *mutex) { *mutex = 0; if (!queueEmpty(*mutex)) unpark(); // wake up process Note: almost right, but need to protect queue, too (see OSTEP, 28.14 for final touch)

Synchronization Primitive - Semaphore int sem_wait(sem_t &s) { s = s - 1 if (s < 0) add process to queue and sleep } int sem_post(sem_t &s) { s = s + 1 if (s <= 0) remove process from queue and wake “Special” integer, provided by OS Only accessible through two routines: sem_post() sem_wait() Both routines are atomic Operational Model value of counter = number of procs that may pass before closed counter <= 0  gate closed! blocked process "waits" in Q counter < 0  number of processes waiting in Q

How to Use a Semaphore semaphore mutex; // globally-allocated … wait(&mutex); x = x + 1; // critical region signal(&mutex); Easy, peasy! And available on most operating systems Can use for general synchronization problems (next)

Reducing Problem to Special Case SOS: Semaphore See: “semaphore.c” Design Technique Reducing Problem to Special Case Other examples: name servers, on-line help How does the OS protect access to the semaphore integer count? Previously said this was a bad idea … why is it ok in this context? How else might the OS protect this critical region? Challenge: Implement “attach” and “detach” functions /* Attach to OS semaphore */ int AttachSemaphore(int key); /* Deattach from sem id */ int DetachSemaphore(int sid);

Other Synchronization Primitives Monitors Condition Variables Events … Execise: learn on own Fortunately, if have one (e.g,. Lock) can build others

Outline Introduction (done) Solutions (done) Classic Problems (next) Dining Philosophers Readers-Writiers

Dining Philosophers Philosophers Need 2 chopsticks to eat Think Sit

Dining Philosophers Solutions? Philosopher i: while (1) { /* think… */ wait(chopstick[i]); wait(chopstick[i+1 % 5]); /* eat */ signal(chopstick[i]); signal(chopstick[i+1 % 5]); } For 5 Philosophers This almost works, but not quite. Why not? Solutions?

Dining Philosopher Solutions Allow at most N-1 to sit at a time Allow to pick up chopsticks only if both are available Asymmetric solution (odd L-R, even R-L)

Readers-Writers Readers only read the content of object shared resource Readers only read the content of object Writers read and write the object Critical region, one of: No processes One or more readers (no writers) One writer (nothing else)

Readers-Writers Shared: semaphore mutex; semaphore wrt; int readcount; wait(wrt) /* write stuff */ signal(wrt); Reader: wait(mutex); readcount = readcount + 1; if (readcount==1) wait(wrt); signal(mutex); /* read stuff */ readcount = readcount - 1; if (readcount==0) signal(wrt); Solution “favors” readers. Can you see why?

Other Classic Problems Bounded Buffer Sleeping Barber Bakery Algorithm Cigarette smokers … If can model your problem as one of the above  Solution Akin to Software Design Patterns

Outline Introduction (done) Solutions (done) Classic Problems (done)