Example (_cwait -- Wait for Child Process)

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