13.07.2015 Views

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

274 Concurrency Chapter 8scope (a block, in the Algol sense) that may export types and procedures to the outsideworld but uses local variables to store state and perform necessary computations. Monitorshave this structure, too, although they are principally used as a mechanism forshared data abstractions. They export procedures, which we will call guard procedures,that may be called from outside the monitor. No matter how many activities are running,only one is allowed to execute a guard procedure at a time.The most straightforward use of monitors is to package all routines that use a set ofshared data into a single monitor. All accesses to those data will be forced to use guardprocedures because the data themselves are hidden from the outside world. For example,if we wish to implement a counter that records the number of times some interestingevent has happened, and different activities will wish to modify and inspect the counter,we could build the following monitor. The syntax chosen here is meant to convey theideas without introducing extraneous concepts; therefore, it differs from Modula, whichalso provides monitors with a different syntax.1 monitor Counter;23 export RaiseCount, ReadCount;45 var6 Count : integer;78 guard procedure RaiseCount;9 begin10 Count := Count + 1;11 end RaiseCount;1213 guard procedure ReadCount : integer;14 begin15 ReadCount := Count;16 end ReadCount;1718 begin { initialize }19 Count := 0;20 end Counter.One way to picture the monitor is shown in Figure 8.2, which shows the monitor as afloor plan of a building. When activities wish to invoke guard procedures, they mustentryqueue2guardprocedures1Figure 8.2 A simple monitor

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!