Docsity

CS 361 Concurrent Programming Lecture 14: Database Reader-Writer Problem and Solutions - P, Study notes of Computer Science

Toggle sidebar
bg1
1
page 1
CS 361 Concurrent programming
Drexel University
Fall 2004
Lecture 14
©Bruce Char and Vera Zaychik. All rights reserved by the author.
Permission is given to students enrolled in CS361 Fall 2004to
reproduce these notes for their own use.
page 2
Database reader/writer problem
• The readers and writers problem: readers may read a
database simultaneously as long as no writer writes
• Only one writer at a time may write (if there are no active
readers).
• Attain: maximal parallelism (readers may read
simultaneously if there is no writing), data integrity (only
one writer at a time if no readers are reading).
• Possible additional goal: avoid writer starvation (locked
out of writing because readers are always coming along to
read).
page 3
Database reader/writer solution
import Utilities.*;
import Synchronization.*;
class Database extends MyObject {
private int numReaders = 0;
private BinarySemaphore mutex = new B inarySemaphore(1);
private BinarySemaphore ok = new Bina rySemaphore(1);
public Database() { super("Database") ; }
page 4
Reader code
public void startRead(int i) {
P(mutex);
numReaders++;
if (numReaders == 1) {
System.out.println("age=" + age() + " reader " + i
+ " waiting to read, numReaders=" + numReaders);
P(ok);
}
System.out.println(" age=" + age() + " reader " + i
+ " has begun reading, numReaders=" + numReaders);
V(mutex);
}
public void endRead(int i) {
P(mutex);
numReaders--;
System.out.println(" age=" + age() + " reader " + i
+ " finished reading, numReaders=" + numReaders);
if (numReaders == 0) V(ok);
V(mutex);
}
page 5
Writer code
public void startWrite(int i) {
P(ok);
System.out.println(" age=" + age() + " WRITER " + i
+ " has begun Writing");
}
public void endWrite(int i) {
System.out.println(" age=" + age() + " WRITER " + i
+ " has finished Writing");
V(ok);
}
}
page 6
Notes on reader/writer solution
– Only the first reader should lock the “ok” lock.
The last reader should unlock it when it finishes
its read. Then the
– Writers can write. Starvation of writers might
occur.
pf3

Partial preview of the text

Download CS 361 Concurrent Programming Lecture 14: Database Reader-Writer Problem and Solutions - P and more Study notes Computer Science in PDF only on Docsity!

page 1

CS 361 Concurrent programming

Drexel University

Fall 2004

Lecture 14

©Bruce Char and Vera Zaychik. All rights reserved by the author.

Permission is given to students enrolled in CS361 Fall 2004 to

reproduce these notes for their own use.

page 2

Database reader/writer problem

  • The readers and writers problem: readers may read a

database simultaneously as long as no writer writes

  • Only one writer at a time may write (if there are no active

readers).

  • Attain: maximal parallelism (readers may read

simultaneously if there is no writing), data integrity (only

one writer at a time if no readers are reading).

  • Possible additional goal: avoid writer starvation (locked

out of writing because readers are always coming along to

read).

page 3

Database reader/writer solution

import Utilities.; import Synchronization.;

class Database extends MyObject {

private int numReaders = 0; private BinarySemaphore mutex = new BinarySemaphore(1); private BinarySemaphore ok = new BinarySemaphore(1);

public Database() { super("Database"); }

page 4

Reader code

public void startRead(int i) { P(mutex); numReaders++; if (numReaders == 1) { System.out.println("age=" + age() + " reader " + i

  • " waiting to read, numReaders=" + numReaders); P(ok); } System.out.println(" age=" + age() + " reader " + i
  • " has begun reading, numReaders=" + numReaders); V(mutex); }

public void endRead(int i) { P(mutex); numReaders--; System.out.println(" age=" + age() + " reader " + i

  • " finished reading, numReaders=" + numReaders); if (numReaders == 0) V(ok); V(mutex); }

page 5

Writer code

public void startWrite(int i) { P(ok); System.out.println(" age=" + age() + " WRITER " + i

  • " has begun Writing"); }

public void endWrite(int i) { System.out.println(" age=" + age() + " WRITER " + i

  • " has finished Writing"); V(ok); } }

page 6

Notes on reader/writer solution

– Only the first reader should lock the “ok” lock.

The last reader should unlock it when it finishes

its read. Then the

– Writers can write. Starvation of writers might

occur.

page 7

Preventing starvation of writers

  • “Serialization”
    • Use a queue for all (makes any reader wait who arrives after a writer).
  • “Platooning”
    • Use a queue for waiting threads, and have a separate “active group” of threads.
    • While readers in the active group are working, writers wait in the queue.
    • If a writer is waiting, any later reader must wait in the queue.
    • After a writer finishes, it sweeps all the waiting readers into the active group (even if they have arrived after the next writer).
    • Avoids starvation but increases parallelism.

page 8

How do you do this?

• Hint: try time stamping a reader’s or

writer’s arrival to keep track of who should

proceed or not.

page 9

A problem to solve

• Baboons crossing a canyon

• A canyon cuts through the territory of a colony of

baboons. the baboons use a rope stretching across

the canyon to cross from one side to the other. the

rope is strong enough to permit any number of

baboons to cross in the same direction at the same

time, but is too thin to allow baboons to cross in

both directions at once.

page 10

Baboons, cont.

• So a baboon that wants to cross from west to east

must wait until all westward moving baboons have

finished and the rope is free, and vice versa.

• If the rope is being used by westward-moving

baboons, then other baboons may start to cross

from east to west no matter how many eastward-

moving baboons are waiting.

page 11

How to solve it

• First, write your program without worrying

about a series of eastward-moving baboons

holding up the waiting westward-moving

ones (i.e. don’t worry about starvation).

• Then modify your program so that it is fair

and there is no starvation. Use serialization

or platooning to do this (or another method

of your invention).

page 12

Deadlock with semaphores

– Deadlock can happen through a typo:

– P(s); critical section; P(s);