다음을 통해 공유


_cwait

Waits until another process terminates.

int_cwait(int*termstat,intprocHandle**,intaction);**

Routine Required Header Optional Headers Compatibility
_cwait <process.h> <errno.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

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

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

Parameters

termstat

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

procHandle

Handle to the current process or thread

action

NULL: Ignored by Windows NT and Windows 95 applications; for other applications: action code to perform on procHandle

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 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 NT and Windows 95 because parent-child relationships are not implemented in these environments. Therefore, the OS/2 wait function, which allows a parent process to wait for any of its immediate children to terminate, is not available.

Example

/* CWAIT.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" } };

void 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] );

   }

}

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.

Process and Environment Control Routines

See Also   _spawn Functions