2015-02-09 90 views
0

我有以下基本上實現了DNS服務器的程序。共享內存用於實現服務器緩存。我正試圖通過從文件中讀取它們來將一些初始條目填充到緩存中。但是,當文件條目超出某一點時,我遇到了分段錯誤。我無法自行調試該程序。符號查找錯誤:未定義符號:fclose

typedef struct rs{ 
char domainName[256]; 
char ip[36]; 
time_t timeStamp; 
struct rs *cacheEnd; 
} ResourceRecord; 
ResourceRecord *cache, *tempCachePtr, *cacheEnd; 
void create_Shared_Memory()   //Creates shared memory 
{ 
    int stateMemory; 
    stateMemory=shmget(IPC_PRIVATE, (MAX_CACHE_SIZE)*sizeof(ResourceRecord *),IPC_CREAT|0660); 
    if (stateMemory == -1) 
    { 
      perror("Shared memory creation"); 
       exit(EXIT_FAILURE); 
     } 
    cache=(ResourceRecord *)shmat(stateMemory,NULL,0); 
    tempCachePtr = cache; 
    if(shmctl(stateMemory,IPC_RMID,NULL)==-1) 
    printf("ERROR CREATING CARMODEMEMORY!!!"); 

    if(cache==NULL) 
    { 
     perror("Shared memory attach "); 
     shmctl(stateMemory,IPC_RMID,NULL); 
     exit(EXIT_FAILURE); 

    } 
} 

void populateCache(){ 
    tempCachePtr = cache+1; 
    struct hostent* domainAddress; 
    int i; 
    ResourceRecord record; 
    char * line = NULL; 
    size_t fileInputLength = 0; 
    ssize_t read; 
    puts("Populating cache"); 
    FILE *fp = fopen("hosts.txt", "r"); 
    for(i=1;i<=40; i++){ 
     if((read = getline(&line, &fileInputLength, fp)) == -1){ 
     break; 
     } 
     printf("%d From file %s\n",i, line); 
     strcpy(record.domainName, strtok(line, " ")); 
     strcpy(record.ip, strtok(NULL, "\n")); 
     time(&record.timeStamp); 
     *tempCachePtr = record; 
     tempCachePtr++; 
    } 
    cacheEnd = tempCachePtr-1; 
    *(cache->cacheEnd) = cacheEnd; 
    fclose(fp); 
} 

我試圖用gdb調試的代碼,這裏是我

symbol lookup error: /home/path/dnsserver: undefined symbol: fclose, version GLIBC_2.2.5 [Inferior 1 (process 7178) exited with code 0177]

而且當我試圖更改文件條目的數量,並再次調試:

SIGSEGV from /lib64/ld-linux-x86-64.so.2 shared memory

+0

我不假設一旦seg-fault命中你設法回溯? – WhozCraig 2015-02-09 10:15:00

+0

@WhozCraig:不是。他們來自單獨的調試。 – Unni 2015-02-09 10:16:53

+1

請原諒我,但是您是否嘗試使用'-g'開關進行編譯? – 2015-02-09 10:17:44

回答

0

正如AntoJurković指出的那樣,錯誤在於我使用50 * sizeof(ResourceRecord *)創建共享內存,其中sizeof(ResourceRecord *)僅僅是幾個字節,因爲它是一個指針,但sizeof(ResourceRecord)是更大(我的系統中8和312)。所以50 * sizeof(ResourceRecord *)只能分配400字節的內存,這對於40個ResourceRecord類型記錄來說顯然是不夠的。

相關問題