예제 #1
0
  public void run() {

    pr.println("Reader " + id + " trying to get into CR");

    mutex.semWait(); // semWait on mutex, only one reader can adjust count
    readCount++;

    if (readCount == 1) S.semWait(); // semWait on S

    mutex.semSignal(); // semSignal on mutex

    pr.println("Reader " + id + " is reading");

    try {
      sleep((long) (Math.random() * 1000));

    } catch (InterruptedException e) {
    }
    ;

    pr.println("Reader " + id + " is done reading");
    mutex.semWait();
    readCount--;
    if (readCount == 0) S.semSignal(); // semSignal on S
    mutex.semSignal(); // semSignal on mutex
  }
예제 #2
0
  public void run() {
    pr.println("Writer " + id + " is trying to write");
    S.semWait(); // semWait on S, see if allowed in critical section
    pr.println("Writer " + id + " is starting to write");

    try {
      sleep((long) (Math.random() * 1000)); // "writing"
    } catch (InterruptedException e) {
    }
    ;
    pr.println("Writer " + id + " is done writing");

    S.semSignal(); // semSignal on S, done writing critical section
    pr.println("Writer " + id + " is wrapping things up");
  }