qa_ipc_semset.cpp

00001 
00002 /***************************************************************************
00003  *  qa_ipc_semset.h - QA for IPC semaphore sets
00004  *
00005  *  Generated: Tue Sep 19 17:00:23 2006
00006  *  Copyright  2005-2006  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 // Do not include in api reference
00025 ///@cond QA
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     // child
00059 
00060     SemaphoreSet *s2 = new SemaphoreSet(".", 'A', 2, false, false);
00061 
00062     while ( !s2->valid() ) {
00063       // wait for father to open up semaphore, could also set create to true
00064       // in constructor call
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     // father
00091 
00092     // Will be used by father
00093     // Semaphore set with two semaphores, but zero at the beginning
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 /// @endcond

Generated on Tue Feb 22 13:32:28 2011 for Fawkes API by  doxygen 1.4.7