qa_ipc_semset.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <utils/ipc/semset.h>
00028
00029 #include <signal.h>
00030 #include <sys/types.h>
00031 #include <sys/wait.h>
00032 #include <iostream>
00033
00034 #define FATHER_LOCK 0
00035 #define CHILD_LOCK 1
00036
00037 using namespace std;
00038 using namespace fawkes;
00039
00040 bool quit;
00041
00042 void
00043 signal_handler(int signum)
00044 {
00045 cout << "Signal handler called" << endl;
00046 quit = true;
00047 }
00048
00049 int
00050 main( int argc, char **argv )
00051 {
00052 quit = false;
00053 signal(SIGINT, signal_handler);
00054
00055 pid_t child_pid;
00056
00057 if ((child_pid = fork()) == 0) {
00058
00059
00060 SemaphoreSet *s2 = new SemaphoreSet(".", 'A', 2, false, false);
00061
00062 while ( !s2->valid() ) {
00063
00064
00065 usleep(100000);
00066 }
00067
00068 while ( ! quit ) {
00069
00070 cout << "Child: Unlocking child lock" << endl;
00071 s2->unlock(CHILD_LOCK);
00072
00073 cout << "Child: Waiting for father lock to become ready" << endl;
00074 s2->lock(FATHER_LOCK);
00075 cout << "Child: Father lock aquired, unlocking" << endl;
00076 s2->unlock(FATHER_LOCK);
00077
00078 cout << "Child: Sleeping" << endl;
00079 usleep(521342);
00080 cout << "Child: Locking child lock" << endl;
00081 s2->lock(CHILD_LOCK);
00082 cout << "Child: Sleeping again" << endl;
00083 usleep(12323);
00084 }
00085
00086 cout << "Child: Destroying s2" << endl;
00087 delete s2;
00088
00089 } else {
00090
00091
00092
00093
00094 SemaphoreSet *s1 = new SemaphoreSet(".", 'A', 2, true, true);
00095
00096 while ( ! quit ) {
00097 cout << "Father: Unlocking father lock" << endl;
00098 s1->unlock(FATHER_LOCK);
00099
00100 cout << "Father: Waiting for child lock to become ready" << endl;
00101 s1->lock(CHILD_LOCK);
00102 cout << "Father: Child lock aquired, unlocking" << endl;
00103 s1->unlock(CHILD_LOCK);
00104
00105 cout << "Father: Sleeping" << endl;
00106 usleep(821342);
00107 cout << "Father: Locking father lock" << endl;
00108 s1->lock(FATHER_LOCK);
00109 cout << "Father: again" << endl;
00110 usleep(52323);
00111 }
00112
00113 cout << "Father: Waiting for child to exit" << endl;
00114 int status;
00115 waitpid(child_pid, &status, 0);
00116
00117 cout << "Father: Destroying s1" << endl;
00118 delete s1;
00119 }
00120
00121 return 0;
00122 }
00123
00124
00125