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

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