#include #include #include #include #include #include #include #include #include #include #include #include using namespace boost::interprocess; //Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator void_allocator; typedef allocator int_allocator; typedef allocator long_allocator; typedef allocator ushort_allocator; typedef allocator char_allocator; typedef basic_string, char_allocator> char_string; typedef allocator char_string_allocator; //#include class InterfaceObject_sh { public: int a; char_string b; public: InterfaceObject_sh(int a_, const char *b_,const void_allocator &void_alloc) : a(a_), b(b_,void_alloc) {} InterfaceObject_sh& operator = (const InterfaceObject_sh& sourceInterfaceObject) { a=sourceInterfaceObject.a; b=sourceInterfaceObject.b; return *this; } }; typedef allocator interfaceobject_type_allocator; typedef vector interfaceobject_vector; class RequestObject { public: class InterfaceObject_sh embeddedInterfaceObject; //time_t start_timep; long start_timep; //pid_t current_processid; long current_processid; unsigned short flag; //0(default) not assign 1 assign 2 complish char_string request_id; interfaceobject_vector myvector; //constructor function public: RequestObject(const InterfaceObject_sh& interfaceObj,long mytime, long myprocessid, unsigned short myflag, const char* request_id_, const void_allocator &void_alloc) :embeddedInterfaceObject(interfaceObj.a,interfaceObj.b.c_str() ,void_alloc),start_timep(mytime),current_processid(myprocessid), flag(myflag) , request_id(request_id_,void_alloc),myvector(void_alloc) {} }; typedef allocator requestobject_type_allocator; typedef list requestobject_list; void childServiceProcess(); int main(int argc, char *argv[]) { using namespace boost::interprocess; requestobject_list *myRequestObjectList; shared_memory_object::remove("MySharedMemory"); remove_shared_memory_on_destroy remove_on_destroy("MySharedMemory"); { //Create shared memory managed_shared_memory segment(create_only,"MySharedMemory", 1310720); //An allocator convertible to any allocator type void_allocator alloc_inst (segment.get_segment_manager()); //Create allocators char_string_allocator charstringallocator(segment.get_segment_manager()); interfaceobject_type_allocator interface_alloc_inst(segment.get_segment_manager()); requestobject_type_allocator request_alloc_inst(segment.get_segment_manager()); //Construct the shared memory map and fill it myRequestObjectList = segment.construct //(object name), (first ctor parameter, second ctor parameter) ("MyList")(request_alloc_inst); //for(int i = 0; i < 2; ++i) { //InterfaceObject myInterfaceObject(5,"hello boost",alloc_inst); InterfaceObject_sh myInterfaceObject(3,"hello boost",charstringallocator); //RequestObject myRequestObject(myInterfaceObject,3,2,0); //InterfaceObject myInterfaceObject2(8,"dear li",charstringallocator); InterfaceObject_sh *myInterfaceObject2 = segment.construct ("MyInterObject") (8,"darling",charstringallocator); //RequestObject *myRequestObject = segment.construct // ("MyObject") (myInterfaceObject,3,2,0,"hello world",charstringallocator); //InterfaceObject_sh myInterfaceObject3= InterfaceObject_sh(9,"hha,haa"); RequestObject *myRequestObject = segment.construct ("MyObject") (*myInterfaceObject2,3,2,0,"hello world",alloc_inst); //myInterfaceObjectVector->reserve(10); //myInterfaceObjectVector->push_back(*myInterfaceObject2); myRequestObject->myvector.push_back(myInterfaceObject); //myInterfaceObjectVector->insert(myInterfaceObjectVector->begin(), 1,*myInterfaceObject2); //myRequestObject->myvector=myInterfaceObjectVector; myRequestObjectList->push_front(*myRequestObject); } segment.destroy("MyInterObject"); segment.destroy("MyObject"); requestobject_list::iterator myIterator; myIterator=myRequestObjectList->begin(); for(myIterator; myIterator != myRequestObjectList->end(); myIterator++) { printf("The element by Iterator is %d, %ld, %ld ,%u ,%s,%s\n", (myIterator->embeddedInterfaceObject).a,myIterator->start_timep,myIterator->current_processid,myIterator->flag,(myIterator->embeddedInterfaceObject).b.c_str(),myIterator->request_id.c_str()); //printf("The element by Iterator is %d, %ld, %ld ,%u \n", myIterator->start_timep,myIterator->current_processid,myIterator->flag); myIterator->request_id="I change it, you change it , but we can still see it"; printf("The element by vector is %d,%s\n",myIterator->myvector.front().a, myIterator->myvector.front().b.c_str()); myIterator->myvector.front().b="I change darling to www.google.com, I want to know whether I am successful."; } } using namespace std; pid_t child; current_log_level=LOGDEBUG; current_show_level=SHOWDEBUG; //initialize log sub module get_current_dir(); sew_log_dir("mylog"); int j=initialize_log_mutex(); printf("The j is %d \n",j); int k=initialize_process_sharedlistobject_mutex(); printf("The k is %d \n",k); //first at all, check if we can fork a process //InterfaceObject myInterfaceObject; //RequestObject myRequestObject(myInterfaceObject); //myRequestObjectList.push_front(myRequestObject); //begin to fork a process to deal with new task if ((child=fork()) == -1) { //error occurs, log error code and message pid_t currentprocessid=getpid(); char* perrormsg=geterrormsg(); show_msg("main ",SHOWDEBUG,currentprocessid,"fork fail %d %s",errno,perrormsg); logmsg("main ",LOGDEBUG,currentprocessid,"fork fail %d %s",errno,perrormsg); free(perrormsg); } else if(child==0) { //enter into child process, begin to work //childServiceProcess(myRequestObjectList); childServiceProcess(); } else { // return control to parent pid_t currentprocessid=getpid(); char* perrormsg=geterrormsg(); show_msg(" main ",SHOWDEBUG,currentprocessid,"fork success success and back %d",errno,perrormsg); logmsg(" main ",LOGDEBUG,currentprocessid,"fork success success and back %d",errno,perrormsg); free(perrormsg); } release_log_mutex(); release_process_sharedlistobject_mutex(); return 0; } void childServiceProcess() { int find_flag; int del_flag; find_flag=-1; del_flag=-1; pid_t currentprocessid=getpid(); using namespace boost::interprocess; requestobject_list *myRequestObjectList; //get request object from list and change //lock the list try{ //A special shared memory where we can //construct objects associated with a name. //Connect to the already created shared memory segment //and initialize needed resources managed_shared_memory segment (open_only ,"MySharedMemory"); //segment name //Find the list using the c-string name myRequestObjectList = segment.find("MyList").first; RequestObject *myRequestObject=segment.find("MyObject").first; //for(int i = 0; i < 2; ++i) //{ printf("The element is %ld \n",myRequestObjectList->front().flag); printf("The element is %ld \n",myRequestObjectList->front().current_processid); printf("The element is %ld \n",myRequestObjectList->front().start_timep); printf("The element is %ld \n",myRequestObjectList->front().embeddedInterfaceObject.a); printf("The element is %s \n",myRequestObjectList->front().embeddedInterfaceObject.b.c_str()); printf("The element is %s \n",myRequestObjectList->front().request_id.c_str()); printf("The element by vector is %d,%s\n",myRequestObjectList->front().myvector.front().a, myRequestObjectList->front().myvector.front().b.c_str()); //} printf("list size is %d \n",myRequestObjectList->size()); requestobject_list::iterator myIterator; myIterator=myRequestObjectList->begin(); myRequestObjectList->erase(myIterator); //segment.destroy("MyObject"); printf("list size is %d \n",myRequestObjectList->size()); //When done, destroy the vector from the segment segment.destroy("MyList"); } catch(...){ shared_memory_object::remove("MySharedMemory"); throw; } exit(EXIT_SUCCESS); }