Fawkes API  Fawkes Development Version
qa_ipc_semset.cpp
1 
2 /***************************************************************************
3  * qa_ipc_semset.h - QA for IPC semaphore sets
4  *
5  * Generated: Tue Sep 19 17:00:23 2006
6  * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 // Do not include in api reference
25 ///@cond QA
26 
27 #include <utils/ipc/semset.h>
28 
29 #include <signal.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <iostream>
33 
34 #define FATHER_LOCK 0
35 #define CHILD_LOCK 1
36 
37 using namespace std;
38 using namespace fawkes;
39 
40 bool quit;
41 
42 void
43 signal_handler(int signum)
44 {
45  cout << "Signal handler called" << endl;
46  quit = true;
47 }
48 
49 int
50 main( int argc, char **argv )
51 {
52  quit = false;
53  signal(SIGINT, signal_handler);
54 
55  pid_t child_pid;
56 
57  if ((child_pid = fork()) == 0) {
58  // child
59 
60  SemaphoreSet *s2 = new SemaphoreSet(".", 'A', 2, false, false);
61 
62  while ( !s2->valid() ) {
63  // wait for father to open up semaphore, could also set create to true
64  // in constructor call
65  usleep(100000);
66  }
67 
68  while ( ! quit ) {
69 
70  cout << "Child: Unlocking child lock" << endl;
71  s2->unlock(CHILD_LOCK);
72 
73  cout << "Child: Waiting for father lock to become ready" << endl;
74  s2->lock(FATHER_LOCK);
75  cout << "Child: Father lock aquired, unlocking" << endl;
76  s2->unlock(FATHER_LOCK);
77 
78  cout << "Child: Sleeping" << endl;
79  usleep(521342);
80  cout << "Child: Locking child lock" << endl;
81  s2->lock(CHILD_LOCK);
82  cout << "Child: Sleeping again" << endl;
83  usleep(12323);
84  }
85 
86  cout << "Child: Destroying s2" << endl;
87  delete s2;
88 
89  } else {
90  // father
91 
92  // Will be used by father
93  // Semaphore set with two semaphores, but zero at the beginning
94  SemaphoreSet *s1 = new SemaphoreSet(".", 'A', 2, true, true);
95 
96  while ( ! quit ) {
97  cout << "Father: Unlocking father lock" << endl;
98  s1->unlock(FATHER_LOCK);
99 
100  cout << "Father: Waiting for child lock to become ready" << endl;
101  s1->lock(CHILD_LOCK);
102  cout << "Father: Child lock aquired, unlocking" << endl;
103  s1->unlock(CHILD_LOCK);
104 
105  cout << "Father: Sleeping" << endl;
106  usleep(821342);
107  cout << "Father: Locking father lock" << endl;
108  s1->lock(FATHER_LOCK);
109  cout << "Father: again" << endl;
110  usleep(52323);
111  }
112 
113  cout << "Father: Waiting for child to exit" << endl;
114  int status;
115  waitpid(child_pid, &status, 0);
116 
117  cout << "Father: Destroying s1" << endl;
118  delete s1;
119  }
120 
121  return 0;
122 }
123 
124 
125 /// @endcond
IPC semaphore set.
Definition: semset.h:32
bool valid()
Check if the semaphore set is valid.
Definition: semset.cpp:216
Fawkes library namespace.
STL namespace.
void lock(unsigned short sem_num=0, short num=1)
Lock resources on the semaphore set.
Definition: semset.cpp:255
void unlock(unsigned short sem_num=0, short num=-1)
Unlock resources on the semaphore set.
Definition: semset.cpp:308