Synchronization CS470 -- Spring 2002.

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

Operating Systems Part III: Process Management (Process Synchronization)
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
Concurrency: mutual exclusion and synchronization Slides are mainly taken from «Operating Systems: Internals and Design Principles”, 8/E William Stallings.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Concurrency.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Chapter 2.3 : Interprocess Communication
Synchronization Solutions
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
Concurrency, Mutual Exclusion and Synchronization.
Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Process Synchronization Presentation 2 Group A4: Sean Hudson, Syeda Taib, Manasi Kapadia.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
CSE 120 Principles of Operating
G.Anuradha Reference: William Stallings
Concurrency.
Concurrency: Mutual Exclusion and Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Synchronization and Semaphores
Critical section problem
Background and Motivation
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: 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
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Chapter 5 Mutual Exclusion(互斥) and Synchronization(同步)
Presentation transcript:

Synchronization CS470 -- Spring 2002

Overview Concurrency Problems and Mutual Exclusion Requirements Software methods with busy-wait Hardware Support for Mutual Exclusion Software methods without busy-wait Semaphores Message Passing

Concurrency Problems Interleaved & Overlapped computation - both exhibit similar problems Ordering access to global resources - leads to reservation of resources Reservation of resources leads to inefficient use. Lack of reproducibility makes debugging and testing difficult.

Example int GetTicket(void) { } static customerNbr; return customerNbr++; } Fails to give sequential unduplicated tickets

Vocabulary Mutual Exclusion protects a critical resource by allowing no more than one thread at a time to execute in a critical section of code which handles that resource. Mutual exclusion can lead to deadlock or starvation

Win32 Critical Sections Works for threads in same process CRITICAL_SECTION data structure InitializeCriticalSection (&cs) or InitializeCriticalSectionAndSpinCount( &cs, cnt) EnterCriticalSection (&cs) or TryEnterCriticalSection(&cs) LeaveCriticalSection(&cs) Implemented with a semaphore

Mutual Exclusion Requirements Mutual exclusion - at most one thread at a time can be in the critical section Threads not in the critical section and not trying to enter it cannot interfere with those trying to enter it No deadlock or starvation possible if no thread dallies in the critical section

Software Approaches Standard programming methods are both difficult to code and error prone Some failed approaches Dekker’s Algorithm Peterson’s Algorithm All make inefficient use of processor because they spin in busy-wait loops.

Attempt 1 - Taking Turns BOOL turn = FALSE; DoThread(BOOL me) { DoNonCritical ( ); while (turn != me) ; DoCriticalSection( ); turn = !me; DoMoreNonCritical( ); } Does Mutual Exclusion without deadlock or starvation; but failure outside of critical section can prevent other thread from entering.

Attempt 2 - checking other BOOL inside[2] = {FALSE, FALSE}; DoThread(BOOL me) { DoNonCritical ( ); while (inside[!me]) ; inside[me] = TRUE; DoCriticalSection ( ); inside[me] = FALSE; DoMoreNonCritical ( ); } Does not guarantee mutual exclusion

Attempt 3 - early locking BOOL inside[2] = {FALSE, FALSE}; DoThread(BOOL me) { DoNonCritical ( ); inside[me] = TRUE; while (inside[!me]) ; DoCriticalSection ( ); inside[me] = FALSE; DoMoreNonCritical ( ); } Just swap two lines…. Does mutual exclusion; but can easily deadlock.

Attempt 4 - intermittant locks DoNonCritical ( ); do { inside[me] = FALSE; Sleep(DELAY); inside[me] = TRUE; } while (inside[!me]); DoCriticalSection ( ); DoMoreNonCritical ( ); Mutual exclusion is achieved; but starvation could result.

Peterson’s Algorithm BOOL turn, inside[2] = {FALSE, FALSE}; DoThread(BOOL me) { DoNonCritical ( ); inside[me] = TRUE; turn = !me; while (inside[!me] && turn != me) ; DoCriticalSection ( ); inside[me] = FALSE; DoMoreNonCritical ( ); } Simpler Algorithm

Hardware: Disabling Interrupts Method: DisableInterrupts( ); DoCriticalSection ( ); EnableInterrupts ( ); Based on thread context switch being triggered by (clock) interrupt Works only with uniprocessor Reduces total system concurrency Intel: cli and sti instructions Works for n threads

