APUE unix出錯處理

2021-10-06 03:34:56 字數 1909 閱讀 6634

當unix函式出錯時(系統呼叫),常常會返回乙個負值,而且整型變數errno通常被設定為含有附加資訊的乙個值。

檔案中定義了符合errno以及可以賦予它的各種常量,這些常量都以字元e開頭。另外,unix系統手冊第2部分的第1頁intro(2)列出了所有這些出錯常量。在linux中,出錯常量在errno(3)手冊頁中列出(可以使用命令man 3 errno檢視)。

errno被重新定義為執行緒私有資料。這樣,乙個執行緒做了設定errno的操作並不會影響程序中其他執行緒的errno的值。

如果沒有出錯,則其值不會被乙個例程清除。因此,僅當函式的返回值指明出錯時,才檢驗其值。

任一函式都不會將errno值設定為0,在中定義的所有常量都不為0。

extern

int errno;

將errno設定為執行緒區域性變數是個不錯的主意,事實上,gcc中就是這麼幹的。他保證了執行緒之間的錯誤原因不會互相串改,當你在乙個執行緒中序列執行一系列過程,那麼得到的errno仍然是正確的。

多執行緒的時候,為了保證執行緒安全,在單個執行緒中,errno通過指標獲取當前執行緒中的錯誤資訊。

extern

int*

__errno_location

(void);

#define errno (*__errno_locatioin())

輸入為errno的整形值,為errno或者是具體的列舉的key

char

*strerror

(int errnum)

#include

#include

#include

int main (

)return(0

);}

error: no such file or directory
c 庫函式 void perror(const char *str) 把乙個描述性錯誤訊息輸出到標準錯誤 stderr。首先輸出字串 str,後跟乙個冒號,然後是乙個空格。

void

perror

(const

char

*str)

#include

#include

"apue.h"

#include

intmain()

error:

: no such file or directory

the error is : no such file or directory

errno:

0 success

errno:

1 operation not permitted

errno:

2 no such file or directory

errno:

3 no such process

errno:

4 interrupted system call

errno:

5 input/output error

errno:

6 no such device or address

errno:

7 argument list too long

errno:

8 exec format error

errno:

9 bad file descriptor

errno:

10 no child processes

等133個

出錯處理函式

我們知道,系統函式呼叫不能保證每次都成功,必須進行出錯處理,這樣一方面可以保證程式邏輯正常,另一方面可以迅速得到故障資訊。出錯處理函式 include include char strerror int errnum see notes errnum 傳入引數,錯誤編號的值,一般取 errno 的值...

linux 出錯處理

當linux 系統出錯時候,常返回乙個負值記錄在變數errno中。對於errno的使用用兩條規則 1 如果沒有出錯,其值不會被清除 2 任一函式不會將errno置0 c標準有2個函式列印出錯日誌 include char strerror int errnum 此函式講errno 對映成乙個字串,返...

001 出錯處理

函式strerror 1.1 函式原型 char strerror int errnum 分析 此函式將errnum 它通常就說errno值 對映為乙個出錯資訊字串,並返回錯誤此字串 1.2 清單 include include include int main return 0 編譯與執行 2.1...