3、查找功能的实现,流程如下:
先malloc一段空间出来,然后往这段内存中放查找出来的订单,最后在接收端释放这段内存。
附上我的查找功能的实现:
/*查找函数*/
char* Find_Key(char *q,int *ordernum)
{
//int i;
static int j = 0;//out的下标,
head* tmp;
uint32_t shmaddr = 0;
OrderValue *exc;
uint32_t keyaddr;
pthread_mutex_t* mutex = (pthread_mutex_t*)Mutex_Addr;
Flag_Count* flagcount = (Flag_Count*)Flag_Addr;
int flag = flagcount->flag;
//printf("Find: flag=%d \n",flagcount->flag);
pthread_mutex_lock(mutex);//加锁
if(flag == -1)
{
pthread_mutex_unlock(mutex);
printf("flag == -1,Not init hash table yet!\n");
return NULL;
}
if(flag == 0)
{
shmaddr = Block0_Addr;
(flagcount->count0)++;
pthread_mutex_unlock(mutex);
}
else if(flag == 1)
{
shmaddr = Block1_Addr;
(flagcount->count1)++;
pthread_mutex_unlock(mutex);
}
if((int)shmaddr == -1)
{
printf("error in Check_Flag\n");
return NULL;
}
tmp = (head*)shmaddr; //接收共享内存的头地址
/*处理 订单上限 Order_MaxNum*/
int memsize = tmp->Order_MaxNum * OrderId_Num;
char *out = (char*)malloc(memsize);
if(out == NULL )
{
printf("error in malloc for order\n");return NULL;
}
memset(out,0,memsize);
uint32_t hash = Hash_Fun(q,strlen(q),987654)%KeyWord_Num;
//printf("Find:str=%s,hash=%d\n",q,hash);
if( *((uint32_t *)(shmaddr + tmp->HEAD_hash + hash*4)) == 0 )//没有匹配到
{
printf("Find1:not find the keyword\n");
*ordernum = 0;
/*加锁 count--*/
pthread_mutex_lock(mutex);
if(flag == 0)
{
(flagcount->count0)--;
pthread_mutex_unlock(mutex);
}
else if(flag == 1)
{
(flagcount->count1)--;
pthread_mutex_unlock(mutex);
}
return out;
}
else
{
keyaddr = Check_keyword(*((uint32_t*)(shmaddr+tmp->HEAD_hash+hash*4))+shmaddr,\
shmaddr,q );
//printf("%d %d\n",sizeof(int),sizeof(long));
//printf("keyaddr:%x ****%p \n",keyaddr,keyaddr);
if( (uint32_t)keyaddr > 0 )//没有相同关键字
{
exc = (OrderValue*)( *((uint32_t*)keyaddr) + shmaddr );
}
else
{
printf("Find2:not find the keywords\n");
*ordernum = 0;
/*加锁 count--*/
pthread_mutex_lock(mutex);
if(flag == 0)
{
(flagcount->count0)--;
pthread_mutex_unlock(mutex);
}
else if(flag == 1)
{
(flagcount->count1)--;
pthread_mutex_unlock(mutex);
}
return out;
}
}
// printf("find order head: %s**\n",exc->value);
while(1)
{
if( exc->next_order == 0)
{
//printf(" insert order :%s j:%d\n",exc->value,j);
strcpy((char *)(out + j*OrderId_Num),exc->value);//将订单号放入out数组中
j++;
break;
}
//printf("111 insert order :%s j:%d\n",exc->value,j);
strcpy((char *)(out + j*OrderId_Num),exc->value);//将订单号放入out数组中
j++;
exc = (OrderValue*)(shmaddr + exc->next_order);
}
*ordernum = j;
// printf("ordernum :%d\n",*ordernum);
j = 0;//将 J 置零
/*加锁 count--*/
pthread_mutex_lock(mutex);
if(flag == 0)
{
(flagcount->count0)--;
pthread_mutex_unlock(mutex);
}
else if(flag == 1)
{
(flagcount->count1)--;
pthread_mutex_unlock(mutex);
}
#if 0
for(i = 0;i<*ordernum;i++)
printf("find %s\n",(out+i*OrderId_Num));
#endif
return out;
}
4、查找功能、插入功能都是多进程调用,所以需要互斥锁机制,将互斥锁放在共享内存中,每次调用功能时都要获得锁,将引用计数做加减。
/*
*互斥锁的初始化
*
* 失败返回-1
* 成功返回共享内存ID
*
* */
int initMutex(void)
{
int shm_id;
int shm_size;
int ret;
shm_size = sizeof(pthread_mutex_t);
pthread_mutex_t *mutex = NULL;
pthread_mutexattr_t mutexattr;
if((shm_id = shmget(/*key*/Mutex_key, shm_size, IPC_CREAT|0666)) < 0)
{
if(errno == EEXIST)
{
shm_id = shmget(Mutex_key,shm_size,0666);
mutex = (pthread_mutex_t*)shmat(shm_id, NULL, 0);
}
}
else
{
mutex = (pthread_mutex_t*)shmat(shm_id, NULL, 0);
}
memset(mutex,0,sizeof(pthread_mutex_t));
printf("be zero success\n");
pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);
ret = pthread_mutex_init(mutex,&mutexattr);
if (ret < 0)
{
printf("error in mutex init\n");
return -1;
}
return shm_id;
}
/*
*初始化标志位、引用计数
* 成功返回共享内存ID
*失败返回 -1
* */
int Flag_init(void)
{
int shm_id;
int shm_size = sizeof(Flag_Count);
uint32_t shmaddr = 0;
if((shm_id = shmget(/*key*/FlagCount_key, shm_size, IPC_CREAT|0666)) < 0 )
{
if( errno == EEXIST )
{
shm_id = shmget(/*key*/FlagCount_key,shm_size,0666);
shmaddr = (uint32_t )shmat(shm_id,NULL,0);
}
}
else
{
shmaddr = (uint32_t )shmat(shm_id,NULL,0);
}
if(shm_id == -1)
{
perror("get Flag shm error");
return -1;
}
memset((void*)shmaddr,0,shm_size);
((Flag_Count*)shmaddr)->flag = -1;//置flag为-1
printf("init Flag success: flag = %d count0 = %d count1 = %d\n",\
((Flag_Count*)shmaddr)->flag,((Flag_Count*)shmaddr)->count0,
((Flag_Count*)shmaddr)->count1);
return shm_id;
}
如需源码,请发站内信。。。。。。。。。。。。