virtual int Init(size_t memlen, unsigned int bloc_size, int id = 0) { LockInit(id); size_t mem_size; bool bNewCreate; ssize_t iInitType; mem_size = memlen; char buffer[256]; snprintf(buffer, sizeof(buffer), "touch .mem_cache_%d_key_value_mng", id); system(buffer); snprintf(buffer, sizeof(buffer), ".mem_cache_%d_key_value_mng", id); key_t shm_key = ftok(buffer, 'A'); printf("Creating %s cache[%d] shm: key[0x%08x], size[%u B].\n", buffer, id, (unsigned int)shm_key,(unsigned int)mem_size); assert(shm_key != -1); m_pMemKeyCache = new MemKeyCache(); m_iMemSize = mem_size; int err; m_p_shm_addr = CreateShm(shm_key, mem_size, err, bNewCreate, 0); if (!m_p_shm_addr) { printf("attach %s key[0x%08x] size[%d] error\n", buffer, (unsigned int)shm_key, (int)mem_size); return -1; } iInitType = bNewCreate?emInit:emRecover; ssize_t atBytes = m_pMemKeyCache->AttachMem(m_p_shm_addr, mem_size, 0, iInitType, bloc_size); if (atBytes < 0) { return -2; } return 0; }
int QueueOperNoLock::CreateQueue(key_t tQueueKey,int iQueueSize) { int iQueueShmId; char *pQueueShm = NULL; this->~QueueOperNoLock(); if ( iQueueSize <= 0 ) iQueueSize = DEFAULT_QUEUE_SIZE; m_tQueueKey = tQueueKey; if ( (iQueueShmId = CreateShm(m_tQueueKey,iQueueSize + sizeof(QueueCtl),SHM_CREATE)) < 0 ) { snprintf(m_sErrMsg,sizeof(m_sErrMsg),"Create queue fail:%s",strerror(errno)); return -1; } pQueueShm = (char *)shmat(iQueueShmId,NULL,0); if ( pQueueShm == NULL || (long)pQueueShm == -1 || SemLockInit(m_tQueueKey,SHM_CREATE) < 0 ) { snprintf(m_sErrMsg,sizeof(m_sErrMsg),"Create queue fail:%s",strerror(errno)); shmctl(iQueueShmId,IPC_RMID,(struct shmid_ds *)NULL); return -1; } m_pQueueCtl = (QueueCtl *)pQueueShm; m_pPkgRoot = (char *)(pQueueShm + sizeof(QueueCtl)); m_pQueueCtl->cValidFlag = 1; m_pQueueCtl->iSpaceSize = iQueueSize; m_pQueueCtl->iQueueHeadPos = 0; m_pQueueCtl->iQueueTailPos = 0; return 0; }
int CMessageQueue::initShmMode(const char *szShmConfigFile,unsigned int iDataSize) { int iErrno; bool bNewCreate; m_pData = CreateShm(_get_map_key(szShmConfigFile,'M'), iDataSize, iErrno, bNewCreate, 1); if (NULL == m_pData) { return -1; } init(m_pData, iDataSize); // check if (false == bNewCreate) { if (m_pQueueHeader->iDataSize != iDataSize) { return -2; } if (m_pQueueHeader->iDataSize <= get_usage_size()) { return -3; } if (m_pQueueHeader->iBegin >= iDataSize || m_pQueueHeader->iEnd > iDataSize) { return -4; } } return 0; }
int main(void) { int rc; struct GShmStruct sh; rc=CreateShm(1,&sh,1024*16,1); printf("CreateShm rc=%d\n",rc); rc=LockShm(&sh,GSH_LOCK_RW); printf("LockShm rc=%d\n",rc); return 0; }
int QueueOperNoLock::DestroyQueue() { int iQueueShmId; if ( !m_pQueueCtl ) { snprintf(m_sErrMsg,sizeof(m_sErrMsg),"Destory queue fail:Not init locally"); return -1; } m_pQueueCtl->cValidFlag = 0; this->~QueueOperNoLock(); iQueueShmId = CreateShm(m_tQueueKey,0,SHM_ATTACH); shmctl(iQueueShmId,IPC_RMID,(struct shmid_ds *)NULL); SemLockRm(); return 0; }
/*void** malloc2d(int line, int col,int unitsize) { int i; int col_size=col*unitsize; int index_size=line*sizeof(void*); void **a=(void**)malloc(index_size+line*col_size); char *data_start=(char*)a+index_size; for(i=0;i<line;++i) a[i]=data_start+i*col_size; return a; }*/ int init_loop(struct loopbuf** loop,int *shmid,char *shmpath,int id){ // int shmid; *shmid=CreateShm(shmpath,id,sizeof(UC_ShmMemory)); *loop=(UC_ShmMemory*)AttachShm(*shmid); //my_lock_init(*loop); (*loop)->looplen=ROW; (*loop)->buflen=COL; (*loop)->tail=0; (*loop)->head=0; printf("aaa\n"); return 1; //sem_init(&loop->sem,0,0); // memset(loop->recvlen,0,MAX_RECV_QUEUE*sizeof(int)); // loop->p=(char**)malloc2d(row,col,typelen); }
int QueueOperNoLock::AttachQueue(key_t tQueueKey) { int iQueueShmId; char *pQueueShm = NULL; this->~QueueOperNoLock(); m_tQueueKey = tQueueKey; if ( (iQueueShmId = CreateShm(m_tQueueKey,0,SHM_ATTACH)) < 0 ) { snprintf(m_sErrMsg,sizeof(m_sErrMsg),"Attach queue fail:%s",strerror(errno)); return -1; } pQueueShm = (char *)shmat(iQueueShmId,NULL,0); if ( pQueueShm == NULL || (long)pQueueShm == -1 || SemLockInit(m_tQueueKey,SHM_ATTACH) < 0 ) { snprintf(m_sErrMsg,sizeof(m_sErrMsg),"Attach queue fail:%s",strerror(errno)); return -1; } m_pQueueCtl = (QueueCtl *)pQueueShm; m_pPkgRoot = (char *)(pQueueShm + sizeof(QueueCtl)); return 0; }
bool svrInitCommandHandler(SerialCom* comm,PipeShell* shell,char* sIfName,bool dbg_gws) { struct itimerval interval; ShowStatusBar("%s", "Handler Init....\n"); if (NULL == comm || NULL == shell || NULL == sIfName) return false; static_chp.m_comm = comm; static_chp.m_shell = shell; static_chp.m_ifName = sIfName; static_chp.m_nQid = OpenMessageQueue(1); static_chp.m_pKpi = CreateShm(&static_chp.m_nIdentifier); ShowStatusBar("%s", "MSQ & SHM OK\n"); if (static_chp.m_nQid < 0 || NULL == static_chp.m_pKpi) { if (static_chp.m_nQid < 0) { ShowStatusBar("Error on create message queue,error=%d", errno); svrSetErrorNo(errno); return false; } else if (NULL == static_chp.m_pKpi) { ShowStatusBar("Error on open share memory,error=%d", errno); svrSetErrorNo(errno); return false; } } ShowStatusBar("%s", "Turnning on GWS Tx....\n"); ScSend(static_chp.m_comm, "txon\n", 5); ShowStatusBar("%s", "Adjusting GWS channel....\n"); iw_setchannel(-1,static_chp.m_pKpi,static_chp.m_nIdentifier); pthread_mutex_init(&mutex_lock_timer,NULL); interval.it_value.tv_sec = 0; interval.it_value.tv_usec = TIME_SLOT; interval.it_interval.tv_sec = 0; interval.it_interval.tv_usec = TIME_SLOT; static_debug_gws = dbg_gws; signal(SIGALRM, svrTimer); setitimer(ITIMER_REAL, &interval, NULL); ShowStatusBar("%s", "Initialize Handler OK!"); return true; }
int main(int argc, char *argv[]) { CreateShm(TSSHMNAME, TSSHMSZ); return(0); }