Syntax
#include <process.h> int _cwait(int *stat_loc, int process_id, int action_code);Description
The process_id is the value returned by the _spawn function that started the child process. If the specified child process ends before _cwait is called, _cwait returns to the calling process immediately with a value of -1. If the value of process_id is 0, the parent process waits until all of its child processes end.
If the variable pointed to by stat_loc is NULL, _cwait does not use it. If it is not NULL, _cwait places information about the return status and the return code of the child process in the location pointed to by stat_loc.
If the child process ended normally with a call to the OS/2 DosExit function, the lowest-order byte of the variable pointed to by stat_loc is 0. The next highest-order byte contains the lowest-order byte of the argument passed to DosExit by the child process. The value of this byte depends on how the child process caused the system to call DosExit. If the child called exit, _exit, or return from main, or used a DosExit coded into the program, the byte contains the lowest-order byte of the argument the child passed to exit, _exit, or return. The value of the byte is undefined if the child caused a DosExit call simply by reaching the end of main.
If the child process ended abnormally (without a call to DosExit), the lowest-order byte of the variable pointed to by stat_loc contains the return code from the OS/2 DosWaitChild function, and the next higher-order byte is 0. See the OS/2 online reference for details about the DosWaitChild return codes.
The action_code specifies when the parent process is to start running again. Values for action_code include:
Action Code
An alternative to this function is the DosWaitChild call.
At the normal end of the child process, _cwait returns the process identifier of the child to the parent process. If a child process ends abnormally, _cwait returns -1 to the parent process and sets errno to EINTR. In the case of an error, _cwait returns immediately with a value of -1 and sets errno to one of the following values:
Value
This example creates a new process called child.exe. The parent calls _cwait and waits for the child to end. The parent then displays the child's return information in hexadecimal.
#include <stdio.h>#include <process.h> #include <errno.h> int stat_child; int main(void) { int i,result; /* spawn a child and 'cwait' for it to finish */ if ((result = _spawnl(P_NOWAIT, "child", "child", "1", NULL)) != -1) { if ((i = _cwait(&stat_child, result, WAIT_CHILD)) != result) printf("Error ...expected pid from child"); else { if (0 == errno) { printf("Child process ended successfully and ...\n"); printf("program returned to the Parent process.\n"); } else printf("Child process had an error\n"); } } else printf("Error ...could not spawn a child process\n"); return 0; /**************************************************************************** If the source code for child.exe is: #include <stdio.h> int main(void) { puts("This line was written by child.exe"); return 0; } The output should be similar to : This line was written by child.exe Child process ended successfully and ... program returned to the Parent process. ****************************************************************************/ }Related Information