Adding a Critical Process Monitor

A critical process monitor is a system service implemented as software, firmware, and hardware that keeps critical processes running at all times. If a critical process stops running, an Auto PC ceases to function fully. A critical process must regularly notify the critical process monitor that the process is running properly. If a process is not running properly, the OS can restart the improperly running process.

In addition to monitoring individual processes, a critical process monitor notifies a hardware watchdog timer at least once every four seconds to demonstrate that an Auto PC is still active. If the notification fails to occur, the hardware forces an Auto PC to reboot.

The following illustration shows the relationship between applications and the components of the critical process monitor.

You call critical process monitor functions to register your application with a critical process monitor and to regularly notify that critical process monitor. You can provide a callback function that the critical process monitor can call to clean up after your application, if the critical process monitor needs to terminate your application. When your application no longer qualifies as a critical process, you can also call a function to notify the critical process monitor of this change.

    To register an application as a critical process

  1. Call GetCurrentProcessID to obtain the process identifer.
  2. Determine how many milliseconds should occur between each signal.

    If your application does not notify the critical process monitor within this interval, the critical process monitor considers your application to be an improperly running process.

  3. Determine a callback function to close your application, if your application must be terminated.
  4. Call CPMRegister to pass the parameters that you determined in the previous steps.

The following code example shows how to register an application as a critical process.

dwProcessId = GetCurrentProcessID ();

if (!CPMRegister (dwProcessId, 1000, CPM_RELAUNCH, szAppName, pCallback,
    CPM_RELAUNCH_FAIL_OK))
{
// If an error occurs, handle it here.
}

    To create a notification loop

  1. Use a while loop statement to construct a notification loop.
  2. Specify the time between notifications by calling Sleep.
  3. Include code that determines if the process continues to run properly.
  4. Call CPMNotify with the process identifier to let the critical process monitor know that the process is still running properly.

The following code example shows how to create a notification loop.

while (TRUE)
{
Sleep(500);
// You must provide code that determines if the application 
// continues to function properly.
CPMNotify (dwProcessId);
}

    To create a callback function

  1. Create an application-defined callback function. CPMCallback shows the necessary parameters and is a placeholder for the application-defined function name. You may use any name that you prefer for your callback function.
  2. Include in the callback function, code to save open data files and save the status of the running application so that the application can be restarted in its current state.
  3. Be sure that you pass the callback function as a parameter when you invoke CPMRegister.

The following code example shows how to create a callback function. The name of the function is pCallback, rather than CPMCallback. The two parameters are specified in CPMCallback and the function is called with CPM_CLEANUP.

Void pCallback (DWORD dwCmd, DWORD dwCmdInfo)
{
ASSERT (dwCmd == CPM_CLEANUP);
// You must provide code to save open data files, and you
// must save the status of the running application.
}

To notify the critical process monitor that a process is no longer critical, call CPMUnRegister with the same process identifier that you used to register the process.