当前位置: 技术问答>linux和unix
怎样在共享内存里面建立二叉树,非常着急在线等,拜托各位高手了
来源: 互联网 发布时间:2015-09-21
本文导语: redhat linux 9.0、gcc 代码如下: #include #include #include #include #include #include #include #include #include #include #include #include #include "../hour/val.h" #define KEYKEY 539 #define SIZE (150 * 1024 * 1024) int shmid; long read...
redhat linux 9.0、gcc
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "../hour/val.h"
#define KEYKEY 539
#define SIZE (150 * 1024 * 1024)
int shmid;
long read_count=0;
struct Keylist
{
long ID;
char Key[256];
struct Keylist *rptr;
struct Keylist *lptr;
};
typedef struct Keylist Keynode;
typedef Keynode *Keylink;
Keylink Keyroot;
Keylink keyptr_temp;
// 关键字开始
int main(int argc, char *argv[])
{
FILE *fp;
char *def_host_name=NULL,query[1024];
char DB[50];
char KeyLine[1024];
char KeyLineTemp[1024];
char *KeyLineStr;
char KeyLinePan[]="`";
char KeyStr[1024];
char path[255];
char cmdstr[255]="";
long ID=0;
Keylink Keyinsert();
Keyroot=NULL;
shmid=shmget(KEYKEY,SIZE,IPC_CREAT|0600);
Keyroot=(Keylink)shmat(shmid,NULL,SHM_RND);
Key_count_display_shm(Keyroot);
shmdt(Keyroot);
if(read_count == 1)
{
// 如果内容已经被读入共享内存则不做,否则就做下面的过程
printf("tread KEYn");
Keyroot=NULL;
shmid=shmget(KEYKEY,SIZE,IPC_CREAT|0600);
sprintf(cmdstr,"mkdir -p %s%s%s;chmod 777 %s%s%s -R",textdb_path2,argv[2],textdb_path3,textdb_path2,argv[2],textdb_path3);
//printf("%sn",cmdstr);exit(0);
system(cmdstr);
sprintf(path,"%s%s%sKeyword",textdb_path2,argv[2],textdb_path3);
//printf("%sn",path);exit(0);
//sprintf(path,"%s%sKeyword",textdb_path2,argv[2]);
if((fp=fopen(path,"r"))==NULL)
{
printf("KeyList file not found!n");
perror("");
//exit(0);
}else
{
while(fgets(KeyLine,1024,fp))
{
//printf("%s",KeyLine);
KeyLine[strlen(KeyLine)-1]='';
strcpy(KeyLineTemp,KeyLine);
KeyLineStr=KeyLineTemp;
ID=atol(strsep(&KeyLineStr,KeyLinePan));
strcpy(KeyStr,strsep(&KeyLineStr,KeyLinePan));
Keysearch(Keyroot,KeyStr,ID,0);
}
}
//Key_display_shm(Keyroot);
shmdt(Keyroot);
}else
{
printf(" KEY already inputn");
}
}
int Keysearch(Keylink ptr,char KeyLine[1024],long ID,int i)
{
int found=0;
Keylink fptr;
Keylink Keyinsert();
if(ptr==NULL)
{
//ptr=root=(llink)malloc(sizeof(node));
ptr=Keyroot=keyptr_temp=(Keylink)shmat(shmid,NULL,SHM_RND);
ptr->rptr=NULL;
ptr->lptr=NULL;
//printf("#%i#n",sizeof(node));exit(0);
sprintf((ptr->Key),"%s",KeyLine);
ptr->ID=ID;
}else
{
while((found!=1) && (ptr!=NULL))
{
if(!strcmp(ptr->Key,KeyLine))
{
//printf("found %sn",LineInfo->Cookie);
//tupdate(ptr,LineInfo,URLID);
found=1;
return 0;
}else
{
if(strcmp(ptr->Key,KeyLine)>0)
{
fptr=ptr;
ptr=ptr->lptr;
}else
{
fptr=ptr;
ptr=ptr->rptr;
}
}
}
if(found==0)
{
Keyinsert(fptr,KeyLine,ID,i);
}
}
return 1;
}
Keylink Keyinsert(Keylink fptr,char KeyLine[1024],long ID,int i)
{
Keylink nptr;
nptr=(keyptr_temp+1);
keyptr_temp=nptr;
nptr->rptr=NULL;
nptr->lptr=NULL;
sprintf((nptr->Key),"%s",KeyLine);
nptr->ID=ID;
if(strcmp(fptr->Key,KeyLine)>0)
{
fptr->lptr=nptr;
}else
{
fptr->rptr=nptr;
}
return nptr;
}
Key_display_shm(Keylink ptr)
{
if(ptr!=NULL)
{
printf("%ld`%sn",ptr->ID,ptr->Key);
Key_display_shm(ptr->lptr);
Key_display_shm(ptr->rptr);
}
}
Key_count_display_shm(Keylink ptr)
{
if(ptr!=NULL)
{
//printf("%ld`%sn",ptr->ID,ptr->Key);
read_count++;
Key_count_display_shm(ptr->lptr);
Key_count_display_shm(ptr->rptr);
}
}
// 关键字结束
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "../hour/val.h"
#define KEYKEY 539
#define SIZE (150 * 1024 * 1024)
int shmid;
long read_count=0;
struct Keylist
{
long ID;
char Key[256];
struct Keylist *rptr;
struct Keylist *lptr;
};
typedef struct Keylist Keynode;
typedef Keynode *Keylink;
Keylink Keyroot;
Keylink keyptr_temp;
// 关键字开始
int main(int argc, char *argv[])
{
FILE *fp;
char *def_host_name=NULL,query[1024];
char DB[50];
char KeyLine[1024];
char KeyLineTemp[1024];
char *KeyLineStr;
char KeyLinePan[]="`";
char KeyStr[1024];
char path[255];
char cmdstr[255]="";
long ID=0;
Keylink Keyinsert();
Keyroot=NULL;
shmid=shmget(KEYKEY,SIZE,IPC_CREAT|0600);
Keyroot=(Keylink)shmat(shmid,NULL,SHM_RND);
Key_count_display_shm(Keyroot);
shmdt(Keyroot);
if(read_count == 1)
{
// 如果内容已经被读入共享内存则不做,否则就做下面的过程
printf("tread KEYn");
Keyroot=NULL;
shmid=shmget(KEYKEY,SIZE,IPC_CREAT|0600);
sprintf(cmdstr,"mkdir -p %s%s%s;chmod 777 %s%s%s -R",textdb_path2,argv[2],textdb_path3,textdb_path2,argv[2],textdb_path3);
//printf("%sn",cmdstr);exit(0);
system(cmdstr);
sprintf(path,"%s%s%sKeyword",textdb_path2,argv[2],textdb_path3);
//printf("%sn",path);exit(0);
//sprintf(path,"%s%sKeyword",textdb_path2,argv[2]);
if((fp=fopen(path,"r"))==NULL)
{
printf("KeyList file not found!n");
perror("");
//exit(0);
}else
{
while(fgets(KeyLine,1024,fp))
{
//printf("%s",KeyLine);
KeyLine[strlen(KeyLine)-1]='';
strcpy(KeyLineTemp,KeyLine);
KeyLineStr=KeyLineTemp;
ID=atol(strsep(&KeyLineStr,KeyLinePan));
strcpy(KeyStr,strsep(&KeyLineStr,KeyLinePan));
Keysearch(Keyroot,KeyStr,ID,0);
}
}
//Key_display_shm(Keyroot);
shmdt(Keyroot);
}else
{
printf(" KEY already inputn");
}
}
int Keysearch(Keylink ptr,char KeyLine[1024],long ID,int i)
{
int found=0;
Keylink fptr;
Keylink Keyinsert();
if(ptr==NULL)
{
//ptr=root=(llink)malloc(sizeof(node));
ptr=Keyroot=keyptr_temp=(Keylink)shmat(shmid,NULL,SHM_RND);
ptr->rptr=NULL;
ptr->lptr=NULL;
//printf("#%i#n",sizeof(node));exit(0);
sprintf((ptr->Key),"%s",KeyLine);
ptr->ID=ID;
}else
{
while((found!=1) && (ptr!=NULL))
{
if(!strcmp(ptr->Key,KeyLine))
{
//printf("found %sn",LineInfo->Cookie);
//tupdate(ptr,LineInfo,URLID);
found=1;
return 0;
}else
{
if(strcmp(ptr->Key,KeyLine)>0)
{
fptr=ptr;
ptr=ptr->lptr;
}else
{
fptr=ptr;
ptr=ptr->rptr;
}
}
}
if(found==0)
{
Keyinsert(fptr,KeyLine,ID,i);
}
}
return 1;
}
Keylink Keyinsert(Keylink fptr,char KeyLine[1024],long ID,int i)
{
Keylink nptr;
nptr=(keyptr_temp+1);
keyptr_temp=nptr;
nptr->rptr=NULL;
nptr->lptr=NULL;
sprintf((nptr->Key),"%s",KeyLine);
nptr->ID=ID;
if(strcmp(fptr->Key,KeyLine)>0)
{
fptr->lptr=nptr;
}else
{
fptr->rptr=nptr;
}
return nptr;
}
Key_display_shm(Keylink ptr)
{
if(ptr!=NULL)
{
printf("%ld`%sn",ptr->ID,ptr->Key);
Key_display_shm(ptr->lptr);
Key_display_shm(ptr->rptr);
}
}
Key_count_display_shm(Keylink ptr)
{
if(ptr!=NULL)
{
//printf("%ld`%sn",ptr->ID,ptr->Key);
read_count++;
Key_count_display_shm(ptr->lptr);
Key_count_display_shm(ptr->rptr);
}
}
// 关键字结束
|
建议你用: cat /proc/sys/kernel/shmmax 看一下共享内存的大小,然在在程序里计算一下节点数量和节点总容量,是不是超出共享内存大小了。
|
注意数据的大小不能超过共巷内存的大小,共享内存大小是有上限的,所一超大文本可能会大于分配到的共享内存的大小
|
kernel 2.4 缺省最大shm 32M, 不知道你sizeof(Keylink)是多大,如果写多了会crash. 你的程序也没有检查。