home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / primcuts.zip / NamedSharedMem.c < prev    next >
Text File  |  2000-06-30  |  3KB  |  124 lines

  1. /*
  2.  * Illustrate the usage of shared memory.
  3.  * The allocating process does not have to be the one to free the memeory
  4.  * object.
  5.  */
  6. #pragma strings(readonly)
  7.  
  8. #define INCL_DOSMEMMGR
  9. #define INCL_DOSPROCESS
  10. #define INCL_DOSSEMAPHORES
  11. #define INCL_DOSMISC
  12. #define INCL_DOSERRORS
  13. #define INCL_KBD
  14.  
  15. #include <os2.h>
  16.  
  17. #include <stdio.h>
  18.  
  19.  
  20. int main(void)
  21. {
  22.    APIRET rc = NO_ERROR;
  23.    PBYTE pbBuf = NULL;
  24.    ULONG ulPid = 0;
  25.    PTIB ptib = NULL;
  26.    PPIB ppib = NULL;
  27.    UCHAR pchMemName[] = "\\SHAREMEM\\Primecuts\\SharedMemTest";
  28.    HMTX hmtxLock = NULLHANDLE;
  29.    UCHAR pchMtxName[] = "\\SEM32\\Primecuts\\SharedMemTest";
  30.    KBDKEYINFO keyinfo = { 0 };
  31.  
  32.    /*
  33.     * Attempt to create mutual exclusion sempahore to serialize access to the
  34.     * access counter.
  35.     */
  36.    rc = DosCreateMutexSem(pchMtxName, &hmtxLock, 0UL, FALSE);
  37.    if(rc == ERROR_DUPLICATE_NAME)
  38.    {
  39.       /*
  40.        * The semaphore already exists in system, attempt to open it instead.
  41.        */
  42.       rc = DosOpenMutexSem(pchMtxName, &hmtxLock);
  43.    }
  44.  
  45.    if(rc != NO_ERROR)
  46.    {
  47.       puts("Couldn't create or open mutex semaphore.");
  48.       return 1;
  49.    }
  50.  
  51.    rc = DosGetInfoBlocks(&ptib, &ppib);
  52.    printf("PID: %04x\n", ppib->pib_ulpid);
  53.  
  54.    /*
  55.     * Allocate named memory block
  56.     */
  57.    rc = DosAllocSharedMem((PPVOID)&pbBuf, pchMemName, 1024, PAG_READ | PAG_WRITE | PAG_COMMIT);
  58.    if(rc == ERROR_ALREADY_EXISTS)
  59.    {
  60.       rc = DosGetNamedSharedMem((PPVOID)&pbBuf, pchMemName, PAG_READ | PAG_WRITE);
  61.       if(rc != NO_ERROR)
  62.       {
  63.          printf("Error: DosGetNamedSharedMem() returned: %d\n", rc);
  64.       }
  65.       else
  66.       {
  67.          printf("Got Address: %08x\n", pbBuf);
  68.       }
  69.    }
  70.    else
  71.    {
  72.       printf("Address of new memoryblock: %08x\n", pbBuf);
  73.       *pbBuf = 0;
  74.    }
  75.  
  76.    /*
  77.     * Increase access count
  78.     */
  79.    rc = DosRequestMutexSem(hmtxLock, SEM_INDEFINITE_WAIT);
  80.    if(rc == NO_ERROR)
  81.    {
  82.       (*pbBuf)++;
  83.  
  84.       printf("%d\n", *pbBuf);
  85.  
  86.       rc = DosReleaseMutexSem(hmtxLock);
  87.    }
  88.  
  89.    /*
  90.     * Wait for a key press
  91.     */
  92.    KbdCharIn(&keyinfo, IO_WAIT, (HKBD)0);
  93.  
  94.    /*
  95.     * Decrease access count
  96.     */
  97.    rc = DosRequestMutexSem(hmtxLock, SEM_INDEFINITE_WAIT);
  98.    if(rc == NO_ERROR)
  99.    {
  100.       (*pbBuf)--;
  101.  
  102.       rc = DosReleaseMutexSem(hmtxLock);
  103.    }
  104.  
  105.    /*
  106.     * When the access count is zero, free the memory object.
  107.     */
  108.    if(*pbBuf == 0)
  109.    {
  110.       rc = DosFreeMem(pbBuf);
  111.       if(rc == NO_ERROR)
  112.       {
  113.          printf("Memory block Freed.\n");
  114.       }
  115.    }
  116.  
  117.    /*
  118.     * Close mutex semaphore
  119.     */
  120.    rc = DosCloseMutexSem(hmtxLock);
  121.  
  122.    return 0;
  123. }
  124.