19.2.2 Monitoring System Status

A TSR that uses a hardware device such as the video or disk must not interrupt while the device is active. A TSR monitors a device by handling the device's interrupt. Each interrupt handler need only set a flag to indicate that the device is in use and then clear the flag when the interrupt finishes.

The following shows a typical monitor handler:

NewHandler PROC FAR

mov ActiveFlag, TRUE ; Set active flag

pushf ; Simulate interrupt by

; pushing flags,

call OldHandler ; then calling original routine

mov ActiveFlag, FALSE ; Clear active flag

iret ; Return from interrupt

NewHandler ENDP

Only hardware used by the TSR requires monitoring. For example, a TSR that performs disk input/output (I/O) must monitor disk use through Interrupt 13h. The disk handler sets an active flag that prevents the TSR from executing during a read or write operation. Otherwise, the TSR's own I/O would move the disk head. This would cause the suspended disk operation to continue with the head incorrectly positioned when the TSR returned control to the interrupted program.

In the same way, an active TSR that displays to the screen must monitor calls to Interrupt 10h. The Interrupt 10h BIOS routine does not protect critical sections of code that program the video controller. The TSR must therefore ensure that it does not interrupt such nonreentrant operations.

The activities of the operating system also affect the system status. With few exceptions, DOS functions are not reentrant and must not be interrupted. However, monitoring DOS is somewhat more complicated than monitoring hardware. Discussion of this subject is deferred until Section 19.4.

The following comments describe the chain of events depicted in Figure 19.1. Each comment refers to one of the numbered pointers in the figure.

1.At time = t, the timer handler activates. It finds the flag TsrRequestFlag clear, indicating that the TSR has not been requested. The handler terminates without taking further action. Notice that Interrupt 13h is currently processing a disk I/O operation.

2.Before the next timer interrupt, the keyboard handler detects the hot key, signalling a request for the TSR. The handler sets TsrRequestFlag and returns.

3.At time = t + 1/18 second, the timer handler again activates and finds TsrRequestFlag set. The handler checks other active flags to determine if the TSR can safely execute. Since Interrupt 13h has not yet completed its disk operation, the timer handler finds DiskActiveFlag set. The handler therefore terminates without invoking the TSR.

4.At time = t + 2/18 second, the timer handler again finds TsrRequestFlag set and repeats its scan of the active flags. DiskActiveFlag is now clear, but in the interim, Interrupt 10h has activated as indicated by the flag VideoActiveFlag. The timer handler accordingly terminates without invoking the TSR.

5.At time = t + 3/18 second, the timer handler repeats the process. This time it finds all active flags clear, indicating that the TSR may safely execute. The timer handler calls the TSR, which sets its own active flag to ensure that it will not interrupt itself if requested again.

6.The timer and other interrupts continue to function normally while the TSR executes.