Escolar Documentos
Profissional Documentos
Cultura Documentos
n Java Synchronization n Solaris Synchronization n Windows XP Synchronization n Linux Synchronization n Pthreads Synchronization
7b.1
Java Synchronization
n Bounded Buffer solution using synchronized, wait(), notify()
statements
n Multiple Notifications n Block Synchronization n Java Semaphores n Java Monitors
7b.2
synchronized Statement
n Every object has a lock associated with it
n Calling a synchronized method requires owning the lock n If a calling thread does not own the lock (another thread already
owns it), the calling thread is placed in the wait set for the objects lock
n The lock is released when a thread exits the synchronized
method
7b.3
Entry Set
7b.4
7b.5
7b.6
the thread releases the object lock thread state is set to blocked thread is placed in the wait set
7b.7
7b.8
selects an arbitrary thread T from the wait set moves T to the entry set sets T to Runnable
7b.9
7b.10
7b.12
Multiple Notifications
n notify() selects an arbitrary thread from the wait set.
them in the entry set. This allows the threads to decide among themselves who should proceed next.
n notifyAll() is a conservative strategy that works best when
7b.13
acquireReadLock() Method
public synchronized void acquireReadLock() { while (dbWriting == true) { try { wait(); } catch(InterruptedException e) { } } ++readerCount; }
7b.15
releaseReadLock() Method
public synchronized void releaseReadLock() { --readerCount; // if I am the last reader tell writers // that the database is no longer being read if (readerCount == 0) notify(); }
7b.16
Writer Methods
public synchronized void acquireWriteLock() { while (readerCount > 0 || dbWriting == true) { try { wait(); } catch(InterruptedException e) { } } // once there are either no readers or writers // indicate that the database is being written dbWriting = true; } public synchronized void releaseWriteLock() { dbWriting = false; notifyAll(); }
Operating System Concepts with Java 7b.17 Silberschatz, Galvin and Gagne 2003
Block Synchronization
n Scope of lock is time between lock acquire and release n Blocks of code rather than entire methods may be declared
as synchronized
n This yields a lock scope that is typically smaller than a
synchronized method
7b.18
7b.19
Java Semaphores
n Java does not provide a semaphore, but a basic semaphore can
7b.20
10
Semaphore Class
public class Semaphore { private int value; public Semaphore() { value = 0; } public Semaphore(int value) { this.value = value; }
7b.21
7b.22
11
Syncronization Examples
n Solaris n Windows XP n Linux n Pthreads
7b.23
Solaris Synchronization
n Implements a variety of locks to support multitasking,
7b.24
12
Windows XP Synchronization
n Uses interrupt masks to protect access to global resources on
uniprocessor systems
n Uses spinlocks on multiprocessor systems n Also provides dispatcher objects which may act as either
7b.25
Linux Synchronization
n Linux:
l disables interrupts to implement short critical sections
n Linux provides:
l semaphores l spin locks
7b.26
13
Pthreads Synchronization
n Pthreads API is OS-independent n It provides:
l mutex locks l condition variables
7b.27
14