/*************************************************************************** * Copyright (C) 2005 by Jeff Ferr * * root@sat * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "jsharedfifo.h" #include #include #include #include #define TESTBLOCKSZ 1024 /* Max number of int's in each test block */ #define NTESTS 1000 struct priv { int counter; }; jshared::SharedFifo *fifo = NULL; void fifo_teste() { int child; int status; fifo = new jshared::SharedFifo(0, 100, sizeof(struct priv)); if ((child=fork()) != 0) { int i; int buf[TESTBLOCKSZ]; struct priv mypriv; fifo->Attach(); mypriv.counter = 0; fifo->Setpriv(&mypriv); srand(time(NULL)); for(i=0;iPut(buf,(sz+1)*sizeof(int))==-1){ std::cout << "\nno free mem left? waiting a bit..." << std::flush; mypriv.counter++; fifo->Setpriv(&mypriv); sleep(1); if(waitpid(child,&status,WNOHANG)){ std::cout << "\nBUG! child already died !"; exit(1); } }; } std::cout << "\nwaiting for child" << std::endl; wait(&status); sleep(1); std::cout << "parent quit" << std::endl; fifo->Detach(); fifo->Dealloc(); exit(0); } else { // child int i; int buf[TESTBLOCKSZ]; struct priv mypriv; fifo->Attach(); for(i=0;iGetpriv(&mypriv); std::cout << "waitcounter: " << mypriv.counter << std::endl; /* fetch block */ while((sz = fifo->Get(buf,sizeof(buf)))==-1){ std::cout << "nothing in shmem? waiting..." << std::endl; sleep(1); }; // checking it; sz = sz / sizeof(int); for(pos=0;posDetach(); } } int main(void) { try { fifo_teste(); } catch (...) { std::cout << "Fifo teste aborted !" << std::endl; } return 0; }