CS GRIND

CSC Notes Chapter 6 Process Synchronization

Chapter Objective

  • To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data.
  • To present both software and hardware solutions to the critical-section problem.
  • To introduce the concept of an atomic transaction and describe mechanisms to ensure atomicity.

The Critical-Section Problem

Critical section: the part of codes in which process may be changing common variables, updating a table, writing a file, and so on. Each process must request permission to enter its critical section. The section of code implementing this request is the entry section. The critical section may be followed by an exit section. The remaining code is the remainder section. Requirements:

Two general approaches are used to handle critical sections in operating systems:

Peterson's Solution

Peterson’s solution is restricted to two processes that alternate execution between their critical sections and remainder sections.

Synchronization Hardware

Semaphores

Implementation

The main disadvantage of the semaphore definition just described is that it requires busy waiting. A semaphore that produces this result is also called a spinlock, because the process “spins” while waiting for the lock.

The critical aspect of semaphores is that they be executed atomically, which can be solved in one of two ways:

Deadlocks and Starvation

two or more processes are waiting indefinitely for an event that can be caused only by one of the waiting processes.

Priority Inversion

Low priority process interfere high priority process because the high priority is waiting a lower priority process to finish and it has been preempted by the low priority process.

Solution:

Classic Problems of Synchronization

The Bounded-Buffer Problem

The Readers-Writers Problem

The Dining-Philosophers Problem

Monitors

Java Synchronization

Multiple Notifications

Block Synchronization

Synchronization Rules

  1. A thread that owns the lock for an object can enter another synchronized method (or block) for the same object. This is known as a recursive or reentrant lock.
  2. A thread can nest synchronized method invocations for different objects. Thus, a thread can simultaneously own the lock for several different objects.
  3. If a method is not declared synchronized, then it can be invoked regardless of lock ownership, even while another synchronized method for the same object is executing.
  4. If the wait set for an object is empty, then a call to notify() or notifyAll() has no effect.
  5. wait(), notify(), and notifyAll() may only be invoked from synchronized methods or blocks; otherwise, an IllegalMonitorStateException is thrown.

It is also possible to declare static methods as synchronized. This is because, along with the locks that are associated with object instances, there is a single class lock associated with each class.

Handling InterruptedException

Concurrency Features in Java

Atomic Transactions

System Model

Transactional Memory

A memory transaction is a sequence of memory read–write operations that are atomic. If all operations in a transaction are completed, the memory transaction is committed; otherwise, the operations must be aborted and rolled back. The benefits of transactional memory can be obtained through features added to a programming language.

Log-Based Recovery

Checkpoints

Concurrent Atomic Transactions