CMShm Class Reference

Comunication through shared memory segments. More...

Public Member Functions

 CMShm (long int id, unsigned long int buffer)
 ~CMShm ()
int del ()
int read (char *data)
int write (char *data)

Private Member Functions

unsigned long int myatoi (const char *word)

Private Attributes

long int id
unsigned int buffer_size

Detailed Description

Header for easy comunication aplications through IPC(shared memory segment).

Author:
Miguel Ángel Vílchez López 2009 <sacra@openmatica.es>

Codigo 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 3 of the License, or (at your option) any later version.

Codigo 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, see <http://www.gnu.org/licenses/>.

Examples:

TestShm.cpp.


Constructor & Destructor Documentation

CMShm::CMShm ( long int  id,
unsigned long int  buffer = 1024 
)

Constructor

Parameters:
[in] id The identificator who identifies the portion of memory shared.
[in] buffer The buffer size.

00031 {
00032   this->id = id;
00033   this->buffer_size=buffer;
00034 //  this->debug.debug("Trying to create the shared memory segment...");
00035   if((this->id = shmget((key_t)this->id,this->buffer_size, IPC_CREAT | 0666))<0)
00036   { 
00037 //            this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00038         }
00039 }

CMShm::~CMShm (  ) 

Destructor

00042 {
00043 }


Member Function Documentation

int CMShm::del (  ) 

Remove the shared memory segment.

Returns:
Returns 0 if succesfull, -1 if not.

00078 {
00079 //  this->debug.debug("Trying to delete the shared memory segment...");
00080   if(shmctl(this->id,IPC_RMID,0)==-1)
00081   {
00082 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00083     return -1;
00084   }
00085   return 0;
00086 }

unsigned long int CMShm::myatoi ( const char *  word  )  [private]

Converts an array of chars (10 caracters max.) into numbers.

Returns:
Returns the number of the word.
int CMShm::read ( char *  data  ) 

Read data from a share memory segment.

Parameters:
[out] data Variable to put data.
Returns:
Returns the lenght of bytes wrotes if succesfull, -1 if not.
Examples:
TestShm.cpp.

00046 {
00047   char *shm;
00048 
00049 //  this->debug.debug("Trying to read from the shared memory segment...");
00050   if ((shm = (char *)shmat(this->id, 0, 0)) <(char *) 0)
00051   {
00052 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00053     return -1;
00054   }
00055 
00056   strncpy(data,shm,this->buffer_size);
00057 
00058   return strlen(shm);
00059 }

int CMShm::write ( char *  data  ) 

Write data to a share memory segment.

Parameters:
[in] data Variable data from.
Returns:
Returns 0 if succesfull, -1 if not.
Examples:
TestShm.cpp.

00062 {
00063   char *shm;
00064 
00065 //  this->debug.debug("Trying to write to the shared memory segment...");
00066   if ((shm = (char *)shmat(this->id, 0, 0)) <(char *) 0)
00067   {
00068 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00069     return -1;
00070   }
00071 
00072   strncpy(shm,data,this->buffer_size);
00073 
00074   return 0;
00075 }


Field Documentation

unsigned int CMShm::buffer_size [private]

The buffer size.

long int CMShm::id [private]

the id who identifies the ipc.

 All Data Structures Functions Variables
Generated on Mon Sep 12 07:59:09 2011 for API MokArdu by  doxygen 1.6.3