home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / share / shrchild.c < prev   
Encoding:
C/C++ Source or Header  |  1989-11-20  |  1.5 KB  |  54 lines

  1. /*
  2.  * This is the child process exec'd asynchronously from share.c.
  3.  *
  4.  * Created by Microsoft Corp. 1986
  5.  */
  6.  
  7. #define INCL_DOSMEMMGR
  8. #define INCL_DOSSEMAPHORES
  9. #define INCL_DOSFILEMGR
  10. #define INCL_DOSPROCESS
  11.  
  12. #include <os2def.h>
  13. #include <bsedos.h>        /* necessary whenever doscalls are made */
  14. #include "share.h"        /* common shared memory declarations */
  15.  
  16. main()
  17. {
  18.     SEL            Selector;
  19.     unsigned        rc;        /* return code */
  20.     USHORT            nBytes;
  21.     char            c;
  22.     struct ShareRec     *SmemPtr;    /* shared memory pointer */
  23.  
  24.     /* get selector to the shared memory segment defined in parent */
  25.  
  26.     if (rc = DosGetShrSeg( (PSZ) SHRSEGNAME,
  27.                 &Selector ))  {
  28.         DosExit(EXIT_PROCESS, 0);
  29.     }
  30.  
  31.     /* Get a far pointer from a 16 bit selector */
  32.  
  33.     SmemPtr = (struct ShareRec *) MAKEP(Selector, 0);
  34.     
  35.     for (;;) {
  36.  
  37.         /* block if buffer empty */
  38.         DosSemWait((HSEM)&(SmemPtr->emptysem), WAITFOREVER);
  39.  
  40.         /* mutual exclusion on buffer pointers and semaphores */
  41.         DosSemRequest((HSEM)&(SmemPtr->mutexsem), WAITFOREVER);
  42.         c = SmemPtr->CircBuffer[SmemPtr->tail];    /* get next char */
  43.         SmemPtr->tail++;            /* step pointer */
  44.         SmemPtr->tail %= CIRCBUFSIZE;   /* wrap at end  */
  45.         if (SmemPtr->tail == SmemPtr->head)
  46.         DosSemSet((HSEM)&(SmemPtr->emptysem)); /* indicate buf empty */
  47.         DosSemClear((HSEM)&(SmemPtr->fullsem));    /* indicate buf !full */
  48.         DosSemClear((HSEM)&(SmemPtr->mutexsem));
  49.  
  50.         DosWrite( 1, (PVOID) &c, 1, &nBytes );     /* display next char */
  51.         DosSleep(1000L);    /* max 1 per sec to demo buffering & sems */
  52.     }
  53. }
  54.