A. Frank - P. Weisberg
Introduction to Concurrency
Classical Problems of Concurrency Critical Regions Monitors Inter-Process Communication (IPC) Communications in Client-Server Systems
A. Frank - P. Weisberg
A. Frank - P. Weisberg
region buffer when (counter > 0) { nextc = pool[out]; out = (out+1) % n; counter--; }
7
A. Frank - P. Weisberg
A. Frank - P. Weisberg
Monitors (1)
A high-level abstraction that provides a convenient and effective mechanism for process synchronization. Only one process may be active within the monitor at a time. There are many kinds. Found in many concurrent programming languages:
Concurrent Pascal, Modula-3, uC++, Java...
9
Characteristics:
local variables accessible only by monitors procedures. a process enters the monitor by invoking one of its procedures. only one process can be in the monitor at any A. Frank - P. Weisberg one time.
10
Monitor Layout
monitor monitor-name { shared variable declarations procedure body P1 () { . . . } procedure body P2 () { . . . } ... procedure body Pn () { . . . } ... { initialization code } }
11
A. Frank - P. Weisberg
12
A. Frank - P. Weisberg
Monitor Example
13
A. Frank - P. Weisberg
Monitor Characteristics
The monitor ensures mutual exclusion: no need to program this constraint explicitly. Hence, shared data are protected by placing them in the monitor.
The monitor locks shared data on process entry.
Process synchronization is done by the programmer by using condition variables that represent conditions a process may need to wait for before executing in the monitor.
14
A. Frank - P. Weisberg
17
A. Frank - P. Weisberg
18
19
Producer/Consumer Problem
Two types of processes:
producers consumers
Synchronization is now confined within the monitor. append() and take() are procedures within the monitor: the only means by which P/C can access the buffer. If these procedures are correct, synchronization will be correct for all participating processes.
20
A. Frank - P. Weisberg
ProducerI: repeat produce v; append(v); forever ConsumerI: repeat take(v); consume v; forever
21
A. Frank - P. Weisberg
22
23
24
27
A. Frank - P. Weisberg
28
29
A. Frank - P. Weisberg
Solaris Synchronization
Implements a variety of locks to support multitasking, multithreading (including real-time threads), and multiprocessing. Uses adaptive mutexes for efficiency when protecting data from short code segments. Uses condition variables and readers-writers locks when longer sections of code need access to data. Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock.
30
A. Frank - P. Weisberg
Windows XP Synchronization
Uses interrupt masks to protect access to global resources on uniprocessor systems. Uses spinlocks on multiprocessor systems. Also provides dispatcher objects which may act as wither mutexes and semaphores. Dispatcher objects may also provide events; An event acts much like a condition variable.
31
A. Frank - P. Weisberg
Linux provides:
semaphores spin locks
32
A. Frank - P. Weisberg