關(guān)于errno后事妥善處理

      網(wǎng)友投稿 1224 2022-05-30

      前言

      當(dāng)UNIX函數(shù)出現(xiàn)錯(cuò)誤時(shí),常常返回一個(gè)負(fù)值,而且整型變量errno通常被設(shè)置為含有附加信息的一個(gè)值。例如,open函數(shù)如果成功執(zhí)行則返回一個(gè)非負(fù)文件描述符,如出錯(cuò)則返回-1.在open出錯(cuò)時(shí),有大約15種不同的errno值(文件不存在、權(quán)限問題等)。某些函數(shù)并不返回負(fù)值而是使用另一種約定。例如,返回一個(gè)指向?qū)ο笾羔樀拇蠖鄶?shù)函數(shù),在出錯(cuò)時(shí),將返回一個(gè)NULL指針。

      文件====中定義了符號errno以及可以賦予的各個(gè)常量,這些常量都以字符E開頭。例如,若errno等于常量EACESS,這表示產(chǎn)生了權(quán)限問題(例如,沒有打開所要求文件的足夠權(quán)限)。

      POSIX和ISO C將errno定義為這一一個(gè)符號,它擴(kuò)展成為一個(gè)可以修改的整型左值。這可以是包含出錯(cuò)編號的一個(gè)整數(shù),或是一個(gè)返回出錯(cuò)編號指針的函數(shù)。以前使用的定義是:

      extern int errno;

      但是在支持線程的環(huán)境中,多個(gè)線程共享進(jìn)程地址空間,

      每個(gè)線程都有屬于自己的局部errno以避免一個(gè)線程干擾另一個(gè)線程。

      對于errno應(yīng)當(dāng)知道兩條規(guī)則。第一條規(guī)則:如果沒有出錯(cuò),則其值不會(huì)被一個(gè)例程清除。因此,僅當(dāng)函數(shù)返回指明出錯(cuò)時(shí),才檢驗(yàn)其值。第二條:任何一個(gè)函數(shù)都不會(huì)將errno值設(shè)置為0,在中定義的所有常量都不為0。

      C標(biāo)準(zhǔn)定義了兩個(gè)函數(shù),它們幫助打印出錯(cuò)信息。

      #include char *strerror(int errnum);

      1

      2

      此函數(shù)將errnum(它通常就是errno值)映射到一個(gè)出錯(cuò)信息字符串,并且返回此字符串的指針。

      perror函數(shù)基于errno的當(dāng)前值,在標(biāo)準(zhǔn)錯(cuò)誤上產(chǎn)生一條出錯(cuò)信息,

      #include void perror(const char *msg);

      1

      2

      3

      它首先輸出由msg指向的字符串,然后是一個(gè)冒號,一個(gè)空格,接著是對應(yīng)于errno值的出錯(cuò)信息,最后是一個(gè)換行符。

      出錯(cuò)恢復(fù)

      可與將中定義的各種出錯(cuò)分出

      致命性的

      非致命性的

      兩類。對于致命性的錯(cuò)誤,無法執(zhí)行恢復(fù)動(dòng)作,最多只能在用戶屏幕上打印一條出錯(cuò)信息,或者將一條出錯(cuò)信息寫到日志文件中,然后終止。關(guān)于這點(diǎn)可以看我的:段錯(cuò)誤?打的就是段錯(cuò)誤!

      而對于非致命性錯(cuò)誤,有課可以較為妥善的處理。大多數(shù)非致命性錯(cuò)誤在本質(zhì)上是暫時(shí)的,例如資源短缺,當(dāng)系統(tǒng)中活動(dòng)較少時(shí),這種出錯(cuò)很可能就不會(huì)發(fā)生。

      與資源相關(guān)的非致命性出錯(cuò)包括EAGAIN. ENFILE、ENOBUFS、ENOLCK、ENOSPC、ENOSR、EWOULDBLOCK, 有時(shí)ENOMEM也是非致命性出錯(cuò)。當(dāng)EBUSY指明共享資源正在使用時(shí),也可將它作為非致命性出錯(cuò)處理。當(dāng)EINTR中斷- -慢速系統(tǒng)調(diào)用時(shí),可將它作為非致命性出錯(cuò)處理。

      對于資源相關(guān)的非致命性出錯(cuò),一般恢復(fù)動(dòng)作是延遲一些時(shí)間, 然后再試。這種技術(shù)可應(yīng)用于其他情況。例如,假設(shè)-個(gè) 出錯(cuò)表明一個(gè) 網(wǎng)絡(luò)連接不再起作用, 那么應(yīng)用程序可以在短時(shí)間延遲后重建該連接。某些應(yīng)用使用指數(shù)補(bǔ)償算法,在每次重復(fù)中等待更長時(shí)間。

      最后,取決于應(yīng)用程序的開發(fā)者,他可以決定那些出錯(cuò)是可恢復(fù)的。如若使用一種從錯(cuò)誤中恢復(fù)的合理策略,那么由于避免了應(yīng)用程序的異常終止,就能改善應(yīng)用程序的健壯性。

      栗子

      對于中斷的read、write系 統(tǒng)調(diào)用,POSIX.I的語 義在該標(biāo)準(zhǔn)的2001版有所改變。對于如何處理已read、write部分?jǐn)?shù)據(jù)量的相應(yīng)系統(tǒng)調(diào)用,早期版本允許實(shí)現(xiàn)進(jìn)行選擇。如若read系統(tǒng)調(diào)用已接收并傳送數(shù)據(jù)至應(yīng)用程序緩沖區(qū),但尚未接收到應(yīng)用程序請求的全部數(shù)據(jù),此時(shí)被中斷、操作系統(tǒng)可以認(rèn)為該系統(tǒng)調(diào)用失敗、并將errno設(shè)置為EINTR;另一種處理方式是允許該系統(tǒng)調(diào)用成功返回,返回已接收到的部分?jǐn)?shù)據(jù)量。與此類似,如若write巳傳輸了應(yīng)用程序緩沖區(qū)中的部分?jǐn)?shù)據(jù),然后被中斷,操作系統(tǒng)可以認(rèn)為該系統(tǒng)調(diào)用失敗,并將ermo設(shè)置為EINTR;另一種處理方式是允許該系統(tǒng)調(diào)用成功返回,返回已寫的部分?jǐn)?shù)據(jù)量。歷史上,從系統(tǒng)V派生的實(shí)現(xiàn),將這種系統(tǒng)調(diào)用視為失敗,而BSD派生的實(shí)現(xiàn)則處理為部分成功返回。POSIX.1 標(biāo)準(zhǔn)的2001版采用BSD風(fēng)格的語義。

      假定一個(gè)讀操作,它被中斷,我們要恢復(fù)它:

      again: if(n= read(fd, buf, BUFSIZE)) < 0) if (errno == EINTR) goto again;/* just an interrupted system call * / /* handle other errors */

      1

      2

      3

      4

      5

      6

      errno表

      其實(shí)看不看這個(gè)表意義不大,但是有的人他就是想看,那我就貼一份吧。

      查看系統(tǒng)中所有的errno所代表的含義,可以采用如下的代碼: /* Function: obtain the errno string * char *strerror(int errno) */ #include #include //for strerror() //#include int main() { int tmp = 0; for(tmp = 0; tmp <=256; tmp++) { printf("errno: %2d\t%s\n",tmp,strerror(tmp)); } return 0; } 輸出信息如下: 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 errno: 11 Resource temporarily unavailable errno: 12 Cannot allocate memory errno: 13 Permission denied errno: 14 Bad address errno: 15 Block device required errno: 16 Device or resource busy errno: 17 File exists errno: 18 Invalid cross-device link errno: 19 No such device errno: 20 Not a directory errno: 21 Is a directory errno: 22 Invalid argument errno: 23 Too many open files in system errno: 24 Too many open files errno: 25 Inappropriate ioctl for device errno: 26 Text file busy errno: 27 File too large errno: 28 No space left on device errno: 29 Illegal seek errno: 30 Read-only file system errno: 31 Too many links errno: 32 Broken pipe errno: 33 Numerical argument out of domain errno: 34 Numerical result out of range errno: 35 Resource deadlock avoided errno: 36 File name too long errno: 37 No locks available errno: 38 Function not implemented errno: 39 Directory not empty errno: 40 Too many levels of symbolic links errno: 41 Unknown error 41 errno: 42 No message of desired type errno: 43 Identifier removed errno: 44 Channel number out of range errno: 45 Level 2 not synchronized errno: 46 Level 3 halted errno: 47 Level 3 reset errno: 48 Link number out of range errno: 49 Protocol driver not attached errno: 50 No CSI structure available errno: 51 Level 2 halted errno: 52 Invalid exchange errno: 53 Invalid request descriptor errno: 54 Exchange full errno: 55 No anode errno: 56 Invalid request code errno: 57 Invalid slot errno: 58 Unknown error 58 errno: 59 Bad font file format errno: 60 Device not a stream errno: 61 No data available errno: 62 Timer expired errno: 63 Out of streams resources errno: 64 Machine is not on the network errno: 65 Package not installed errno: 66 Object is remote errno: 67 Link has been severed errno: 68 Advertise error errno: 69 Srmount error errno: 70 Communication error on send errno: 71 Protocol error errno: 72 Multihop attempted errno: 73 RFS specific error errno: 74 Bad message errno: 75 Value too large for defined data type errno: 76 Name not unique on network errno: 77 File descriptor in bad state errno: 78 Remote address changed errno: 79 Can not access a needed shared library errno: 80 Accessing a corrupted shared library errno: 81 .lib section in a.out corrupted errno: 82 Attempting to link in too many shared libraries errno: 83 Cannot exec a shared library directly errno: 84 Invalid or incomplete multibyte or wide character errno: 85 Interrupted system call should be restarted errno: 86 Streams pipe error errno: 87 Too many users errno: 88 Socket operation on non-socket errno: 89 Destination address required errno: 90 Message too long errno: 91 Protocol wrong type for socket errno: 92 Protocol not available errno: 93 Protocol not supported errno: 94 Socket type not supported errno: 95 Operation not supported errno: 96 Protocol family not supported errno: 97 Address family not supported by protocol errno: 98 Address already in use errno: 99 Cannot assign requested address errno: 100 Network is down errno: 101 Network is unreachable errno: 102 Network dropped connection on reset errno: 103 Software caused connection abort errno: 104 Connection reset by peer errno: 105 No buffer space available errno: 106 Transport endpoint is already connected errno: 107 Transport endpoint is not connected errno: 108 Cannot send after transport endpoint shutdown errno: 109 Too many references: cannot splice errno: 110 Connection timed out errno: 111 Connection refused errno: 112 Host is down errno: 113 No route to host errno: 114 Operation already in progress errno: 115 Operation now in progress errno: 116 Stale file handle errno: 117 Structure needs cleaning errno: 118 Not a XENIX named type file errno: 119 No XENIX semaphores available errno: 120 Is a named type file errno: 121 Remote I/O error errno: 122 Disk quota exceeded errno: 123 No medium found errno: 124 Wrong medium type errno: 125 Operation canceled errno: 126 Required key not available errno: 127 Key has expired errno: 128 Key has been revoked errno: 129 Key was rejected by service errno: 130 Owner died errno: 131 State not recoverable errno: 132 Operation not possible due to RF-kill errno: 133 Memory page has hardware error errno: 134~255 unknown error! Linux中,在頭文件 /usr/include/asm-generic/errno-base.h 對基礎(chǔ)常用errno進(jìn)行了宏定義: #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERIC_ERRNO_BASE_H #define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #define ESRCH 3 /* No such process */ #define EINTR 4 /* Interrupted system call */ #define EIO 5 /* I/O error */ #define ENXIO 6 /* No such device or address */ #define E2BIG 7 /* Argument list too long */ #define ENOEXEC 8 /* Exec format error */ #define EBADF 9 /* Bad file number */ #define ECHILD 10 /* No child processes */ #define EAGAIN 11 /* Try again */ #define ENOMEM 12 /* Out of memory */ #define EACCES 13 /* Permission denied */ #define EFAULT 14 /* Bad address */ #define ENOTBLK 15 /* Block device required */ #define EBUSY 16 /* Device or resource busy */ #define EEXIST 17 /* File exists */ #define EXDEV 18 /* Cross-device link */ #define ENODEV 19 /* No such device */ #define ENOTDIR 20 /* Not a directory */ #define EISDIR 21 /* Is a directory */ #define EINVAL 22 /* Invalid argument */ #define ENFILE 23 /* File table overflow */ #define EMFILE 24 /* Too many open files */ #define ENOTTY 25 /* Not a typewriter */ #define ETXTBSY 26 /* Text file busy */ #define EFBIG 27 /* File too large */ #define ENOSPC 28 /* No space left on device */ #define ESPIPE 29 /* Illegal seek */ #define EROFS 30 /* Read-only file system */ #define EMLINK 31 /* Too many links */ #define EPIPE 32 /* Broken pipe */ #define EDOM 33 /* Math argument out of domain of func */ #define ERANGE 34 /* Math result not representable */ #endif 在 /usr/include/asm-asm-generic/errno.h 中,對剩余的errno做了宏定義: #ifndef _ASM_GENERIC_ERRNO_H #define _ASM_GENERIC_ERRNO_H #include #define EDEADLK 35 /* Resource deadlock would occur */ #define ENAMETOOLONG 36 /* File name too long */ #define ENOLCK 37 /* No record locks available */ #define ENOSYS 38 /* Function not implemented */ #define ENOTEMPTY 39 /* Directory not empty */ #define ELOOP 40 /* Too many symbolic links encountered */ #define EWOULDBLOCK EAGAIN /* Operation would block */ #define ENOMSG 42 /* No message of desired type */ #define EIDRM 43 /* Identifier removed */ #define ECHRNG 44 /* Channel number out of range */ #define EL2NSYNC 45 /* Level 2 not synchronized */ #define EL3HLT 46 /* Level 3 halted */ #define EL3RST 47 /* Level 3 reset */ #define ELNRNG 48 /* Link number out of range */ #define EUNATCH 49 /* Protocol driver not attached */ #define ENOCSI 50 /* No CSI structure available */ #define EL2HLT 51 /* Level 2 halted */ #define EBADE 52 /* Invalid exchange */ #define EBADR 53 /* Invalid request descriptor */ #define EXFULL 54 /* Exchange full */ #define ENOANO 55 /* No anode */ #define EBADRQC 56 /* Invalid request code */ #define EBADSLT 57 /* Invalid slot */ #define EDEADLOCK EDEADLK #define EBFONT 59 /* Bad font file format */ #define ENOSTR 60 /* Device not a stream */ #define ENODATA 61 /* No data available */ #define ETIME 62 /* Timer expired */ #define ENOSR 63 /* Out of streams resources */ #define ENONET 64 /* Machine is not on the network */ #define ENOPKG 65 /* Package not installed */ #define EREMOTE 66 /* Object is remote */ #define ENOLINK 67 /* Link has been severed */ #define EADV 68 /* Advertise error */ #define ESRMNT 69 /* Srmount error */ #define ECOMM 70 /* Communication error on send */ #define EPROTO 71 /* Protocol error */ #define EMULTIHOP 72 /* Multihop attempted */ #define EDOTDOT 73 /* RFS specific error */ #define EBADMSG 74 /* Not a data message */ #define EOVERFLOW 75 /* Value too large for defined data type */ #define ENOTUNIQ 76 /* Name not unique on network */ #define EBADFD 77 /* File descriptor in bad state */ #define EREMCHG 78 /* Remote address changed */ #define ELIBACC 79 /* Can not access a needed shared library */ #define ELIBBAD 80 /* Accessing a corrupted shared library */ #define ELIBSCN 81 /* .lib section in a.out corrupted */ #define ELIBMAX 82 /* Attempting to link in too many shared libraries */ #define ELIBEXEC 83 /* Cannot exec a shared library directly */ #define EILSEQ 84 /* Illegal byte sequence */ #define ERESTART 85 /* Interrupted system call should be restarted */ #define ESTRPIPE 86 /* Streams pipe error */ #define EUSERS 87 /* Too many users */ #define ENOTSOCK 88 /* Socket operation on non-socket */ #define EDESTADDRREQ 89 /* Destination address required */ #define EMSGSIZE 90 /* Message too long */ #define EPROTOTYPE 91 /* Protocol wrong type for socket */ #define ENOPROTOOPT 92 /* Protocol not available */ #define EPROTONOSUPPORT 93 /* Protocol not supported */ #define ESOCKTNOSUPPORT 94 /* Socket type not supported */ #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ #define EPFNOSUPPORT 96 /* Protocol family not supported */ #define EAFNOSUPPORT 97 /* Address family not supported by protocol */ #define EADDRINUSE 98 /* Address already in use */ #define EADDRNOTAVAIL 99 /* Cannot assign requested address */ #define ENETDOWN 100 /* Network is down */ #define ENETUNREACH 101 /* Network is unreachable */ #define ENETRESET 102 /* Network dropped connection because of reset */ #define ECONNABORTED 103 /* Software caused connection abort */ #define ECONNRESET 104 /* Connection reset by peer */ #define ENOBUFS 105 /* No buffer space available */ #define EISCONN 106 /* Transport endpoint is already connected */ #define ENOTCONN 107 /* Transport endpoint is not connected */ #define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */ #define ETOOMANYREFS 109 /* Too many references: cannot splice */ #define ETIMEDOUT 110 /* Connection timed out */ #define ECONNREFUSED 111 /* Connection refused */ #define EHOSTDOWN 112 /* Host is down */ #define EHOSTUNREACH 113 /* No route to host */ #define EALREADY 114 /* Operation already in progress */ #define EINPROGRESS 115 /* Operation now in progress */ #define ESTALE 116 /* Stale file handle */ #define EUCLEAN 117 /* Structure needs cleaning */ #define ENOTNAM 118 /* Not a XENIX named type file */ #define ENAVAIL 119 /* No XENIX semaphores available */ #define EISNAM 120 /* Is a named type file */ #define EREMOTEIO 121 /* Remote I/O error */ #define EDQUOT 122 /* Quota exceeded */ #define ENOMEDIUM 123 /* No medium found */ #define EMEDIUMTYPE 124 /* Wrong medium type */ #define ECANCELED 125 /* Operation Canceled */ #define ENOKEY 126 /* Required key not available */ #define EKEYEXPIRED 127 /* Key has expired */ #define EKEYREVOKED 128 /* Key has been revoked */ #define EKEYREJECTED 129 /* Key was rejected by service */ /* for robust mutexes */ #define EOWNERDEAD 130 /* Owner died */ #define ENOTRECOVERABLE 131 /* State not recoverable */ #define ERFKILL 132 /* Operation not possible due to RF-kill */ #define EHWPOISON 133 /* Memory page has hardware error */ #endif

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      85

      86

      87

      88

      89

      90

      91

      92

      93

      94

      95

      96

      97

      98

      99

      100

      101

      102

      103

      104

      105

      106

      107

      108

      109

      110

      111

      112

      113

      114

      115

      116

      117

      118

      119

      120

      121

      122

      123

      124

      125

      126

      127

      128

      129

      130

      131

      132

      133

      134

      135

      136

      137

      138

      139

      140

      141

      142

      143

      144

      145

      146

      147

      148

      149

      150

      151

      152

      153

      154

      155

      156

      157

      158

      159

      160

      161

      162

      163

      164

      165

      166

      167

      168

      169

      170

      171

      關(guān)于errno的后事妥善處理

      172

      173

      174

      175

      176

      177

      178

      179

      180

      181

      182

      183

      184

      185

      186

      187

      188

      189

      190

      191

      192

      193

      194

      195

      196

      197

      198

      199

      200

      201

      202

      203

      204

      205

      206

      207

      208

      209

      210

      211

      212

      213

      214

      215

      216

      217

      218

      219

      220

      221

      222

      223

      224

      225

      226

      227

      228

      229

      230

      231

      232

      233

      234

      235

      236

      237

      238

      239

      240

      241

      242

      243

      244

      245

      246

      247

      248

      249

      250

      251

      252

      253

      254

      255

      256

      257

      258

      259

      260

      261

      262

      263

      264

      265

      266

      267

      268

      269

      270

      271

      272

      273

      274

      275

      276

      277

      278

      279

      280

      281

      282

      283

      284

      285

      286

      287

      288

      289

      290

      291

      292

      293

      294

      295

      296

      297

      298

      299

      300

      301

      302

      303

      304

      305

      306

      307

      308

      309

      310

      311

      312

      313

      314

      315

      任務(wù)調(diào)度

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:ArrayList 源碼分析
      下一篇:代碼重構(gòu)技術(shù)探究
      相關(guān)文章
      看亚洲a级一级毛片| 亚洲videos| 亚洲精品久久久久无码AV片软件| 亚洲乱码一二三四区国产| 亚洲黄色高清视频| 亚洲资源在线视频| 亚洲精品动漫在线| 亚洲成A∨人片在线观看无码| 亚洲黄色中文字幕| 亚洲国产精品成人久久久 | 亚洲喷奶水中文字幕电影| 亚洲最新黄色网址| 亚洲一级毛片免观看| 2020久久精品亚洲热综合一本| 亚洲人成77777在线观看网| 涩涩色中文综合亚洲| 亚洲国产精品无码久久九九大片 | 99久久国产亚洲综合精品| 亚洲日本VA中文字幕久久道具| 亚洲人成电影网站免费| 亚洲av日韩综合一区二区三区| 处破女第一次亚洲18分钟| 亚洲国产精品成人| 中文字幕亚洲一区| 亚洲Av永久无码精品三区在线 | 久久被窝电影亚洲爽爽爽| 亚洲AV日韩精品久久久久| 亚洲精品视频在线观看免费 | 精品国产人成亚洲区| 亚洲乱亚洲乱妇无码麻豆| 香蕉蕉亚亚洲aav综合| 亚洲黄色在线播放| 2020亚洲男人天堂精品| 亚洲精品国产摄像头| 亚洲精品国产自在久久 | 最新亚洲精品国偷自产在线 | 亚洲2022国产成人精品无码区| 亚洲福利视频网站| 亚洲色欲啪啪久久WWW综合网| 美国毛片亚洲社区在线观看| 国产中文在线亚洲精品官网|