home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch12 / exec2.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  5.0 KB  |  131 lines

  1.         title   EXEC2.ASM Demo of asynchronous child process
  2.         page    55,132
  3.         .286
  4. ; EXEC2.ASM   Demonstrates use of DosExecPgm and DosCwait to 
  5. ;             run CHKDSK as an asynchronous child process.
  6. ; Copyright (C) 1987 Ray Duncan
  7. ;
  8. ; Build:        MASM EXEC2;
  9. ;               LINK EXEC2,,,OS2,EXEC2
  10. ;
  11. ; Usage:        EXEC2
  12. ;
  13. ; Note:         File CHKDSK.COM must be available in PATH.
  14.  
  15. stdin   equ     0                       ; standard input handle
  16. stdout  equ     1                       ; standard output handle
  17. stderr  equ     2                       ; standard error handle
  18.  
  19. cr      equ     0dh                     ; ASCII carriage return
  20. lf      equ     0ah                     ; ASCII line feed
  21.  
  22.         extrn   DosCwait:far            ; reference to OS/2 API
  23.         extrn   DosExecPgm:far          
  24.         extrn   DosExit:far
  25.         extrn   DosWrite:far
  26.  
  27. DGROUP  group   _DATA
  28.  
  29. _DATA   segment word public 'DATA'
  30.  
  31. objbuff db      64 dup (0)      ; receives name of dynamic link
  32. objbuff_len equ $-objbuff       ; causing DosExecPgm failure    
  33.  
  34.                                 ; argument strings for child
  35. argblk  db      'chkdsk',0      ; simple filename of child
  36.         db      ' *.*',0        ; simulated command tail
  37.         db      0               ; extra null byte terminates block
  38.  
  39.                                 ; receives DosExecPgm info
  40. cinfo   dw      0               ; PID of child process
  41.         dw      0               ; not used
  42.  
  43.                                 ; receives DosCwait info
  44. retcode dw      0               ; termination code for child
  45.         dw      0               ; result code from child's DosExit
  46.  
  47. pname   db      'chkdsk.com',0  ; pathname of child program
  48.  
  49. wlen    dw      ?
  50.  
  51. msg1    db      cr,lf,'Unexpected OS/2 Error',cr,lf
  52. msg1_len equ $-msg1
  53.  
  54. msg2    db      cr,lf,'Exec function successful',cr,lf
  55. msg2_len equ $-msg2
  56.  
  57. _DATA   ends
  58.  
  59.  
  60. _TEXT   segment word public 'CODE'
  61.  
  62.         assume  cs:_TEXT,ds:DGROUP
  63.  
  64. main    proc    far
  65.  
  66.                                         ; run CHKDSK as an asyn-
  67.                                         ; chronous child process...
  68.         push    ds                      ; receives module/entry
  69.         push    offset DGROUP:objbuff   ; point if dynlink fails
  70.         push    objbuff_len             ; length of object buffer
  71.         push    2                       ; 2 = execute asynchronously
  72.         push    ds                      ; address of argument block
  73.         push    offset DGROUP:argblk
  74.         push    0                       ; address of environment
  75.         push    0                       ; (0 = inherit parent's) 
  76.         push    ds                      ; receives child's PID
  77.         push    offset DGROUP:cinfo
  78.         push    ds                      ; name of child program
  79.         push    offset DGROUP:pname
  80.         call    DosExecPgm              ; transfer to OS/2
  81.         or      ax,ax                   ; did child process run?
  82.         jnz     error                   ; jump if function failed
  83.  
  84.                                         ; we could do other 
  85.                                         ; other processing here...
  86.  
  87.                                         ; now resynchronize with
  88.                                         ; child process...
  89.         push    0                       ; 0=immediate child only
  90.         push    0                       ; 0=wait till child ends
  91.         push    ds                      ; receives termination info
  92.         push    offset DGROUP:retcode
  93.         push    ds                      ; receives PID (N/A here)
  94.         push    offset DGROUP:cinfo+2
  95.         push    cinfo                   ; PID of process to wait for
  96.         call    DosCwait                ; transfer to OS/2
  97.  
  98. exit:                                   ; display success message...
  99.         push    stdout                  ; standard output handle
  100.         push    ds                      ; message address
  101.         push    offset msg2
  102.         push    msg2_len                ; message length
  103.         push    ds                      ; receives bytes written
  104.         push    offset wlen
  105.         call    DosWrite                ; transfer to OS/2
  106.  
  107.         push    1                       ; terminate all threads
  108.         push    0                       ; return success code
  109.         call    DosExit                 ; exit program
  110.  
  111. error:                                  ; display error message...
  112.         push    stdout                  ; standard output handle
  113.         push    ds                      ; message address
  114.         push    offset msg1
  115.         push    msg1_len                ; message length
  116.         push    ds                      ; receives bytes written
  117.         push    offset wlen               
  118.         call    DosWrite                ; transfer to OS/2
  119.  
  120.         push    1                       ; terminate all threads
  121.         push    1                       ; return error code
  122.         call    DosExit                 ; exit program
  123.  
  124. main    endp
  125.  
  126. _TEXT   ends
  127.  
  128.         end     main
  129.  
  130.