2015-11-28 45 views
0

我一直在學習如何使用Unix函數在C中進行編程,以便我可以從頭開始編程信號量功能(無pthreads),但我目前被卡住了。手冊頁告訴我要包含特定的頭文件以使用感興趣的函數(例如malloc,tsleep,wakeup等),但是當我嘗試運行程序頭和方法調用時,我收到以下錯誤:'[OpenBSD 3.5系統定義的方法]的未定義引用

/tmp//ccg29960.o: In function `allocate_semaphore': 
/tmp//ccg29960.o(.text+0x28d): undefined reference to `simple_lock_init' 
/tmp//ccg29960.o: In function `down_semaphore': 
/tmp//ccg29960.o(.text+0x2fb): undefined reference to `tsleep' 
/tmp//ccg29960.o: In function `up_semaphore': 
/tmp//ccg29960.o(.text+0x3b5): undefined reference to `wakeup' 
/tmp//ccg29960.o: In function `free_semaphore': 
/tmp//ccg29960.o(.text+0x43b): undefined reference to `simple_lock' 
/tmp//ccg29960.o(.text+0x4af): undefined reference to `simple_unlock' 
collect2: ld returned 1 exit status 

相關的代碼是下面:

//#include <stdio.h> 
//#include <stdlib.h> 
#include <sys/errno.h> 
#include <sys/queue.h> 
//#include <sys/time.h> 
#include <sys/param.h> 
#include <sys/types.h> 
#include <sys/proc.h> 
#include <sys/malloc.h> 
#include <sys/lock.h> 


struct entry 
{ 
    pid_t id; 
    SIMPLEQ_ENTRY(entry) next; 
} *np; 

typedef struct 
{ 
    const char* name; 
    pid_t process; 
    pid_t p_process; //parent process 
    int count; 
    SIMPLEQ_HEAD(queuehead,entry) head; 
    struct simplelock *slock; 
} named_semaphore; 

named_semaphore* s_list[64]; 
int num_semaphores = 0; 

int main() 
{ 
    //lockinit(0, 0, 0,0, 0); 
    printf("Hello world\n"); 
    return 0; 
} 

//... irrelevant code elided 

int allocate_semaphore(const char* name, int initial_count) 
{ 
    int num_elements, i; 
    named_semaphore *new_s; 

    //perform initial checks before creating a new semaphore 

    //make sure the given name is an acceptable length 
    num_elements = sizeof(name)/sizeof(*name); 
    if (num_elements > 32) 
    { 
    return ENAMETOOLONG; 
    } 

    //make sure the given name is unique to this process 
    for (i = 0; i < num_semaphores; i++) 
    { 
    if (s_list[i]->process == getpid() && strcmp(s_list[i]->name, name)) 
     { 
    return EEXIST; 
     } 
    } 

    //make sure there are no more than 64 semaphores active 
    if (num_semaphores >= 64) 
    { 
    return ENOMEM; 
    } 

    //create a new semaphore and add it to the collection 

    new_s = (named_semaphore*) malloc(sizeof(named_semaphore), 0, 0); 
    new_s->name = name; 
    new_s->process = getpid(); 
    new_s->p_process = getppid(); 
    new_s->count = initial_count; 
    s_list[num_semaphores] = new_s; 
    ++num_semaphores; 

    //initialize the waiting queue 
    SIMPLEQ_INIT(&(new_s->head)); 

    //initialize its lock 
    simple_lock_init(new_s->slock); 

    //need to handle negative initial_count somehow 

    return (0); 
} 

int down_semaphore(const char* name) 
{ 
    named_semaphore* s; 

    s = getSemaphore(name); 
    if (s == NULL) 
    { 
     return (ENOENT); 
    } 
    s->count = (s->count) - 1; 
    if (s->count < 0) 
    { 
    //put process to sleep 
    tsleep(getpid(), getpriority(), 0, 0); 

    //add process to waiting queue 
    np = (struct entry *) malloc(sizeof(struct entry)); 
    np->id = getpid(); 
    SIMPLEQ_INSERT_TAIL(&(s->head), np, next); 
    } 

    return 0; 
} 

int up_semaphore (const char* name) 
{ 
    named_semaphore* s; 
    s = getSemaphore(name); 
    if (s == NULL) 
    { 
    return (ENOENT); 
    } 
    s->count = (s->count) + 1; 
    if (s->count <= 0) 
    { 
    //wakeup longest waiting process 
    wakeup((SIMPLEQ_FIRST(&(s->head)))->id); 

    //remove process from waiting queue 
    SIMPLEQ_REMOVE_HEAD(&(s->head), np, next); 
    free(np); 
    } 

    return 0; 

} 

int free_semaphore(const char* name) 
{ 
    named_semaphore* s; 
    s = getSemaphore(name); 
    if (s == NULL) 
    { 
    return (ENOENT); 
    } 
    simple_lock(s->slock); 
    while ((np = SIMPLEQ_FIRST(&(s->head))) != NULL) 
    { 
    //wakeup the process and return ECONNABORTED 
    //wakeup(getSemaphore(np->id)); 

    SIMPLEQ_REMOVE_HEAD(&(s->head), np, next); 
    free(np); 
    } 
    free(s); 
    simple_unlock(s->slock); 
} 

我沒有做修改/固定我的整體程序的邏輯(例如,鎖()荷蘭國際集團只發生在預期的三分之一方法),但理解爲什麼我會得到我當前的錯誤,以便我知道如何在將來修復類似的錯誤會很好。

對我來說,好像這些方法不能識別它們的頭文件,或者我錯過了一條必需的信息,以便兩者可以通信。

爲了解決這些錯誤,我嘗試重新安排並註釋掉列出的頭文件,並且還用大寫字母重新命名了方法調用,就像它們在頭文件文檔中提供的一樣。

任何幫助或洞察力表示讚賞,並提前感謝您!

回答

0

您閱讀的手冊頁...那些是第9章,不是嗎?第9節用於內核編程。除非你的代碼在內核中,否則你不能調用這些函數。