semaphores.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <iostream>
14 #include <cstdlib>
15 #include <gtest/gtest.h>
16 #include <ecl/time/sleep.hpp>
17 #include "../../include/ecl/ipc/semaphore.hpp"
18 #include "../../include/ecl/ipc/shared_memory.hpp"
19 
20 #ifdef ECL_HAS_SEMAPHORES
21 
22 /*****************************************************************************
23 ** Using
24 *****************************************************************************/
25 
26 using std::string;
27 using ecl::SharedMemory;
28 using ecl::Semaphore;
29 using ecl::Sleep;
30 
31 /*****************************************************************************
32 ** Doxygen
33 *****************************************************************************/
34 
39 /*****************************************************************************
40 ** Namespaces
41 *****************************************************************************/
42 
43 namespace ecl {
44 namespace ipc {
45 namespace tests {
46 
47 /*****************************************************************************
48 ** Data Storage Class
49 *****************************************************************************/
50 
51 class SemaphoreTestData {
52  public:
53  double value[2];
54  void initialise() { value[0] = 1.0; value[1] = 2.0; };
55 };
56 
57 } // namespace tests
58 } // namespace ipc
59 } // namespace ecl
60 
61 /*****************************************************************************
62 ** Using
63 *****************************************************************************/
64 
65 using ecl::ipc::tests::SemaphoreTestData;
66 
67 /*****************************************************************************
68 ** Doxygen
69 *****************************************************************************/
70 
75 /*****************************************************************************
76 ** Tests
77 *****************************************************************************/
78 
79 TEST(SemaphoreTests,access) {
80 
81  string sm_name("shared_memory");
82  string sem_name("shared_semaphore");
83  Sleep sleep;
84 
85  pid_t pID = fork();
86  if (pID == 0)
87  {
88  /*********************************************************************
89  ** Child
90  *********************************************************************/
91  sleep(1);
92  try {
93  SharedMemory<SemaphoreTestData> sm(sm_name);
94  Semaphore semaphore(sem_name);
95 // std::cout << "Child waiting" << std::endl;
96 // std::cout << "Child trying the lock" << std::endl;
97  if ( !semaphore.trylock() ) {
98 // std::cout << "Child tried the lock but failed, now hard locking" << std::endl;
99  semaphore.lock();
100  }
101 // std::cout << "Child entering shared memory" << std::endl;
102  SemaphoreTestData *data = sm.data();
103 // std::cout << "Child: " << data->value[0] << " " << data->value[1] << std::endl;
104  EXPECT_EQ(1.3,data->value[0]);
105  EXPECT_EQ(2.3,data->value[1]);
106  semaphore.unlock();
107  } catch ( ecl::StandardException &e) {
108  // Don't fail the test, hudson doesn't let us have permissions for instance.
109  std::cout << e.what() << std::endl;
110  }
111  }
112  else if (pID < 0) // failed to fork
113  {
114  std::cerr << "Failed to fork" << std::endl;
115  exit(1);
116  }
117  else
118  {
119  /*********************************************************************
120  ** Parent
121  *********************************************************************/
122  try {
123  SharedMemory<SemaphoreTestData> sm(sm_name);
124  Semaphore semaphore(sem_name);
125  SemaphoreTestData *data = sm.data();
126  semaphore.lock();
127 // std::cout << "Parent in the shared memory" << std::endl;
128  sleep(2);
129  data->value[0] = 1.3;
130  data->value[1] = 2.3;
131  EXPECT_EQ(1.3,data->value[0]);
132  EXPECT_EQ(2.3,data->value[1]);
133 // std::cout << "Parent: " << data->value[0] << " " << data->value[1] << std::endl;
134 // std::cout << "Parent leaving shared memory" << std::endl;
135  semaphore.unlock();
136  sleep(3);
137  } catch ( ecl::StandardException &e) {
138  // Don't fail the test, hudson doesn't let us have permissions for instance.
139  std::cout << e.what() << std::endl;
140  }
141  }
142  SUCCEED();
143 
144 }
145 
146 /*****************************************************************************
147 ** Main program
148 *****************************************************************************/
149 
150 int main(int argc, char **argv) {
151 
152  std::cout << std::endl;
153  std::cout << "What you should see:" << std::endl;
154  std::cout << " - Process forks" << std::endl;
155  std::cout << " - Parent creates shared memory." << std::endl;
156  std::cout << " - Parent locks a semaphore and enters shared memory for 2 seconds." << std::endl;
157  std::cout << " - Child waits one second and attempts to lock the already locked semaphore." << std::endl;
158  std::cout << " - Parent writes into shared memory and unlocks the semaphore." << std::endl;
159  std::cout << " - Child finally gains access and reads the shared memory." << std::endl;
160  std::cout << " - Parent process sleeps for four seconds while child dies." << std::endl;
161  std::cout << " - Parent dies." << std::endl;
162  std::cout << std::endl;
163 
164  testing::InitGoogleTest(&argc,argv);
165  return RUN_ALL_TESTS();
166 }
167 
168 #else
169 
170 /*****************************************************************************
171 ** Alternative Main
172 *****************************************************************************/
173 
174 int main(int argc, char **argv) {
175  std::cout << std::endl;
176  std::cout << "Semaphores are not supported on this platform (or ecl is just lacking)." << std::endl;
177  std::cout << std::endl;
178  return 0;
179 }
180 
181 #endif /* ECL_HAS_SEMAPHORES */
182 
183 
int main(int argc, char **argv)
Definition: semaphores.cpp:174
Embedded control libraries.
const char * what() const
TEST(TypeTests, fundamentals)


ecl_ipc
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:17