Export (0) Print
Expand All
div
Expand Minimize
This topic has not yet been rated - Rate this topic

_cwait

Waits until another process terminates.

intptr_t _cwait( 
   int *termstat,
   intptr_t procHandle,
   int action 
);

Parameters

termstat
Pointer to a buffer where the result code of the specified process will be stored, or NULL.
procHandle
The handle to the process to wait on (the process that has to terminate before _cwait can return).
action
NULL: Ignored by Windows 98/Me and Windows NT/2000/XP applications; for other applications: action code to perform on procHandle.

Return Value

When the specified process has successfully completed, returns the handle of the specified process and sets termstat to the result code returned by the specified process. Otherwise, returns –1 and sets errno as follows.

Value Description
ECHILD No specified process exists, procHandle is invalid, or the call to the GetExitCodeProcess or WaitForSingleObject API failed.
EINVAL action is invalid.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these and other return codes.

Remarks

The _cwait function waits for the termination of the process ID of the specified process that is provided by procHandle. The value of procHandle passed to _cwait should be the value returned by the call to the _spawn function that created the specified process. If the process ID terminates before _cwait is called, _cwait returns immediately. _cwait can be used by any process to wait for any other known process for which a valid handle (procHandle) exists.

termstat points to a buffer where the return code of the specified process will be stored. The value of termstat indicates whether the specified process terminated "normally" by calling the Windows NT ExitProcess API. ExitProcess is called internally if the specified process calls exit or _exit, returns from main, or reaches the end of main. See GetExitCodeProcess for more information regarding the value passed back through termstat. If _cwait is called with a NULL value for termstat, the return code of the specified process will not be stored.

The action parameter is ignored by Windows 98/Me and Windows NT/2000/XP because parent-child relationships are not implemented in these environments.

Note that unless procHandle is -1 or -2 (handles to the current process or thread), the handle will be closed. Therefore, in this situation, do not use the returned handle.

Requirements

Routine Required header Optional headers Compatibility
_cwait <process.h> <errno.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_cwait.c
// compile with: /c
/* This program launches several processes and waits
 * for a specified process to finish.
 */

#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/* Macro to get a random integer within a specified range */
#define getrandom( min, max ) (( rand() % (int)((( max ) + 1 ) - ( min ))) + ( min ))

struct PROCESS
{

   int     nPid;
   char    name[40];

} process[4] = { { 0, "Ann" }, { 0, "Beth" }, { 0, "Carl" }, { 0, "Dave" } };

int main( int argc, char *argv[] )
{

   int termstat, c;

   srand( (unsigned)time( NULL ) );    /* Seed randomizer */
   /* If no arguments, this is the calling process */
   if( argc == 1 )
   {

      /* Spawn processes in numeric order */
      for( c = 0; c < 4; c++ ){
         _flushall();
         process[c].nPid = spawnl( _P_NOWAIT, argv[0], argv[0], 
                             process[c].name, NULL );
      }

      /* Wait for randomly specified process, and respond when done */
      c = getrandom( 0, 3 );
      printf( "Come here, %s.\n", process[c].name );
      _cwait( &termstat, process[c].nPid, _WAIT_CHILD );
      printf( "Thank you, %s.\n", process[c].name );

   }

   /* If there are arguments, this must be a spawned process */
   else
   {

      /* Delay for a period determined by process number */
      Sleep( (argv[1][0] - 'A' + 1) * 1000L );
      printf( "Hi, Dad. It's %s.\n", argv[1] );

   }

}

Sample Output

Hi, Dad. It's Ann.
Come here, Ann.
Thank you, Ann.
Hi, Dad. It's Beth.
Hi, Dad. It's Carl.
Hi, Dad. It's Dave.

See Also

Process and Environment Control Routines | _spawn Functions | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.