Home > Articles > Operating Systems, Server > Microsoft Servers

  • Print
  • + Share This
This chapter is from the book

Waiting for a Process to Terminate

The simplest, and most limited, method of synchronizing with another process is to wait for that process to complete. The general-purpose Windows wait functions introduced here have several interesting features.

  • The functions can wait for many different types of objects; process handles are just the first use of the wait functions.

  • The functions can wait for a single process, the first of several specified processes, or all processes in a group to complete.

  • There is an optional time-out period.

The two general-purpose wait functions wait for synchronization objects to become signaled. The system sets a process handle, for example, to the signaled state when the process terminates or is terminated. The wait functions, which will get lots of future use, are as follows:

DWORD WaitForSingleObject (
   HANDLE hObject,
   DWORD dwMilliseconds)
DWORD WaitForMultipleObjects (
   DWORD nCount,
   CONST HANDLE *lpHandles,
   BOOL fWaitAll,
   DWORD dwMilliseconds)

Return: The cause of the wait completion, or 0XFFFFFFFF for an error (use GetLastError for more information).

Specify either a single process handle (hObject) or an array of distinct object handles in the array referenced by lpHandles. nCount, the size of the array, should not exceed MAXIMUM_WAIT_OBJECTS (defined as 64 in WINNT.H).

dwMilliseconds is the time-out period in milliseconds. A value of 0 means that the function returns immediately after testing the state of the specified objects, thus allowing a program to poll for process termination. Use INFINITE for no time-out to wait until a process terminates.

fWaitAll, a parameter of the second function, specifies (if TRUE) that it is necessary to wait for all processes, rather than only one, to terminate.

The possible successful return values for this function are as follows.

  • WAIT_OBJECT_0 means that the handle is signaled in the case of WaitForSingleObject or all nCount objects are simultaneously signaled in the special case of WaitForMultipleObjects with fWaitAll set to TRUE.

  • WAIT_OBJECT_0+n, where 0n < nCount. Subtract WAIT_OBJECT_0 from the return value to determine which process terminated when waiting for any of a group of processes to terminate. If several handles are signaled, the returned value is the smallest possible value. WAIT_ABANDONED_0 is a possible base value when using mutex handles; see Chapter 8.

  • WAIT_TIMEOUT indicates that the time-out period elapsed before the wait could be satisfied by signaled handle(s).

  • WAIT_FAILED indicates that the call failed; for example, the handle may not have SYNCHRONIZE access.

  • WAIT_ABANDONED_0 is not possible with processes. This value is discussed in Chapter 8 along with mutex handles.

Determine the exit code of a process using GetExitCodeProcess, as described in the preceding section.

  • + Share This
  • 🔖 Save To Your Account