home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / baclsema.zip / SEMTEST1.C < prev    next >
C/C++ Source or Header  |  1994-06-23  |  2KB  |  56 lines

  1. // semtest1 - Semaphore test program 1
  2.  
  3. #include <iostream.h>
  4. #include <ipc.h>
  5.  
  6. // ****************************************************************************
  7.  
  8. int main()
  9.  
  10. {
  11.    // print program title
  12.    cout << "semtest1 - Semaphore test program" << endl << endl;
  13.  
  14.    // allocate an event semaphore
  15.    cout << "creating an event semaphore..." << endl;
  16.    ipcEventSemaphore event_sem("evtsem", semcreate);
  17.    if (!event_sem) {
  18.       cerr << "Error!  state = " << event_sem.rdstate() << endl;
  19.       return 1;
  20.    }
  21.  
  22.    // show the name, type and id for the semaphore
  23.    cout << "event semaphore name is " << event_sem.Name() << endl
  24.         << "type is " << (int)event_sem.Type() << endl
  25.         << "and id is " << event_sem.ID() << endl << endl;
  26.  
  27.    // prompt user to start semtest2, reset the event semaphore and wait for
  28.    // the semtest2 program to post the semaphore so this program can continue
  29.    cout << "start the semtest2 program..." << endl;
  30.    cout << "waiting for semtest2 to post the event semaphore..." << endl;
  31.    event_sem.Reset();
  32.    event_sem.Wait();
  33.  
  34.    // access a mutex semaphore created by semtest2
  35.    cout << "accessing a mutex semaphore..." << endl;
  36.    ipcMutexSemaphore mutex_sem("mtxsem", semaccess);
  37.    if (!mutex_sem) {
  38.       cerr << "Error !  state = " << mutex_sem.rdstate() << endl;
  39.       return 2;
  40.    }
  41.  
  42.    // show the name, type and id for the semaphore
  43.    cout << "mutex semaphore name is " << mutex_sem.Name() << endl
  44.         << "type is " << (int)mutex_sem.Type() << endl
  45.         << "and id is " << mutex_sem.ID() << endl << endl;
  46.  
  47.    // request the mutex semaphore and prompt user to hit a key to release it
  48.    cout << "requesting the mutex semaphore..." << endl;
  49.    mutex_sem.Request();
  50.    cout << "press any key to release the mutex semaphore...";
  51.    cin.ignore();
  52.    mutex_sem.Release();
  53.  
  54.    return 0;
  55. }
  56.