To start a child process and enable it to run asynchronously (that is, without suspending the parent process until the child process ends), you use the EXEC_ASYNC constant in a call to DosExecPgm. If you start a process in this way, the function copies the process identifier of the child process to the codeTerminate field of the RESULTCODES structure that is returned by DosExecPgm. You can use this process identifier to check the progress of the child process or to end the process.

You can also run a child process asynchronously by using DosExecPgm with the EXEC_ASYNCRESULT constant. In addition to causing DosExecPgm to return to the parent process immediately, this constant also directs OS/2 to save a copy of the termination status when the child process ends. This status specifies the reason the child process stopped. The parent process can retrieve the termination status by using DosWaitChild.

The following code fragment starts the program SIMPLE.EXE, and then waits for it to finish. It then prints the termination code and the return code.

    #define INCL_DOSPROCESS       /* Process and thread values */
    #include <os2.h>
    #include <stdio.h>

    #define START_PROGRAM "SIMPLE.EXE"

    CHAR         szLoadError[100];
    PSZ          pszArgs;
    PSZ          pszEnvs;
    RESULTCODES  rcReturnCodes;
    APIRET       ulrc;

    ulrc = DosExecPgm(szLoadError,         /* Object name buffer           */
                      sizeof(szLoadError), /* Length of object name buffer */
                      EXEC_SYNC,           /* Asynchronous/Trace flags     */
                      pszArgs,             /* Argument string              */
                      pszEnvs,             /* Environment string           */
                      &rcReturnCodes,      /* Termination codes            */
                      START_PROGRAM);      /* Program file name            */

    printf("Termination Code %d  Return Code %d \n",
           rcReturnCodes.codeTerminate,
           rcReturnCodes.codeResult);

/*----------------SIMPLE.EXE------------------*/

    #define INCL_DOSPROCESS       /* Process and thread values */
    #include <os2.h>
    #include <stdio.h>

    #define RETURN_CODE 0

    main()
    {
        printf("Hello!\n");
        DosExit(EXIT_PROCESS,     /* End the thread or process */
                RETURN_CODE);     /* Result code               */
    }


[Back] [Next]