Docsity

Synchronization Techniques in Concurrent Programming: Semaphores and Monitors, Papers of Programming Languages

Partial preview of the text

Download Synchronization Techniques in Concurrent Programming: Semaphores and Monitors and more Papers Programming Languages in PDF only on Docsity!

1

Programming Languages

Tevfik Koşar

Lecture - XXVII May 2nd, 2006

2

Roadmap

  • Shared Memory
    • Cigarette Smokers Problem
    • Monitors
  • Message Passing
    • Cooperative Operations
    • Synchronous and Asynchronous Sends
    • Blocking and Non-blocking Operations
    • Collective Communications

3

Cigarette Smokers Problem

  • Assume a cigarette requires three ingredients to smoke:
    • Tobacco
    • Paper
    • Match
  • Assume there are also three smokers around a table, each of whom has an infinite supply of one of the three ingredients
  • Assume there is also a non-smoking arbiter. The arbiter enables the smokers to make their cigarettes by - selecting two of the smokers at random, - taking one item out of each of their supplies, - and placing the items on the table. - He then notifies the third smoker that he has done this.
  • The third smoker removes the two items from the table and uses them (along with his own supply) to make a cigarette, which he smokes for a while.
  • Meanwhile, the arbiter, seeing the table empty, again chooses two smokers at random and places their items on the table. This process continues forever.

4

Solution

  • Let us define an array of binary semaphores A, one for each smoker; and a binary semaphore for the table, T.
  • Initialize the smokers' semaphores to zero and the table's semaphore to 1.
  • Then the arbiter's code is :

while true {

wait(T); choose smokers i and j randomly, making the third smoker k signal(A[k]);

}

7

Monitors

  • Suggested by Dijkstra (‘72)
  • Formulized by Hoare (’74)
  • Monitors encapsulate all synchronization code into a

single structure (like objects in OOP)

  • Only one operation of a given monitor is allowed to be

active at a given time (ensured by the compiler)

  • A thread which calls a busy monitor is automatically

delayed until the monitor is free

  • No need for P and V, and correctly ordering them

8

Monitors

A monitor consists of:

  • a set of procedures that allow interaction with the shared resource
  • a mutual exclusion lock
  • the variables associated with the resource
  • a monitor invariant that defines the assumptions needed to avoid race conditions

A monitor procedure takes the lock before doing anything else, and holds it until it either finishes or waits for a condition

If every procedure guarantees that the invariant is true before it releases the lock, then no task can ever find the resource in a state that might lead to a race condition.

9

Monitors

As a simple example, consider a monitor for performing transactions on a bank account.

monitor account { int balance := 0

function withdraw(int amount) { if amount < 0 then error "Amount may not be negative" else if balance < amount then error "Insufficient funds" else balance := balance - amount }

function deposit(int amount) { if amount < 0 then error "Amount may not be negative" else balance := balance + amount } }

10

Dining Philosophers using Monitors

class Monitor{ boolean forks[]; public Monitor(int size) { forks=new boolean[size]; for(int i=0;i<forks.length;i++) forks[i]=true; } public synchronized void getForks(int id) throws Exception{ int index1; int index2; if(id==0) // Philosopher 0 { index1=0; index2=forks.length-1; } else { index1=id; index2=id-1; } while(!forks[index1] || !forks[index2]) wait(); forks[index1]=false; forks[index2]=false; System.out.println("Philospher "+id+" got the forks: "+index1+" "+index2); }

13

Distributed Memory

Task 1 (^) Task 2 Task 3

Memory 1 Memory 2 (^) Memory 3

  • Each process/task has access to its own memory

•Processes/Tasks can communicate via messages

14

Message Passing

  • What is message passing?
    • A mechanism for communicating data between processes
    • In point-to-point communication, one process sends data and another process receives data
    • In collective communication, there may be more than one sending and/or receiving process
  • Message passing is important in many areas:
    • parallel computing
    • distributed computing
    • operating systems

15

What is message passing?

  • Data transfer plus synchronization
  • Requires cooperation of sender and receiver

Process 0 Data

Process 1

May I Send?

Yes

Data Data Data Data Data Data Data Data

Time

16

Cooperative Operations for Communication

  • The message-passing approach makes the exchange of data cooperative.
  • Data is explicitly sent by one process and received by another.
  • An advantage is that any change in the receiving process’s memory is made with the receiver’s explicit participation.
  • Communication and synchronization are combined.

Process 0 (^) Process 1 Send(data) Receive(data)

19

Messages

20

Message Passing Paradigms

  • MPI (Message Passing Interface)
    • aims to develop a standard for writing message passing programs
    • designed for homogeneous architectures
  • PVM (Parallel Virtual Machine)
    • allows a heterogeneous network of computers (parallel, vector, or serial) to appear as a single concurrent computational resource - a virtual machine.
  • Both provides libraries for C and Fortran programs

21

Synchronous Sends

22

Blocking Operations

25

Non-blocking Operations (cont.)

26

Collective Communications

27

Broadcast

MPI_BCast(…);

28

Reduction Operations

MPI_Reduce(…);

31

Scatter

32

Gather