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 <sys/types.h>
28 #include <sys/wait.h>
29 #include <utils/ipc/semset.h>
30 
31 #include <iostream>
32 #include <signal.h>
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  cout << "Child: Unlocking child lock" << endl;
70  s2->unlock(CHILD_LOCK);
71 
72  cout << "Child: Waiting for father lock to become ready" << endl;
73  s2->lock(FATHER_LOCK);
74  cout << "Child: Father lock aquired, unlocking" << endl;
75  s2->unlock(FATHER_LOCK);
76 
77  cout << "Child: Sleeping" << endl;
78  usleep(521342);
79  cout << "Child: Locking child lock" << endl;
80  s2->lock(CHILD_LOCK);
81  cout << "Child: Sleeping again" << endl;
82  usleep(12323);
83  }
84 
85  cout << "Child: Destroying s2" << endl;
86  delete s2;
87 
88  } else {
89  // father
90 
91  // Will be used by father
92  // Semaphore set with two semaphores, but zero at the beginning
93  SemaphoreSet *s1 = new SemaphoreSet(".", 'A', 2, true, true);
94 
95  while (!quit) {
96  cout << "Father: Unlocking father lock" << endl;
97  s1->unlock(FATHER_LOCK);
98 
99  cout << "Father: Waiting for child lock to become ready" << endl;
100  s1->lock(CHILD_LOCK);
101  cout << "Father: Child lock aquired, unlocking" << endl;
102  s1->unlock(CHILD_LOCK);
103 
104  cout << "Father: Sleeping" << endl;
105  usleep(821342);
106  cout << "Father: Locking father lock" << endl;
107  s1->lock(FATHER_LOCK);
108  cout << "Father: again" << endl;
109  usleep(52323);
110  }
111 
112  cout << "Father: Waiting for child to exit" << endl;
113  int status;
114  waitpid(child_pid, &status, 0);
115 
116  cout << "Father: Destroying s1" << endl;
117  delete s1;
118  }
119 
120  return 0;
121 }
122 
123 /// @endcond
IPC semaphore set.
Definition: semset.h:32
bool valid()
Check if the semaphore set is valid.
Definition: semset.cpp:205
void unlock(unsigned short sem_num=0, short num=-1)
Unlock resources on the semaphore set.
Definition: semset.cpp:299
void lock(unsigned short sem_num=0, short num=1)
Lock resources on the semaphore set.
Definition: semset.cpp:244
Fawkes library namespace.