errno 介绍

 

errno简介

Linux中系统调用的错误都存储于errno中,errno由操作系统维护,存储就近发生的错误,即下一次的错误码会覆盖掉上一次的错误。

errno是一个包含在<errno.h>中的预定义的外部int变量,用于表示最近一个函数调用是否产生了错误。若为0,则无错误,其它值均表示一类错误。

Linux中系统调用的错误都存储于 errno中,errno由操作系统维护,存储就近发生的错误,即下一次的错误码会覆盖掉上一次的错误。

编程时需要包含#include <errno.h>,可以直接查看errno的值例如:

#include <errno.h>

当一个系统调用或着库函数的调用失败时,将会重置错误代码errno。用户在判断程序出错后,立即检验errno的值可以获取错误代码和错误信息。

errno是线程安全的吗

是的,它是线程安全的。在Linux上,全局errno变量是特定于线程的。POSIX要求errno必须是线程安全的。

参见http://www.unix.org/whitepapers/reentrant.html

在POSIX.1中,errno被定义为外部全局变量。但是此定义在多线程环境中是不可接受的,因为使用它会导致不确定的结果。问题是两个或多个线程可能会遇到错误,所有错误都会导致设置相同的错误号。在这种情况下,一个线程可能已经被另一个线程更新后,最终检查errno。

为了避免产生不确定性,POSIX.1c将errno重新定义为可以访问每个线程错误号的服务,如下所示(ISO / IEC 9945:1-1996,§2.4):

某些函数可能在通过符号errno访问的变量中提供错误号。errno符号是通过包括C标准所指定的标头来定义的。对于进程的每个线程,errno的值不应受函数调用或其他线程对errno的分配的影响。

另请参阅http://linux.die.net/man/3/errno

errno是线程本地的;在一个线程中设置它不会影响在其他任何线程中的值

我们可以通过在机器上运行一个简单的程序来进行检查。

#include <stdio.h>                                                                                                                                             
#include <pthread.h>                                                                                                                                           
#include <errno.h>                                                                                                                                             
#define NTHREADS 5                                                                                                                                             
void *thread_function(void *);                                                                                                                                 

int                                                                                                                                                            
main()                                                                                                                                                         
{                                                                                                                                                              
   pthread_t thread_id[NTHREADS];                                                                                                                              
   int i, j;                                                                                                                                                   

   for(i=0; i < NTHREADS; i++)                                                                                                                                 
   {
      pthread_create( &thread_id[i], NULL, thread_function, NULL );                                                                                            
   }                                                                                                                                                           

   for(j=0; j < NTHREADS; j++)                                                                                                                                 
   {                                                                                                                                                           
      pthread_join( thread_id[j], NULL);                                                                                                                       
   }                                                                                                                                                           
   return 0;                                                                                                                                                   
}                                                                                                                                                              

void *thread_function(void *dummyPtr)                                                                                                                          
{                                                                                                                                                              
   printf("Thread number %ld addr(errno):%p\n", pthread_self(), &errno);                                                                                       
}

output:

Thread number 140672336922368 addr(errno):0x7ff0d4ac0698                                                                                                       
Thread number 140672345315072 addr(errno):0x7ff0d52c1698                                                                                                       
Thread number 140672328529664 addr(errno):0x7ff0d42bf698                                                                                                       
Thread number 140672320136960 addr(errno):0x7ff0d3abe698                                                                                                       
Thread number 140672311744256 addr(errno):0x7ff0d32bd698 

含义:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
 
int main(void){
 
    for(int i = 0; i < 140; i++){
        printf("%4d:%s\n", i, strerror(i));
    }
 
    exit(0);
}

output:

0:Success
   1:Operation not permitted
   2:No such file or directory
   3:No such process
   4:Interrupted system call
   5:Input/output error
   6:No such device or address
   7:Argument list too long
   8:Exec format error
   9:Bad file descriptor
  10:No child processes
  11:Resource temporarily unavailable
  12:Cannot allocate memory
  13:Permission denied
  14:Bad address
  15:Block device required
  16:Device or resource busy
  17:File exists
  18:Invalid cross-device link
  19:No such device
  20:Not a directory
  21:Is a directory
  22:Invalid argument
  23:Too many open files in system
  24:Too many open files
  25:Inappropriate ioctl for device
  26:Text file busy
  27:File too large
  28:No space left on device
  29:Illegal seek
  30:Read-only file system
  31:Too many links
  32:Broken pipe
  33:Numerical argument out of domain
  34:Numerical result out of range
  35:Resource deadlock avoided
  36:File name too long
  37:No locks available
  38:Function not implemented
  39:Directory not empty
  40:Too many levels of symbolic links
  41:Unknown error 41
  42:No message of desired type
  43:Identifier removed
  44:Channel number out of range
  45:Level 2 not synchronized
  46:Level 3 halted
  47:Level 3 reset
  48:Link number out of range
  49:Protocol driver not attached
  50:No CSI structure available
  51:Level 2 halted
  52:Invalid exchange
  53:Invalid request descriptor
  54:Exchange full
  55:No anode
  56:Invalid request code
  57:Invalid slot
  58:Unknown error 58
  59:Bad font file format
  60:Device not a stream
  61:No data available
  62:Timer expired
  63:Out of streams resources
  64:Machine is not on the network
  65:Package not installed
  66:Object is remote
  67:Link has been severed
  68:Advertise error
  69:Srmount error
  70:Communication error on send
  71:Protocol error
  72:Multihop attempted
  73:RFS specific error
  74:Bad message
  75:Value too large for defined data type
  76:Name not unique on network
  77:File descriptor in bad state
  78:Remote address changed
  79:Can not access a needed shared library
  80:Accessing a corrupted shared library
  81:.lib section in a.out corrupted
  82:Attempting to link in too many shared libraries
  83:Cannot exec a shared library directly
  84:Invalid or incomplete multibyte or wide character
  85:Interrupted system call should be restarted
  86:Streams pipe error
  87:Too many users
  88:Socket operation on non-socket
  89:Destination address required
  90:Message too long
  91:Protocol wrong type for socket
  92:Protocol not available
  93:Protocol not supported
  94:Socket type not supported
  95:Operation not supported
  96:Protocol family not supported
  97:Address family not supported by protocol
  98:Address already in use
  99:Cannot assign requested address
 100:Network is down
 101:Network is unreachable
 102:Network dropped connection on reset
 103:Software caused connection abort
 104:Connection reset by peer
 105:No buffer space available
 106:Transport endpoint is already connected
 107:Transport endpoint is not connected
 108:Cannot send after transport endpoint shutdown
 109:Too many references: cannot splice
 110:Connection timed out
 111:Connection refused
 112:Host is down
 113:No route to host
 114:Operation already in progress
 115:Operation now in progress
 116:Stale NFS file handle
 117:Structure needs cleaning
 118:Not a XENIX named type file
 119:No XENIX semaphores available
 120:Is a named type file
 121:Remote I/O error
 122:Disk quota exceeded
 123:No medium found
 124:Wrong medium type
 125:Operation canceled
 126:Required key not available
 127:Key has expired
 128:Key has been revoked
 129:Key was rejected by service
 130:Owner died
 131:State not recoverable
 132:Operation not possible due to RF-kill
 133:Unknown error 133
 134:Unknown error 134
 135:Unknown error 135
 136:Unknown error 136
 137:Unknown error 137
 138:Unknown error 138
 139:Unknown error 139

猜你喜欢

转载自blog.csdn.net/shanandqiu/article/details/111992367