Presentation is loading. Please wait.

Presentation is loading. Please wait.

Atomic Operations David Monismith cs550 Operating Systems.

Similar presentations


Presentation on theme: "Atomic Operations David Monismith cs550 Operating Systems."— Presentation transcript:

1 Atomic Operations David Monismith cs550 Operating Systems

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

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

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

5 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”

6 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

7 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

8 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(); }

9 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

10 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

11 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

12 Monitors (Java) See example code on the class website

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

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

15 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

16 Example For an example see https://computing.llnl.gov/tutorials/pthreads/ #Mutexes https://computing.llnl.gov/tutorials/pthreads/ #Mutexes


Download ppt "Atomic Operations David Monismith cs550 Operating Systems."

Similar presentations


Ads by Google