1 / 36

Chapter 6: Real World Concurrency Techniques

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

dezso
Download Presentation

Chapter 6: Real World Concurrency Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6: Real World Concurrency Techniques

  2. UNIX Concurrency Mechanisms UNIX provides a variety of mechanisms for interprocessor communication and synchronization including:

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

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

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

  6. 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,

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

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

  9. Semaphores • Generalization of the semWaitand semSignalprimitives • no other process may access the semaphore until all operations have completed

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

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

  12. UNIX Signals

  13. Linux Kernel Concurrency Mechanism Includes all the mechanisms found in UNIX plus:

  14. Atomic Operations Atomic operations execute without interruption and without interference Simplest of the approaches to kernel synchronization Two types:

  15. Linux Atomic Operations

  16. 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:

  17. 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) */

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

  19. Linux Spinlocks

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

  21. Linux Semaphores

  22. Barriers Table 6.6 Linux Memory Barrier Operations enforce the order in which instructions are executed

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

  24. Solaris Synchronization Primitives

  25. Solaris Data Structures

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

  27. Semaphores

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

  29. Condition Variables

  30. Windows 7 Concurrency Mechanisms Windows provides synchronization among threads as part of the object architecture

  31. Wait Functions

  32. Table 6.7 Windows Synchronization Objects

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

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

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

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

More Related