home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IPCX.ZIP / SHMEM.C < prev    next >
C/C++ Source or Header  |  1989-09-20  |  3KB  |  115 lines

  1. /* shmem.c RHS 5/2/89
  2.  *
  3.  This program demonstrates the use of shared memory. To use it, run more than
  4.  one copy of the program in different sessions with:
  5.  
  6.  SHMEM
  7.  
  8.  */
  9.  
  10. #define INCL_DOS
  11. #define INCL_KBD
  12. #define INCL_ERRORS
  13.  
  14. #include<os2.h>
  15. #include<stdio.h>
  16. #include<memory.h>
  17.  
  18. #define    SHAREDMEM        "\\sharemem\\shmem.mem"
  19. #define    SEMNAME            "\\sem\\ssem.sem"
  20. #define    SHAREDMEMSIZE    200
  21.  
  22. void error_exit(USHORT err, char *msg);
  23. PID Os2GetPid(void);
  24. void main(void);
  25.  
  26. void main(void)
  27.     {
  28.     HSYSSEM semhandle;
  29.     SEL selector;
  30.     USHORT retval;
  31.     int creator = FALSE;
  32.     KBDKEYINFO kbdkeyinfo;
  33.     PCH p;
  34.     PID pid;
  35.  
  36.     pid = Os2GetPid();
  37.  
  38.         /* first process will successfully execute this code and create the
  39.             the semaphore
  40.          */
  41.     if(retval = DosCreateSem(CSEM_PUBLIC,&semhandle,SEMNAME))
  42.         {
  43.         if(retval != ERROR_ALREADY_EXISTS)
  44.             error_exit(retval,"DosCreateSem");
  45.  
  46.         /* second process will open the semaphore previously created    */
  47.         else if (retval = DosOpenSem(&semhandle,SEMNAME))
  48.             error_exit(retval,"DosOpenSem");
  49.  
  50.         if(retval = DosGetShrSeg(SHAREDMEM,&selector))
  51.             error_exit(retval,"DosGetShrSeg");
  52.  
  53.         DosSemWait(semhandle,SEM_INDEFINITE_WAIT);
  54.         DosSemRequest(semhandle,SEM_INDEFINITE_WAIT);
  55.         }
  56.     else
  57.         {
  58.         /* then first process will wait here until second process executes
  59.             the code in the loop below
  60.          */
  61.         if(retval = DosAllocShrSeg(SHAREDMEMSIZE,SHAREDMEM,&selector))
  62.             error_exit(retval,"DosAllocShrSeg");
  63.  
  64.         DosSemSet(semhandle);
  65.         creator = TRUE;
  66.         }
  67.  
  68.     p = MAKEP(selector,0);                                /* create pointer to shmem    */
  69.     if(!creator)
  70.         memset(p,'\0',SHAREDMEMSIZE);
  71.  
  72.     while(TRUE)    /* both processes will continue to execute in this loop        */
  73.         {
  74.         if(*p)
  75.             printf("%s (%u) received: %s\n",(creator ? "Owner" : "User"),pid,p);
  76.  
  77.         sprintf(p,(creator ?
  78.                "from %u, the owner of this shared memory...Hello!" :
  79.                 "from %u,    a user of this shared memory...Hello!"),pid);
  80.  
  81.         DosSemClear(semhandle);
  82.  
  83.         KbdCharIn(&kbdkeyinfo,IO_NOWAIT,0);            /* check for key press        */
  84.         if(kbdkeyinfo.fbStatus & FINAL_CHAR_IN)    /* if pressed, break out    */
  85.             break;
  86.  
  87.         DosSleep(150L);
  88.         DosSemRequest(semhandle,SEM_INDEFINITE_WAIT);
  89.         }
  90.  
  91.     DosCloseSem(semhandle);                                /* close the semaphore        */
  92.     DosFreeSeg(selector);
  93.     DosExit(EXIT_PROCESS,0);                            /* and exit                        */
  94.     }
  95.  
  96.  
  97. PID Os2GetPid(void)                                        /* returns process id        */
  98.     {
  99.     PIDINFO pidinfo;
  100.  
  101.     DosGetPID(&pidinfo);                                    /* get process id                */
  102.     return pidinfo.pid;                                    /* return it                    */
  103.     }
  104.  
  105. void error_exit(USHORT err, char *msg)
  106.     {
  107.     printf("Error %u returned from %s\n",err,msg);
  108.     DosExit(EXIT_PROCESS,0);
  109.     }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.