Skip to content

Instantly share code, notes, and snippets.

@banderson623
Last active December 14, 2015 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banderson623/5163370 to your computer and use it in GitHub Desktop.
Save banderson623/5163370 to your computer and use it in GitHub Desktop.
Solving Homework 7, part 2 See Diagram of interactions: https://www.dropbox.com/s/8bk1kiidhkxxhph/WritersPriority.png
Brian Anderson
Com S 352, Spring 2013
Assignment 7, Question 2
=========================================================================
2. (30pts) Modify the monitor pseudo-code for the reader-writer problem
on Page 8 of 2013-3-4's lecture to solve the "writer's priority"
version of the problem defined as follows:
(1) At any time, the shared file can be accessed by
one writer exclusively, or one or multiple readers concurrently.
(2) Whenever there are both reader and writer processes
arriving or waiting to access the shared file, a writer
has higher priority than reader to access the shared file.
==========================================================================
Monitor reader-writer
begin
activeReaderCount: integer //renamed to activeReaderCount
busyWriting: boolean //renamed to busyWriting
OKtoread: condition
OKtowrite: condition
/* -- READING ----------------------------------------------------------------------- */
prodcedure startread;
begin
if busyWriting then OKtoread.wait;
activeReaderCount := activeReaderCount + 1;
OKtoread.signal; // all readers can join in now....
end startread
proecedure endread;
begin
activeReaderCount := activeReaderCount - 1;
if activeReaderCount = 0 then OKtowrite.signal;
end endread
/* -- WRITING -------------------------- */
procedure startwrite;
begin
if busyWriting or activeReaderCount <> 0 then OKtowrite.wait;
busyWriting := true;
end startwrite
procedure endwrite;
begin
busyWriting := false;
if OKtowrite.queue then OKtowrite.signal; // If another write queued, signal it
else if OKtoread.queue then OKtoread.signal; // else signal read
end endwrite
/* -- INITIALIZE -------------------------- */
begin
activeReaderCount := 0;
busyWriting := false;
end
end reader-writer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment