Atomic Operations David Monismith cs550 Operating Systems.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems Part III: Process Management (Process Synchronization)
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Chapter 6 Process Synchronization: Part 2. Problems with Semaphores Correct use of semaphore operations may not be easy: –Suppose semaphore variable called.
1 Semaphores and Monitors: High-level Synchronization Constructs.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
CS Introduction to Operating Systems
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Programming with POSIX* Threads Intel Software College.
Internet Software Development Controlling Threads Paul J Krause.
Race condition The scourge of parallel and distributed computing...
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
PARALLEL PROGRAM CHALLENGES
Principles of Operating Systems Lecture 11
Operating Systems CMPSC 473
Monitors.
Chapter 5: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Lecture 14: Pthreads Mutex and Condition Variables
Semaphore Originally called P() and V() wait (S) { while S <= 0
Another Means Of Thread Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6 Synchronization Principles
Lecture 14: Pthreads Mutex and Condition Variables
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
“The Little Book on Semaphores” Allen B. Downey
CSE 542: Operating Systems
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Atomic Operations David Monismith cs550 Operating Systems

How Atomic Operations Work Atomic transactions Multiple related data values may need to be modified in a critical section. Modification must take place as one step.

Example Move $ from bank account 1 to bank account 2 Solution: – Lock accounts Decrement Account 1 balance Increment Account 2 balance – Unlock accounts

What if? A system failure occurs? Need failure recovery system! Either resume the transaction or restart it Need either "checkpoint restart" or "transaction logging"

Checkpoint-Restart Requires recording checkpoints Resume system from saved state Must be performed frequently to prevent loss of many transactions Used on many supercomputing systems – DMTCP – BLCR – “Homegrown”

Transaction Logging Write details of step to a log – Use log to undo steps after incomplete transactions Example: Begin transaction – Decrement account 1 by $100 giving balance xxx – Increment acct 2 by $200 giving balance yyy End transaction Use a database to record all transactions

Atomic Operations Want operations to occur as one step Some assembly languages include atomic operations These may be implemented/used by the OS, too Java provides atomic operations as do some Linux distributions

Example class AtomicCounter { private AtomicInteger c = new AtomicInteger(0); public void increment() { c.incrementAndGet(); } public void decrement() { c.decrementAndGet(); } public int getValue() { return c.get(); }

Monitors Two definitions of a monitor exist – A class/program level synchronization tool – A condition variable (i.e. a mutual exclusion lock that may be conditionally unlocked) that has multiple queues Class/program level example – Use synchronized keyword in Java – Use wait, notify, and notifyAll methods – Only one per class

Monitors Monitors and condition variables may have multiple queues – Entry queue – One or more condition queues each corresponding to a condition variable – Similar to a semaphore with operations including wait and signal – Processes invoking wait on a condition variable x put themselves on the condition queue for x and release mutual exclusion – Processes invoking a signal on a condition variable x reactivate one waiting process from the condition queue for x. The current process releases mutual exclusion and exits the monitor. – The reactivated process re-evaluates the condition variable and continues execution – A process that completes execution inside the monitor releases mutual exclusion and exits

Monitors (Java) Note that it is not possible for two invocations of a synchronized method to interleave. Other threads will block for the same object until one is complete When exits, establishes a "happens before" relationship with any Subsequent invocation of a synchronized method for the same object Can't have synchronized constructor

Monitors (Java) See example code on the class website

Condition Variables in C C condition variables require a mutex lock to work mutex “objects” in C A mutex “object” is of type pthread_mutex_t Such an “object” has similar properties to a binary semaphore – It can be initialized – It can be used to “lock” one shared resource so that only one thread may use it at a time Declaration and initialization: –pthread_mutex_t my_mutex; –pthread_mutex_init(pthread_mutex_t * mutex, pthread_mutexattr_t * attr);

Condition Variables in C To lock the mutex use: –pthread_mutex_lock(&my_mutex); To unlock the mutex use: –pthread_mutex_unlock(&my_mutex); To test the mutex use: –pthread_mutex_trylock(&my_mutex); To destroy use: –pthread_mutex_destroy(&my_mutex);

Condition Variables in C To add a condition variable to a mutex: – Create an “object” of type pthread_cond_t Initialize this with pthread_cond_init Wait on the variable with pthread_cond_wait Signal the variable with pthread_cond_signal Broadcast to all threads using the variable with pthread_cond_broadcast

Example For an example see #Mutexes #Mutexes