linux系統呼叫函式

2021-06-01 08:46:19 字數 2753 閱讀 6647

acct: 禁止/啟用系統記錄程序資訊。若file為null,記錄功能將被禁用;

若file指向乙個存在的檔案(以'\0'結尾),則記錄功能被啟用,

正常結束的程序都會在該檔案尾新增對應的資訊。異常結束是指重啟或其它致命的系統問題。

acct()不能生成那些永遠不會結束的程序的資訊。

關於acct()使用的記錄結構及其它相關資訊,請參閱及acct(5)。

該系統呼叫只能由超級使用者使用

uselib: linux專用, 載入共享庫

sysinfo: 啟動到現在的時間,返回總的可用記憶體大小, 未用記憶體大小, 共享記憶體大小, 緩衝區大小, 交換區大小, 可用交換區大小, 當前程序數。

personality: linux spec, 執行域

yield: 讓出自己, 引起重新排程,

io_setup/io_submit/io_destroy/io_getevents:

直接非同步aio操作

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main()

;// 檢查寫磁碟情況,類似於epoll_wait或select

n =io_getevents(ctx, 1, 10, events, &timeout);

printf("io_getevents: %d:%s\n", n, strerror(-n));

close(fd);

io_destroy(ctx);

return 0;

}structiocb; /* 64 bytes */

structio_event;

系統呼叫功能原型

io_setup

為當前程序初始化乙個非同步io上下文

int io_setup(unsigned nr_events,aio_context_t *ctxp);

io_submit

提交乙個或者多個非同步io操作

int io_submit(aio_context_t ctx_id,long nr, struct iocb **iocbpp);

io_getevents

獲得未完成的非同步io操作的狀態

int io_getevents(aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout);

io_cancel

取消乙個未完成的非同步io操作

int io_cancel(aio_context_t ctx_id, struct iocb *iocb, struct io_event *result);

io_destroy

從當前程序刪除乙個非同步io上下文

int io_destroy(aio_context_t ctx);

posix_fadvise: 預定義file資料的操作行為,主要用於緩衝優化:

linux下讀寫檔案時,os會為檔案建立快取,用以提高速度。這部分用於快取的記憶體,在檔案關閉後,仍然不會被釋放(什麼時候會被釋放不得而知)。

但是在記憶體使用有限度或者檔案很大的情況下,有時需要馬上釋放快取或者禁止使用快取。

有以下兩種方法。

1,posix_fadvise函式

int posix_fadvise(intfd, off_toffset, off_tlen, intadvice);

posix_fadvise(fd, 0, 0, posix_fadv_dontneed);

len=0表示檔案全部

posix_fadv_dontneed表示告訴os,這個檔案在最近一段時間內不會被使用

試驗了一下,發現檔案開啟後讀寫前使用這個函式沒有用處

檔案讀寫之後關閉之前使用有用

2,/proc/sys/vm/drop_caches    linux 2.6.16 以後有效

釋放pagecache echo 1 > /proc/sys/vm/drop_caches

釋放dentry、inode    echo 2 > /proc/sys/vm/drop_caches

釋放pagecache dentry、inode  echo 3 > /proc/sys/vm/drop_caches

這個方法沒有試驗過

mbind:設定mem策略

add_key/request_key/keyctl: linux 核心金鑰服務:它的主要意圖是在 linux 核心中快取身份驗證資料。遠端檔案系統和其他核心服務可以使用這個服務來管理密碼學、身份驗證標記、跨域使用者對映和其他安全問題。它還使 linux 核心能夠快速訪問所需的金鑰,並可以用來將金鑰操作(比如新增、更新和刪除)委託給使用者空間。

具體還需要學習金鑰原理

Linux系統呼叫 getopt 函式

函式原型 include int getopt int argc,char const ar const char optstring 引數說明 前兩個引數是main 函式的兩個引數,第3個引數是選項字串。返回值為int型別,我們知道char型別是可以轉換成int型別的,每個字元都有其對應的整數值,...

Linux系統呼叫和庫函式呼叫

linux下對檔案操作有兩種方式 系統呼叫 system call 和庫函式呼叫 library functions 可以參考 linux程式設計 英文原版為 beginning linux programming 作者是neil matthew和richard stones 第三章 working...

Linux系統呼叫和庫函式呼叫

linux下對檔案操作有兩種方式 系統呼叫 system call 和庫函式呼叫 library functions 可以參考 linux程式設計 英文原版為 beginning linux programming 作者是neil matthew和richard stones 第三章 working...