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

  1. /*
  2.  * This example illustrates the use of shared memory and ram semaphores
  3.  * between separate processes.  One process reads and stores keystrokes
  4.  * into a circular buffer in shared memory, and another reads the buffer
  5.  * and writes to standard output. The output program throttles output to
  6.  * one character per sec so the effect of buffering can be seen. Type
  7.  * CONTROL-d to terminate.
  8.  *
  9.  * Note the mutual exclusion on access to the head and tail pointers of the
  10.  * buffer and on clearing and setting the full and empty semaphores.  For a
  11.  * discussion, see Peterson & Silberschatz, "Operating System Concepts",
  12.  * 2nd ed., chapter 9.
  13.  *
  14.  * Be aware when running this that the system does keyboard buffering
  15.  * underneath this program, so you can type ahead more than you might think.
  16.  *
  17.  * Needs shrchild.c as companion program.
  18.  *
  19.  * Compile as: cl -AL -G2 -Lp share.c
  20.  *
  21.  * Created by Microsoft Corp. 1986
  22.  */
  23.  
  24. #define INCL_DOSPROCESS
  25. #define INCL_SUB
  26. #define INCL_DOSMEMMGR
  27.  
  28. #include <os2def.h>
  29. #include <bse.h>
  30. #include "share.h"        /* common declarations between processes */
  31.  
  32. #define    CHILD1PROG    "SHRCHILD.EXE"        /* child process */
  33. #define    CTRLd        4            /* termination character */
  34. #define FBSZ        32
  35.  
  36. KBDKEYINFO KeyData;        /* declared in subcalls.h */
  37.  
  38. main()
  39. {
  40.     SEL        Selector;
  41.     RESULTCODES    childID;     /* child process id */
  42.     unsigned    rc;        /* return code */
  43.     char        c,
  44.             fbuf[FBSZ];    /* failing object buffer */
  45.     struct ShareRec *SmemPtr;    /* shared memory pointer */
  46.  
  47.     /* allocate the shared memory segment */
  48.     if (rc = DosAllocShrSeg( SHRSEGSIZE, (PSZ)SHRSEGNAME,
  49.                 &Selector ))  {
  50.         printf("alloc of shared memory failed, error: %d\n", rc);
  51.         DosExit(EXIT_PROCESS, 0);
  52.     }
  53.  
  54.     /* Get a far pointer from a 16 bit selector */
  55.     SmemPtr = (struct ShareRec *) MAKEP(Selector, 0);
  56.  
  57.     /* Initialize circular buffer flags */
  58.     DosSemClear((HSEM)&(SmemPtr->fullsem));
  59.     DosSemSet((HSEM)&(SmemPtr->emptysem));
  60.     DosSemClear((HSEM)&(SmemPtr->mutexsem));
  61.     SmemPtr->head = 0;
  62.     SmemPtr->tail = 0;
  63.  
  64.     /* exec asynchronously the consumer process */
  65.     if (rc = DosExecPgm(
  66.         (PCHAR) fbuf,            /* ObjNameBuf        */
  67.         FBSZ,                /* ObjNameLen        */
  68.         EXEC_ASYNC,            /* AsyncTraceFlags    */
  69.         (PSZ) 0L,            /* Argument Strings    */
  70.         (PSZ) 0L,            /* Environment Strings    */
  71.         &childID,
  72.                         /* ID & Termination Codes */
  73.         (PSZ) CHILD1PROG )) {        /* Program Filename    */
  74.         
  75.         printf("exec of child process failed, error: %d\n", rc);
  76.         DosExit(EXIT_PROCESS, 0);
  77.     }
  78.  
  79.     /* Here, we read chars from the keyboard, and put them in a */
  80.     /* circular buffer in shared memory */
  81.  
  82.     KbdCharIn(&KeyData, 0, 0);    /* read character from keyboard */
  83.  
  84.     while((c = KeyData.chChar) != CTRLd) {
  85.  
  86.         /* block if buffer full */
  87.         DosSemWait((HSEM)&(SmemPtr->fullsem), WAITFOREVER);
  88.  
  89.         /* mutual exclusion on buffer pointers and semaphores */
  90.         DosSemRequest((HSEM)&(SmemPtr->mutexsem), WAITFOREVER);
  91.         SmemPtr->CircBuffer[SmemPtr->head] = c;
  92.         SmemPtr->head++;            /* step pointer */
  93.         SmemPtr->head %= CIRCBUFSIZE;   /* wrap at end  */
  94.         if(BUFFUL(SmemPtr))
  95.         DosSemSet((HSEM)&(SmemPtr->fullsem)); /* indicate buf full   */
  96.         DosSemClear((HSEM)&(SmemPtr->emptysem));  /* indicate buf !emtpy */
  97.         DosSemClear((HSEM)&(SmemPtr->mutexsem));
  98.  
  99.  
  100.         KbdCharIn(&KeyData, 0, 0);    /* read character from keyboard */
  101.     }
  102.     DosKillProcess( 1, childID.codeTerminate);    /* kill consumer */
  103.     DosExit( EXIT_PROCESS, 0 );       /* exit, terminating all threads */
  104. }
  105.