cwait

From RAD Studio
Jump to: navigation, search

Go Up to process.h Index


Header File

process.h

Category

Process Control Routines

Prototype

int cwait(int *statloc, int pid, int action);

Description

Waits for child process to terminate.

The cwait function waits for a specified child process to terminate. The process ID of the child to wait for is pid. If statloc is not NULL, it points to the location where cwait will store the termination status, as returned by GETEXITCODEPROCESS.

If pid is 0, cwait waits for any child process to terminate. Otherwise, pid specifies the process ID of the process to wait for; this value must have been obtained by an earlier call to an asynchronous spawn function.

The acceptable values for action, WAIT_CHILD and WAIT_GRANDCHILD, are ignored by the compiler and are supported for compatibility only; cwait simply waits for the process referenced by the pid. These two values are defined in process.h.

Return Value

  • When cwait returns after a normal child process termination, it returns the process ID of the child.
  • When cwait returns after an abnormal child termination, it returns -1 to the parent and sets errno to EINTR (the child process terminated abnormally).
  • If cwait returns without a child process completion, it returns a -1 value and sets errno to one of the following values:

ECHILD

No child exists or the pid value is bad

EINVAL

A bad action value was specified

Example

#include <process.h>
#include <stdio.h>
int spawn_and_wait(wchar_t* program)
{
  /* Spawn another process with NOWAIT flag */
  int pid = _wspawnl(P_NOWAIT, program, NULL);
  int exitCode;

  /* Check for error */
  if (pid == -1)
    return -1;

  /* Wait for the application to finish */
  cwait(&exitCode, pid, 0);
  return exitCode;
}


Portability

POSIX Win32 ANSI C ANSI C++

+