📄 share_mem.cpp
字号:
#include "share_mem.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <pthread.h>
#ifdef __cplusplus
}
#endif
#define PERM (S_IRUSR|S_IWUSR)
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
pthread_mutex_t mutex;
pthread_mutex_t mutex_log;
struct share_memory *pShm;
static key_t key_shm;
static key_t key_sem;
static key_t key_que;
static int queue_id;
static int shmid;
static int semid;
/*--------------------------------------------------------------------------------------------**/
void local_log(char *info)
{
pthread_mutex_lock(&mutex_log);
char* p_today_str;
char filename[128];
FILE *fp_log;
p_today_str=get_today_str();
char *app_path;
app_path= getenv("PORTAL_HOME2");
strcpy(filename,app_path);
strcat(filename,"/logs/");
strcat(filename,p_today_str);
strcat(filename,".log");
fp_log = fopen(filename, "a");
if(fp_log!=NULL)
{
//fprintf(fp_log,"-------------------------------------------- \n");
fprintf(fp_log,"[%s] %s\n",trans_time(),info);
printf("[%s] %s\n",trans_time(),info);
//fprintf(fp_log,"-------------------------------------------- \n");
}
fclose(fp_log);
pthread_mutex_lock(&mutex_log);
}
//------------------------------------------------------------------------------------------
int CreateShm()
{
union semun sem;
void *segptr;
//*创建共享内存段*/
key_shm=(key_t)0x1000;
key_sem=(key_t)0x1500;
key_que=(key_t)0x2000;
if((shmid=shmget(key_shm,sizeof(struct share_memory),IPC_CREAT|IPC_EXCL|0666))==-1)
{
if((shmid=shmget(key_shm,sizeof(struct share_memory),0))==-1)
{
printf("create share memory error!\n");
return -1;
}
}
if((segptr=shmat(shmid,0,0))==(void*)-1)
{
printf("attach to share memory error!\n");
return -1;
}
pShm=(struct share_memory *)segptr;
#ifdef FIRST_MACHINE
pShm->CurrentSN=0;
#else
pShm->CurrentSN=30001;
#endif
pShm->count=0;
if((semid = semget(key_sem,2,0666|IPC_CREAT)) < 0)
{
printf("create sem error!\n");
return -1;
}
sem.val = 1;
if(semctl(semid,0,SETVAL,sem) < 0)
{
shmctl(shmid,IPC_RMID,0);
semctl(semid,IPC_RMID,0);
return -1;
}
if((queue_id=msgget(key_que,IPC_CREAT|0660))==-1)
{
switch(errno)
{
case EACCES:perror("message queue creating permission denied.\n");break;
case EEXIST:perror("to create a message queue be existed.\n");break;
case EIDRM:perror("queue be removed.\n");break;
case ENOMEM:perror("no enough memory to creat message queue.\n ");break;
case ENOSPC:perror("queue number on top.\n");break;
default: break;
}
shmctl(shmid,IPC_RMID,0);
semctl(semid,IPC_RMID,0);
return -1;
}
else pShm->queue_id=queue_id;
return 0;
}
/*--------------------------------------------------------------------------------------------**/
int ConnectShm(struct share_memory *pShareMem)
{
void* segptr;
key_shm=(key_t)0x1000;
key_sem=(key_t)0x1500;
if((shmid=shmget(key_shm,sizeof(struct share_memory),PERM))==-1)
{
return -1;
}
if((segptr=shmat(shmid,0,0))==(void *)-1)
{
return -1;
}
pShm=pShareMem=(struct share_memory *)segptr;
if((semid = semget(key_sem,2,PERM)) < 0)
{
shmdt((void*)pShareMem);
pShareMem=NULL;
pShm=NULL;
return -1;
}
return 0;
}
/*--------------------------------------------------------------------------------------------**/
int DisconShm()
{
shmdt((void*)pShm);
return 0;
}
/*--------------------------------------------------------------------------------------------**/
int RemoveShm()
{
int result;
result=0;
if(msgctl(queue_id,IPC_RMID,0)==-1)
{
printf("remove message queue error!\n") ;
result=-1;
}
shmdt((void*)pShm);
if(shmctl(shmid,IPC_RMID,0) < 0)
{
printf("destroy share memory error!\n") ;
result=-1;
}
if(semctl(semid,IPC_RMID,0) < 0)
{
printf("destroy sem error!\n");
result=-1;
}
return result;
}
/*--------------------------------------------------------------------------------------------**/
int LockShm()
{
struct sembuf semwait;
semwait.sem_num=0;
semwait.sem_op=-1;
semwait.sem_flg=0;
if( semop(semid,&semwait,1)==-1 )
return -1;
return 0;
}
/*--------------------------------------------------------------------------------------------**/
int unLockShm()
{
struct sembuf semwait;
semwait.sem_num=0;
semwait.sem_op= 1;
semwait.sem_flg=0;
if( semop(semid,&semwait,1)==-1 )
return -1;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -