前言 最近下載了 memcached 源碼,準備研究研究。 但是下載后發(fā)現(xiàn)里面有很多庫函數(shù)自己沒見過,于是把這些函數(shù)收集起來。 后來發(fā)現(xiàn)我知道的一些函數(shù)讀者可能還不知道,于是把不常見的函數(shù)都記錄下來吧。 這些函數(shù)都是看 man 的英文手冊學(xué)習(xí)的,所以可能講解
最近下載了 memcached 源碼,準備研究研究。
但是下載后發(fā)現(xiàn)里面有很多庫函數(shù)自己沒見過,于是把這些函數(shù)收集起來。
后來發(fā)現(xiàn)我知道的一些函數(shù)讀者可能還不知道,于是把不常見的函數(shù)都記錄下來吧。
這些函數(shù)都是看 man 的英文手冊學(xué)習(xí)的,所以可能講解的非常淺,如果想深入學(xué)習(xí),可以詢問我或者自行 google 查資料了解。
想看原理的直接轉(zhuǎn)向 memcached 源碼閱讀之原理篇
函數(shù)的含義
abort the program if assertion is false
判斷一個值是否是 false, 如果是false 就退出。
這個函數(shù)主要用于程序員做測試。
對于某個變量應(yīng)該為某個值的時候,為了確保那個變量確實在是那個值,可以用 assert 來擔保。
如果那個變量出現(xiàn)意外不是規(guī)定的值,程序?qū)娭仆顺觯?/script>輸出錯誤信息,格式如下
當程序正式使用時,就要關(guān)閉 assert 這個功能。
當然,我們不會去一個一個的注釋。
我們可以定義一個宏 NDEBUG, 定義之后 assert 就會無效的。
頭文件與聲明
#include void assert(scalar expression); //source assert(argc > 2); //error message a.out: timedrun.c:94: int main(int, char**): Assertion `argc > 2' failed. Aborted
函數(shù)的含義
set an alarm clock for delivery of a signal
設(shè)置一個定時發(fā)送信號的 alarm
這個函數(shù)主要用于那些需要信號量的程序中。
alarm 實際上就是一個超時限制。
alarm 只能設(shè)置一個,后面的會覆蓋前面的。
頭文件與聲明
#include unsigned int alarm(unsigned int seconds);
函數(shù)的含義
create a child process
創(chuàng)建一個子進程
創(chuàng)建一個子進程,這個子進程的內(nèi)存空間和父進程一樣。
但是這個子進程和父進程還是有一些區(qū)別的,這里不多介紹。
如果 fork 成功,子進程的 PID 會返回給父進程,而在子進程中返回的是0.
如果返回 -1, 代表子進程創(chuàng)建失敗。
頭文件與聲明
#include pid_t fork(void);
函數(shù)的含義
print a system error message
向標準錯誤 輸出一條信息。
可以簡單的理解為輸出一條信息。
頭文件與聲明
#includevoid perror(const char *s); #include const char *sys_errlist[]; int sys_nerr; int errno;
函數(shù)的含義
execute a file
執(zhí)行一個文件
就是調(diào)用另一個可執(zhí)行程序。
頭文件與聲明
#include extern char **environ; int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg, ..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]);
函數(shù)的含義
examine and change a signal action
檢查和修改信號 action
頭文件與聲明
#includeint sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
函數(shù)的含義
wait for process to change state
等待進程去修改狀態(tài)
簡單的說就是等待子進程的信號。
成功了就返回修改修改狀態(tài)的那個子進程的 PID.
如果狀態(tài)沒有修改,返回0,錯誤返回 -1.
WIFEXITED(status) 這個宏用來指出子進程是否為正常退出的,如果是,它會返回一個非零值。
WEXITSTATUS(status) 當WIFEXITED返回非零值時,我們可以用這個宏來提取子進程的返回值.
WIFSIGNALED(status)
WTERMSIG(status) 當 WIFSIGNALED 返回非零值時,這個宏會導(dǎo)致子進程結(jié)束的信號數(shù)
頭文件與聲明
#include#include pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
函數(shù)的含義
ANSI C signal handling
ANSI C 信號
設(shè)置一個信號的回調(diào)函數(shù)。
當接受到指定信號時,執(zhí)行 handler 函數(shù)。
頭文件與聲明
#includetypedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);
函數(shù)的含義
send signal to a process
給一個進程發(fā)送信號
頭文件與聲明
#include#include int kill(pid_t pid, int sig);
函數(shù)的含義
get/set resource limits
得到最大的資源限制
頭文件與聲明
#include#include int getrlimit(int resource, struct rlimit *rlim); int setrlimit(int resource, const struct rlimit *rlim);
函數(shù)的含義
get user identity
得到用戶的身份ID
頭文件與聲明
#include #includeuid_t getuid(void); uid_t geteuid(void);
函數(shù)的含義
get password file entry 得到密碼文件實體
頭文件與聲明
#include#include struct passwd *getpwnam(const char *name); struct passwd *getpwuid(uid_t uid); int getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result); int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
函數(shù)的含義
set group identity 設(shè)置用戶組的ID
頭文件與聲明
#include#include int setgid(gid_t gid);
函數(shù)的含義
set user identity 設(shè)置用戶的ID
頭文件與聲明
#include#include int setuid(uid_t uid);
函數(shù)的含義
POSIX signal set operations.
頭文件與聲明
#includeint sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signum); int sigdelset(sigset_t *set, int signum); int sigismember(const sigset_t *set, int signum);
函數(shù)的含義
creates a session and sets the process group ID
頭文件與聲明
#include pid_t setsid(void);
函數(shù)的含義
duplicate a file descriptor
頭文件與聲明
#include int dup(int oldfd); int dup2(int oldfd, int newfd); #define _GNU_SOURCE #include int dup3(int oldfd, int newfd, int flags);
函數(shù)的含義
lock and unlock memory
頭文件與聲明
#includeint mlock(const void *addr, size_t len); int munlock(const void *addr, size_t len); int mlockall(int flags); int munlockall(void);
函數(shù)的含義
get an environment variable
頭文件與聲明
#includechar *getenv(const char *name);
原文地址:memcached 源碼閱讀之庫函數(shù)介紹, 感謝原作者分享。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com