Achieving Synchronization or How to Build a Semaphore

Similar documents
Synchronization Spinlocks - Semaphores

Process/Thread Synchronization

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Process/Thread Synchronization

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

CS420: Operating Systems. Process Synchronization

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Chapter 6: Process Synchronization

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

CS370 Operating Systems

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

CS3733: Operating Systems

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Lecture 6. Process Synchronization

Process Coordination

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization!

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Process Synchronization

CS370 Operating Systems

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Introduction to OS Synchronization MOS 2.3

Lesson 6: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

CHAPTER 6: PROCESS SYNCHRONIZATION

Synchronization Principles

Process Synchronization

Locks. Dongkun Shin, SKKU

Comp 310 Computer Systems and Organization

Chapter 7: Process Synchronization. Background

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Process Synchronization

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Chapter 5: Process Synchronization

Process Synchronization

Interprocess Communication By: Kaushik Vaghani

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Synchronization for Concurrent Tasks

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Chapter 6: Process Synchronization

Synchronization Principles I

Process Synchronization(2)

Using Semaphores CS 241. March 14, University of Illinois

Process Synchronization

Introduction to Operating Systems

Synchronization I. Jo, Heeseung

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Mutual Exclusion and Synchronization

Process Synchronization(2)

Chapter 6 Synchronization

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Chapter 6 Process Synchronization

Dept. of CSE, York Univ. 1

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Module 6: Process Synchronization

IV. Process Synchronisation

Process Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

High-level Synchronization

Process Synchronization(2)

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Concurrency: Mutual Exclusion and

Chapter 5: Process Synchronization

PROCESS SYNCHRONIZATION

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Lecture 5: Synchronization w/locks

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Process Synchronization Mechanisms

Dealing with Issues for Interprocess Communication

Chapter 5: Process Synchronization

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

1. Motivation (Race Condition)

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Resource management. Real-Time Systems. Resource management. Resource management

Last class: Today: CPU Scheduling. Start synchronization

Process Synchronization

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Process Co-ordination OPERATING SYSTEMS

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

Operating Systems CMPSC 473. Synchronization February 21, Lecture 11 Instructor: Trent Jaeger

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

CSC501 Operating Systems Principles. Process Synchronization

CMSC421: Principles of Operating Systems

Synchronization: Advanced

CS 25200: Systems Programming. Lecture 26: Classic Synchronization Problems

Transcription:

Achieving Synchronization or How to Build a Semaphore CS 241 March 12, 2012 Copyright University of Illinois CS 241 Staff 1

Announcements MP5 due tomorrow Jelly beans... Today Building a Semaphore If time: A few midterm problems 2