Hardware: Test and Set Special atomic instruction: BOOL TestSet (BOOL *bitPtr) { BOOL ret = *bitPtr; *bitPtr = TRUE; return ret; } Intel 386: lock bts mem, reg/imm where 2nd operand is bit offset in first operand

Using Test & Set BOOL bit; DoThread (void *c ) { DoNonCritical ( ); while (TestSet(&bit)) ; DoCriticalSection ( ); bit = FALSE; DoMoreNonCritical ( ); } Starvation could occur; but works for n threads.

Hardware: Exchange Special Atomic Instruction: Exchange(BOOL *a, BOOL *b) { BOOL temp = *a; *a = *b; *b = temp; } Intel 386: lock xchg mem, reg

Using Exchange BOOL bit = FALSE; DoThread(void *c) { BOOL key; DoNonCritical( ); key = TRUE; do {Exchange(bit, key); } while (key); DoCriticalSection ( ); bit = FALSE; DoMoreNonCritical ( ); } Starvation is possible; but works for n threads.

Win32 Interlocked Operations Allows for atomic operations on DWORD by threads in same process or otherwise sharing memory DWORD target must be at even modulo four address on Intel multiprocessor machines

Interlocked Operation List InterlockedIncrement(PLONG) InterlockedDecrement(PLONG) InterlockedExchange(PLONG target, LONG value) InterlockedExchangeAdd(PLONG Addend, LONG increment) InterlockedExchange(PVOID dest, PVOID exchange, PVOID comperand)

Hardware Support Summary Advantages Applies to n threads Simple and easy to verify Different variables give different locks Disadvantages Busy-wait wastes cpu resources Starvation is possible Deadlock is possible -- e.g. waiting on lock held by lower priority process.

Counting Semaphores (1 of 2) struct semaphore { DWORD count; ThreadList tList; } void Wait(struct semaphore *s) { if (- - s  count < 0) { EnqueueAndBlock(self, s  tList); Wait is atomic.

Counting Semaphores (2 of 2) void Signal(struct semaphore *s) { if (++s  count <= 0) { MoveThreadToRunQueueFrom( s  tList); } Signal is atomic.

Binary Semaphores (1 of 2) struct BSemaphore { BOOL value; ThreadList tList; } void BWait(struct BSemaphore *s) { if (s  value == TRUE) { s  value = FALSE; } else { EnqueueAndBlock(self, s  tList); }} BWait is atomic.

Binary Semaphores (2 of 2) void BSignal(struct BSemaphore *s) { if (s  tList == NULL) { s  value = TRUE; } else { MoveThreadToRunQueueFrom( s  tList); } BSignal is atomic.

Using Semaphores struct semaphore s; DoThread(void *c) { DoNonCritical( ); Wait(&s); DoCriticalSection( ); Signal(&s); DoMoreNonCritical( ); } Could use Binary Semaphores. Could have multiple critical sections, etc. Use TestSet, etc. to implement in operating system.

Message Passing Message Operations Synchronization Send (destination, message) Receive (source, message) Synchronization Send - blocking or non-blocking Receive - blocking or non-blocking, may be able to check for arrived messages typical: non-blocking sends, blocking receives with arrival check

Message Addressing Direct Indirect via mailboxes Send has explicit address of addressee Receive explicit address of sender implicit, known only after receipt Indirect via mailboxes static via permanent ports dynamic with connect / disconnect Queuing - FIFO, message priorities

Mutual Exclusion via Messages Initialize mbox with 1 message DoThread(void *c) { DoNonCritical ( ); Receive( mbox, message); DoCriticalSection ( ); Send ( mbox, message); DoMoreNonCritical ( ); }

Producer / Consumer (1 of 2) Initialize mayproduce with n messages, mayconsume as empty Producer(void *c) { MESSAGE pmsg; while (TRUE) { receive(mayproduce, &pmsg); pmsg = Produce( ); send(mayconsume, &pmsg); } }

Producer / Consumer (2 of 2) Consumer(void *c) { MESSAGE cmsg; while (TRUE) { Receive(mayconsume, &cmsg); Consume(&cmsg); Send(mayproduce, &cmsg); } Allows multiple servers and clients