380 likes | 609 Views
Chapter 6: Real World Concurrency Techniques. UNIX Concurrency Mechanisms. UNIX provides a variety of mechanisms for interprocessor communication and synchronization including:. Pipes. Circular buffers allowing two processes to communicate on the producer-consumer model
E N D
UNIX Concurrency Mechanisms UNIX provides a variety of mechanisms for interprocessor communication and synchronization including:
Pipes • Circular buffers allowing two processes to communicate on the producer-consumer model • first-in-first-out queue, written by one process and read by another
Pipes The pipe is a fixed size (similar to finite-buffer P/C) The OS prevents a writer from writing into a full pipe & the reader from reading an empty pipe. Unnamed pipes are used by related processes (parent-child or siblings) whereas named pipes can be used by any process.
Messages A block of bytes with an accompanying type UNIX provides msgsndand msgrcvsystem calls for processes to engage in message passing Associated with each process is a message queue, which functions like a mailbox Receivers can retrieve messages in a FIFO manner or specify the message type Also resembles P/C problem, as messages arediscrete data units
Pipes & Messages Pipes enforce data access methods as in the Producer/Consumer problem, but treat the data as a byte stream Messages treat the data as a separate data items (similar to P/C) but the access method may not enforce the same FIFO discipline. Both pipes and message queues were designed to be used on a single computer,
Shared Memory Fastest form of interprocess communication Common block of virtual memory shared by multiple processes Permission is read-only or read-write for a process Mutual exclusion constraints are not part of the shared-memory facility but must be provided by the processes using the shared memory
Shared Memory UNIX system calls allow one process to create an area of shared memory and other processes to attach it to their address space. shared access to a single block of memory is enabled – the processes are responsible for providing mutual exclusion or any other kinds of synchronization.
Semaphores • Generalization of the semWaitand semSignalprimitives • no other process may access the semaphore until all operations have completed
Semaphores Sets of semaphores can be created, allowing multiple processes to wait on different semaphores if desired UNIX semaphores are more versatile that the traditional semaphore, but it is possible to use them exactly like the traditional wait and signal operations that we have discussed in class.
Signals • A software mechanism that informs a process of the occurrence of asynchronous events • similar to a hardware interrupt, but does not employ priorities • A signal is delivered by updating a field in the process table for the process to which the signal is being sent • A process may respond to a signal by: • performing some default action • executing a signal-handler function • ignoring the signal
Linux Kernel Concurrency Mechanism Includes all the mechanisms found in UNIX plus:
Atomic Operations Atomic operations execute without interruption and without interference Simplest of the approaches to kernel synchronization Two types:
Implementation • Linux atomic operations are generally implemented by using a special hardware instruction if available. • An alternative is to use an instruction that locks the memory bus to prevent concurrent execution for the duration of one instruction • Used to implement Linux semaphores or for simple operations such as the following:
Atomic Operation –Examplehttp://www.makelinux.net/books/lkd2/ch09lev1sec1 • Integer atomic ops must be performed on the atomic_T data type (not int) • Defining an atomic_t is done in the usual manner. Optionally, you can set it to an initial value: atomic_t v; /* define v */ atomic_tu = ATOMIC_INIT(0); /* define & init u */ Operations are all simple: atomic_set(&v, 4); /* v = 4 (atomically) */ atomic_add(2, &v); /* v = v + 2 = 6 (atomically) */ atomic_inc(&v); /* v = v + 1 = 7 (atomically) */
Spinlocks • A common technique for protecting a critical section in Linux • Can only be acquired by one thread at a time • any other thread will keep trying (spinning) until it can acquire the lock • Built on an integer location in memory that is checked by each thread before it enters its critical section • Effective in situations where the wait time for acquiring a lock is expected to be very short • Disadvantage: • locked-out threads continue to execute in a busy-waiting mode
Semaphores • User level: • Linux provides a semaphore interface corresponding to that in UNIX SVR4 • Three types of kernel semaphores: • binary semaphores • counting semaphores • reader-writer semaphores
Barriers Table 6.6 Linux Memory Barrier Operations enforce the order in which instructions are executed
Using Barriers • A barrier prevents reordering loads and stores across a barrier. Optimizing compilers may rearrange instructions so they aren’t in their original order. While this is not harmful on a single processor it can cause problems on SMPs • Processor #1: Processor #2: while (f == 0) ; x = 42; // barrier required here // barrier needed here print x; f = 1 • The print statement should always print the number "42"; but if processor #2's store operations are executed out-of-order, f could be updated before x, and the print statement might therefore print "0". • Or, processor #1's load operations are reordered and so that x is read before f is checked.http://en.wikipedia.org/wiki/Memory_barrier
Mutual Exclusion (MUTEX) Lock Used to ensure only one thread at a time can access the resource protected by the mutex The thread that locks the mutex must be the one that unlocks it A thread attempts to acquire a mutex lock by executing the mutex_enter primitive Default blocking policy is a spinlock An interrupt-based blocking mechanism is optional
Readers/Writer Locks • Allows multiple threads to have simultaneous read-only access to an object protected by the lock • Allows a single thread to access the object for writing at one time, while excluding all readers • when lock is acquired for writing it takes on the status of write lock • if one or more readers have acquired the lock its status is read lock
Windows 7 Concurrency Mechanisms Windows provides synchronization among threads as part of the object architecture
Critical Sections • Similar mechanism to mutex except that critical sections can be used only by the threads of a single process • If the system is a multiprocessor, the code will attempt to acquire a spin-lock • as a last resort (after some set time), if the spinlock cannot be acquired, a dispatcher object is used to block the thread so that the kernel can dispatch another thread onto the processor
Slim Read-Writer Locks Windows Vista added a user mode reader-writer The reader-writer lock enters the kernel to block only after attempting to use a spin-lock It is slim in the sense that it normally only requires allocation of a single pointer-sized piece of memory
Condition Variables • Windows also has condition variables • The process must declare and initialize a CONDITION_VARIABLE • Used with either critical sections or SRW locks • Used as follows: • acquire exclusive lock • while (predicate()==FALSE)SleepConditionVariable() • perform the protected operation • release the lock
Lock-free Synchronization • Windows also relies heavily on interlocked operations for synchronization (similar to Linux atomic operations) • interlocked operations use hardware facilities to guarantee that memory locations can be read, modified, and written in a single atomic operation