Review: Semaphores Problem: coordinating simultaneous access to shared data int cnt = 0; Shared data void * worker( void *ptr ) { int i; for (i = 0; i < ITERATIONS_PER_THREAD; i++) cnt++; Critical section (just one line in this simple example) Solution: mutually exclusive access to critical region Only one thread/process accesses shared data at a time 3

Semaphores for mutual exclusion Basic idea Associate a unique semaphore mutex, initially 1, with each shared variable (or related set of shared variables) Surround corresponding critical sections with wait(mutex) and post(mutex) operations. Terminology Binary semaphore: semaphore whose value is always 0 or 1" Mutex: binary semaphore used for mutual exclusion wait operation: locking the mutex post operation: unlocking or releasing the mutex Holding a mutex: locked and not yet unlocked Counting semaphore: used to count a set of available resources 4

goodcounter.c: good synchronization #include <semaphore.h>... int cnt = 0; sem_t cnt_mutex; int main(void) {... /* Initialize mutex */ sem_init(&cnt_mutex, 0, 1);... void * worker( void *ptr ) { int i; for (i = 0; i < ITERATIONS_PER_THREAD; i++) { sem_wait(&cnt_mutex); cnt++; sem_post(&cnt_mutex); Necessary include Declare mutex Initialize to 1 Surround critical section 5

How do we build mutual exclusion? lock(); critical_section(); unlock(); What goes here? Assumption for remainder of lecture: Above code is run simultaneously in multiple threads/processes 6

Mutual Exclusion Solutions Software-only candidate solutions Lock variables Turn Two flag and turn Hardware solutions Test-and-set / swap Semaphores 7

Lock Variables int lock = 0;... while (lock) { /* spin spin spin spin */ lock = 1; critical_section(); lock = 0; 8

Lock Variables int lock = 0;... while (lock) { /* spin spin spin spin */ lock = 1; lock = 1 lock = 0 lock = 1 lock = 1 critical_section(); lock = 0; No mutual exclusion! 9

Turn-based mutual exclusion pthread_t turn = first_thread_id;... while (turn!= my_thread_id) { /* wait your turn */ critical_section(); turn = other_thread_id; 10

Turn-based mutual exclusion pthread_t turn = first_thread_id; Process 0 Process 1... while (turn!= my_thread_id) { turn = 0 turn = 1 /* wait your turn */ critical_section(); turn = other_thread_id; No progress! Other process may not be executing in this critical section. 11

Two Flag and Turn Mutual Exclusion int owner[2]={false, false; int turn; owner[my_process_id] = true; turn = other_process_id; while (owner[other_process_id] && turn == other_process_id) { /* wait your turn */ owner[0] = false owner[1] = false turn = 0 1 0 true true critical_section(); owner[my_process_id] = false; Progress & mutual exclusion! Peterson s solution 12

Are we done? Peterson s algorithm works, but... Problem: software solutions can be slow at just the moment we d like to be fast: contention for shared resource Solution: hardware support 13

Atomic Test and Set Instruction boolean test_and_set(boolean* lock) atomic { boolean initial = *lock; *lock = true; return initial; atomic = executed without interruption 14

Test and Set for mutual exclusion boolean lock = 0; while (test_and_set(&lock)) ; critical_section(); lock = 0; 15

Understanding Test and Set Original boolean test_and_set(boolean* lock) atomic { boolean initial = *lock; *lock = true; return initial; Functionally equivalent version boolean test_and_set(boolean* lock) atomic { if (*lock == 1) return 1; // failure else { *lock = 1; return 0; // success 16

Test and Set for mutual exclusion boolean lock = 0; while (test_and_set(&lock)) ; critical_section(); lock = 0; Remaining problem: busy-waiting 17

Now are we done? Hardware solutions are fast, but... Problem: starvation No guarantee about which process wins the test-and-set race It ll eventually happen, but a process could wait indefinitely Problem: deadlock Proc. 1 enters critical section, gets interrupted by higher priority Proc. 2 P1 can t make progress: waiting to run until P2 is done P2 can t make progress: busy-waiting until P1 exits critical section Problem: busy-waiting Critical section might be arbitrarily long Waiting processes all still spend CPU time! These problems occur for software solutions too Solution: Semaphores 18

Semaphores vs. Test and Set Semaphore semaphore s = 1;... sem_wait(&s); critical_section(); sem_post(&s); Test and Set lock = 0;... while(test_and_set(&lock) ; critical_section(); lock = 0; The magic: avoid busy-waiting during sem_wait() 19

Inside a Semaphore Add a waiting queue Multiple process waiting on s Wake up one of the blocked processes upon getting a signal Semaphore data structure typedef struct { int count; queue_t waiting; semaphore_t; 20

Binary Semaphores typedef struct bsemaphore { enum {0,1 value; queue_t queue; bsem_t; void sem_wait_b (bsem* s) { if (s.value == 1) else { s.value = 0; place current process in s->queue; block current process; 21

Binary Semaphores typedef struct bsemaphore { enum {0,1 value; queue_t queue; bsem_t; void sem_post_b (bsem* s) { if (s->queue is empty()) s->value = 1; else { remove process P from s->queue; place P on ready list; 22

General Semaphore typedef struct { int count; queue_t queue; semaphore_t; void sem_wait(semaphore_t* s) { s.count--; if (s.count < 0) { place P in s->queue; block P; void semsignal(semaphore_t* s) { s.count++; if (s.count 0) { remove P from s.queue; place P on ready list; 23

Making the operations atomic Isn t this exactly the problem semaphores were trying to solve? Are we stuck??! Solution: resort to test and set: typedef struct { boolean lock; int count; queuetype queue; semaphore_t; void sem_wait(semaphore_t* s) { while (test_and_set(lock)) { s.count--; if (s.count < 0) { place P in s.queue; block P; lock = 0; 24

Making the operations atomic Busy-waiting again! How are semaphores better than just test-and-set? T&S: Busy-wait until ready to run Could be arbitrarily long! We re waiting for other processes which may be in long critical sections Semaphores: Busy-wait just during sem_wait, sem_post Now we re waiting for other processes which are doing very short operations (sem_wait, sem_post) 25

Summary Mutual exclusion possible in software Mutual exclusion faster in hardware Common atomic instruction: test_and_set Hardware operations used to bootstrap semaphores...which block processes to avoid busy-waiting and can select which ones to un-block Next time: Classic applications of mutual exclusion 26