## Physical Device Driver Reference

[prev][next][parent][TOC]

### Monitor Termination

An application is responsible for terminating its own monitor threads. The monitor dispatcher is responsible for removing the monitors from the monitor chains belonging to a character device driver. The removal is initiated by the character device driver when it receives a monitor close request. This occurs when:

DosMonClose is called by the application
DosExit (terminate all threads in a process) is called by the application
The process is abnormally terminated by the user (for example, the Ctrl+C key sequence is pressed)

When the character device driver receives a monitor close request, it calls the DevHlp_DeRegister routine for each of its monitor chains. Before returning to the physical device driver, the monitor dispatcher protects the integrity and order of data in the data stream, and guarantees minimal data loss, by:

Locking out the application from subsequent calls to DosMonRead and DosMonWrite. The application is prevented from intercepting data from, and returning data to, the data stream.

Draining all data in an orderly manner from the data stream.

The successful removal of monitors from a monitor chain is dependent on the ability of the physical device driver, or other monitors downstream in the monitor chain, to receive and process data from the data stream. If the monitor being removed from the monitor chain is not the last in the monitor chain, the next monitor in the chain must not be blocked. Similarly, the physical device driver must not be blocked on a call to its notification routine; it must be able to process data it receives in its monitor chain buffer.

During monitor termination, the monitor dispatcher prevents the application from touching the data stream by returning errors on calls to DosMonRead or DosMonWrite. In addition, errors are returned to monitor threads that are blocked on a call to DosMonRead or DosMonWrite. The application can then clean up the appropriate monitor threads. In both cases, the application is responsible for checking and handling the error returns.

During monitor termination, there is always a risk of data loss. If the physical device driver or monitor dispatcher is blocked while waiting for critical data that is lost, the system can be permanently affected. To reduce this risk, a monitor application must be well-behaved. See Well- Behaved Monitor Applications. Separate threads monitoring the data stream must be terminated before DosMonClose is called. This guarantees that all data taken from the data stream is returned to the data stream by the application. The application must stop calling DosMonRead and DosMonWrite before it calls DosMonClose. This can mean synchronizing the calls with semaphores.

The closing of monitor threads can also be synchronized by using signal handler or exitlist routines. Signal handler routines are called when the system sends a signal to the process to indicate that certain events are occurring, (for example, the Ctrl+C key sequence has been pressed). Exitlist routines are a list of routines that are called when an application is terminating. In either case, these routines can be used to signal an application's separate monitor threads to prepare for termination . See the descriptions of the DosSetSigHandler, DosHoldSignal, and DosExitList functions in the OS/2 Control Program Programming Reference for details.