If the shared memory does not already exist, create the shared memory; and create the object.
If the shared memory already exists, skip the object of construction. Initializer:: m_shmid records this identifier, ObjectWithEvents:: ms_pObjectWithEvents recorded on this shared object. Even if all processes with detachment, the shared memory will not be destroyed. So that you can use the ipcrm explicitly deleted or use ipcs commands for viewing. Test program compile as follows: g ++-g-oshm_clientshm_client1.cppObjectWithEvents.cppInitializer.cpp console, the results of running this program as follows: list 8. console results $./shm_clientshm_client1.cpp: 16Messagefrompid (4332): $ ipcs------SharedMemorySegments--------keyshmidownerpermsbytesnattchstatus0x00001234327686sachin6661360 $./shm_clientshm_client1.cpp: 16Messagefrompid (4333): $ ipcrm-m327686ObjectWithEvents instance brings together the events from various processes. It can free up the current process only the registered events. This design pattern describes two points: any of a set of events that access by a mutex to protect. In the event issued before, use the process ID of the filter. Used for IPC shared memory and event cache now let's look at how to use shared memory and event cache for interprocess communication. If the event is in a shared object in the cache, then they may be sent later. The receiving process must query the shared object what event. Thus, by using a synchronization model, can achieve interprocess communication. This is the development of this design pattern. To add two IObjectWithEvents method, as follows: list 9. adding a method to IObjectWithEvents classIObjectWithEvents {public: virtualboolEnqueueEvent (constchar * msg) = 0; virtualboolPollForEvents () = 0;}; EnqueueEvent () simply shared object to add an event cache, PollForEvents () is on the cached data is retrieved. Shm_client1 will use EnqueueEvent () method, as follows: powe-> EnqueueEvent ("Messagefromshm_client1"); shm_client2 (actually a copy of shm_client1) will use PollForEvents () method, as follows: powe-> EnqueueEvent ("Messagefromshm_client2"); powe-> PollForEvents (); in addition, we give something ObjectWithEvents added, as follows: list 10. changes to ObjectWithEvents classObjectWithEvents: publicIObjectWithEvents {public: virtualboolEnqueueEvent (constchar * msg); virtualboolPollForEvents ();//Theeventcaceenum {MAX_EVENTS = 16, MAX_EVENT_MSG = 256,}; longm_nEvents; pid_tm_alPIDEvents [MAX_EVENTS]; charm_aaMsgs [MAX_EVENTS] [MAX_EVENT_MSG];}; These generate a new constructor: ObjectWithEvents:: ObjectWithEvents (): m_npEI (0), m_nEvents (0) {} EnqueueEvent () event (for example, each issue of event messages and process ID) to a queue. PollForEvents () loop through the queue, and one by one on the events in the queue, call the OnEvent (). Manifest 11.EnqueueEventboolObjectWithEvents:: EnqueueEvent (constchar * msg) {if (NULL == msg) {returnfalse;} if(MAX_EVENTS==m_nEvents){//IEventSinkcollectionullreturnfalse;} intbRetVal=Initializer::LockMutex();if(0!=bRetVal){returnfalse;} m_alPIDEvents[m_nEvents]=getpid();strncpy(m_aaMsgs[m_nEvents++],msg,MAX_EVENT_MSG-1);if((0==bRetVal)&&(0!=Initializer::UnlockMutex())){//Dealwitherror.} returntrue;} boolObjectWithEvents::PollForEvents(){if(0==m_nEvents){returntrue;} intbRetVal=Initializer::LockMutex();if(0!=bRetVal){returnfalse;} pid_tpid=getpid();for(longi=0;iLinxu Security , the latest breaking news and information on security, linux, open source, firewalls
Thursday, December 30, 2010
In Linux deal with shared object synchronization events
Labels:
[:]
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment