We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.


Updated: October 2008

Waits until another process terminates.

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


Pointer to a buffer where the result code of the specified process will be stored, or NULL.


The handle to the process to wait on (the process that has to terminate before _cwait can return).


NULL: Ignored by Windows operating system applications; for other applications: action code to perform on procHandle.

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.




No specified process exists, procHandle is invalid, or the call to the GetExitCodeProcess or WaitForSingleObject API failed.


action is invalid.

For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

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.

termstatpoints 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. For more information about the value passed back through termstat, see GetExitCodeProcess. If _cwait is called with a NULL value for termstat, the return code of the specified process is not stored.

The action parameter is ignored by the Windows operating system 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.


Required header

Optional header




For more compatibility information, see Compatibility in the Introduction.

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

#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_s (&number), number) % (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;
   unsigned int number;

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




October 2008

Revised code example.

Customer feedback.