分类:
2008-10-13 16:51:22
//*********************************************
// Version: 1.0.0
// Author:
// Copyright: copyleft,free
// Purpose: SharedMem
// Date: 2004-9-3
//*********************************************
#ifndef CSHAREDMEM_H_
#define CSHAREDMEM_H_
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class CSharedMem
{
public:
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
CSharedMem();
~CSharedMem();
private:
//生成信号量
int Create_Sem(key_t key);
//删除信号量
void Delete_sem();
int p();
int v();
int m_nSegSize; //共享内容大小
char* m_pszContent; //映射区指针
int m_nSemid; //创建信号Key
pthread_mutex_t m_hMutex; //线程锁
public:
bool InitSegSize( int nSegSize=1024 );
bool Read(char* pszContent, int nReadByteNum, bool bDeleteContent=true);
bool Writer(char* pszContent, int nWriterByteNum);
};
#endif //CSHAREDMEM_H_
******************************************************************
******************************************************************
******************************************************************
******************************************************************
//*********************************************
// Version: 1.0.0
// Author:
// Copyright: copyleft,free
// Purpose: SharedMem
// Date: 2004-9-3
//*********************************************
#include "SharedMem.h"
CSharedMem::CSharedMem()
{
m_pszContent = NULL;
/* 用默认属性初始化一个互斥锁对象*/
pthread_mutex_init (&m_hMutex,NULL);
printf("\nCSharedMem Start ...");
}
CSharedMem::~CSharedMem()
{
munmap( m_pszContent, sizeof(char)*m_nSegSize);
printf(" \n umap OK \n");
Delete_sem();
pthread_mutex_destroy(&m_hMutex);
printf("\nCSharedMem End \n");
}
//生成信号量
int CSharedMem::Create_Sem(key_t kKey)
{
union semun sem ;
sem.val=0;
m_nSemid = semget(IPC_PRIVATE, 1, IPC_CREAT|0666);
if (m_nSemid == -1)
{
printf(" create semaphore error\n" );
return -1;
}
//初始化信号量
semctl(m_nSemid, 0, SETVAL, sem);
return m_nSemid;
}
//删除信号量
void CSharedMem::Delete_sem()
{
union semun sem ;
sem.val=0;
semctl(m_nSemid, 0, IPC_RMID, 0);
}
int CSharedMem::p()
{
//struct sembuf sops={0, +1,IPC_NOWAIT};
//return(semop(m_nSemid, &sops, 1));
printf("\n开始进入锁定...\n");
pthread_mutex_lock(&m_hMutex);
printf("...已经锁定\n");
}
int CSharedMem::v()
{
//struct sembuf sops={0, -1,IPC_NOWAIT};
//return(semop(m_nSemid, &sops, 1));
printf("\n开始进入解锁...\n");
pthread_mutex_unlock(&m_hMutex);
printf("...已经解锁\n");
}
bool CSharedMem::InitSegSize( int nSegSize )
{
// 初始化信号量
key_t kKey;
kKey = ftok("/", 0);
int nRet = Create_Sem( kKey );
if( nRet == -1 )
return false;
char* pFileName = "./SharedMem1111";
m_nSegSize = nSegSize;
// 移至文件尾,加上'\0'字符
int fd = open(pFileName, O_CREAT|O_APPEND|O_RDWR, 0777);
lseek(fd, sizeof(char)*m_nSegSize-1, SEEK_SET);
write(fd, "", 1);
void* pVoid = mmap( NULL, sizeof(char)*m_nSegSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if( pVoid == MAP_FAILED )
{
printf(" Mmap ERROR 1\n");
return false;
}
m_pszContent = (char*)pVoid;
//strncpy(m_pszContent, "abcdef", 5);
printf(" \n initiallize over:%s \n",m_pszContent);
return true;
}
bool CSharedMem::Read(char* pszContent, int nReadByteNum, bool bDeleteContent)
{
p();
memcpy(pszContent, m_pszContent, nReadByteNum);
pszContent[nReadByteNum] = '\0';
if( bDeleteContent )
memset(m_pszContent, '\0', bDeleteContent);
v();
return true;
}
bool CSharedMem::Writer(char* pszContent, int nWriterByteNum)
{
p();
strncat(m_pszContent, pszContent, nWriterByteNum);
v();
return true;
}