//取可用部门共享内存大小 int get_adv_memlen() { int *plen = NULL; int len = 0; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen < 0 || *plen > (int)MAX_DEP_VALUE_LEN) { semunlock(); return -1; } len = *plen; semunlock(); return MAX_DEP_VALUE_LEN - len; }
//设置业务部门的数据到共享内存 int adv_attr_set(int attr_id , size_t len , char* pvalue) { char *p = NULL; int *plen = NULL; if (attr_id < DEP_ATTTIBUTE_BASE || len == 0 || pvalue == NULL) return -1; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1;// 修正无法获取共享内存问题,modified by arrowliu, 2006-03-29 } //取可用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen > MAX_DEP_VALUE_LEN || (MAX_DEP_VALUE_LEN - *plen) < len + 8) // modified by arrowliu, 2006-08-10 { semunlock(); return -1; } //copy value p = g_pBusiShareMem; p += sizeof(int);//all the data length p += *plen; *plen += len + 2*sizeof(int);//increase length,一个整形是长度,一个是id //first 4byte is length of data *(int*)p = htonl(len); //second 4byte is attribute id of data p += sizeof(int); *(int*)p = htonl(attr_id); //other buffer for the data p += sizeof(int); memcpy(p , pvalue , len); semunlock(); return 0; }
static void sigHandler(int signo) { if(signo == common_sig_t[0]) // start { if( semVal(pkg->sem_id , 0) > 0 ) { semlock(pkg->sem_id , 0); } } else if(signo == common_sig_t[1]) // stop { if( semVal(pkg->sem_id , 0) == 0) { semunlock(pkg->sem_id , 0); } } else if(signo == common_sig_t[2]) // koniec { semlock(pkg->sem_id, 4); // ustawienie flagi sygnalu do zabicia procesu nr 2 if( semVal(pkg->sem_id , 0) > 0 ) // jesli dostaniemy podczas waita { semlock(pkg->sem_id , 0); } } }
static void * mainLoop(void * write_pipe_fd ) { char * shm = pkg->shm_ptr; int * fd = (int *)write_pipe_fd; killfd = *fd; unsigned char readed_bytes; while(1) { semlock(pkg->sem_id, 2); if( !semVal(pkg->sem_id , 4) ) { kill(pid_p3,communication_sig_t[2]); // poinformowanie procesu 3 if( semVal(pkg->sem_id, 3) ) // ustawienie flagi sygnalu do zabicia procesu nr 1 { semlock(pkg->sem_id, 3); } if( semVal(pkg->sem_id , 0) > 0 ) { semlock(pkg->sem_id , 0); // jbc odblokowanie jesli dostalismy kill podczas stopu } semunlock(pkg->sem_id, 1); // wpuszczenie proc 1 do sprawdzenia flagi i zabicia sie write(killfd, &killfd , sizeof(char) ); // odblokowanie procesu nr 3 aby sie zabil break; /*pthread_cancel(thread); pthread_testcancel(); */ } readed_bytes = (unsigned char)shm[0]; write(*fd, &shm[1] ,readed_bytes); semunlock(pkg->sem_id , 1); } pthread_exit(EXIT_SUCCESS); }
int free_adv_mem() { int *plen = NULL; // int len = 0; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen < 0 || *plen > (int)MAX_DEP_VALUE_LEN) { semunlock(); return -1; } memset(g_pBusiShareMem + sizeof(int) , 0 , *plen); *plen = 0; semunlock(); return 0; }
//取部门共享内存的内容,注意pOut调用者分配,且大小一定要等于或大于len int get_adv_mem(size_t offset , size_t len , char* pOut) { int actual_size = len; int *plen = NULL; if (offset >= MAX_DEP_VALUE_LEN || len < 1 || pOut == NULL) return -1; memset(pOut , 0 , len); //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (actual_size > *plen) actual_size = *plen; memcpy(pOut , g_pBusiShareMem + sizeof(int) + offset ,actual_size); semunlock(); return actual_size; }
static void communicationSigHandler(int signo) { if(signo == communication_sig_t[0]) // start { if( semVal(pkg->sem_id , 0) > 0 ) { semlock(pkg->sem_id , 0); } } else if(signo == communication_sig_t[1]) // stop { if( semVal(pkg->sem_id , 0) == 0) { semunlock(pkg->sem_id , 0); } } else if(signo == communication_sig_t[2]) // koniec { sigHandler(common_sig_t[2]); } }