ENOENT
表示没有找到文件或目录,EACCES
表示权限不足等。在Linux系统中,错误号(Error Codes)通常用于标识系统调用或程序执行过程中发生的错误类型,这些错误号有助于开发者和系统管理员快速定位问题所在,并采取相应的解决措施,本文将详细介绍一些常见的Linux错误号及其含义,并通过表格形式展示部分重要错误码。
常见Linux错误号及含义
错误号 | 名称 | 描述 |
1 | EPERM | Operation not permitted |
2 | ENOENT | No such file or directory |
3 | ESRCH | No such process |
4 | ENOMEM | Not enough space |
5 | EACCES | Permission denied |
6 | EFAULT | Bad address |
7 | EBUSY | Device or resource busy |
8 | EEXISTS | File exists |
9 | EXDEV | Improper link |
10 | ENOTDIR | Is a directory |
11 | EISDIR | Is a regular file |
12 | ENOMEM | Out of memory |
13 | EACCES | Permission denied |
14 | EFAULT | Bad address |
15 | EAGAIN | Try again |
16 | EINVAL | Invalid argument |
17 | ERANGE | Result too large |
18 | EINTR | Interrupted system call |
19 | EIO | I/O error |
20 | ENXIO | No such device or address |
21 | E2BIG | Argument list too long |
22 | ENOEXEC | Exec format error |
23 | EBADF | Bad file number |
24 | ECHILD | No child processes |
25 | EAGAIN | Try again |
26 | ENONET | Machine is not on the network |
27 | EADDRINUSE | Address already in use |
28 | EADDRNOTAVAIL | Can't assign requested address |
29 | ENETDOWN | Network is down |
30 | ENETUNREACH | Network is unreachable |
31 | ENETRESET | Network dropped connection on reset |
32 | ECONNABORTED | Software caused connection abort |
33 | ECONNRESET | Connection reset by peer |
34 | ENOBUFS | No buffer space available |
35 | EAFNOSUPPORT | Address family not supported |
36 | EPROTOTYPE | Protocol wrong type for socket |
37 | ENOTSOCK | Socket operation on non-socket |
38 | EOPNOTSUPP | Operation not supported |
39 | EPFNOSUPPORT | Protocol family not supported |
40 | ENOTSUP | Operation not supported |
41 | ENETRESET | Network dropped connection on reset |
42 | EISCONN | Socket is already connected |
43 | ENOTCONN | The socket is not connected |
44 | ESHUTDOWN | Can't send after socket shutdown |
45 | ETOOMANYREFS | Too many references: can't splice |
46 | ETIMEDOUT | Connection timed out |
47 | ECONNREFUSED | Connection refused |
48 | EHOSTDOWN | Host is down |
49 | EHOSTUNREACH | No route to host |
50 | EALREADY | Operation would block |
51 | EINPROGRESS | Operation now in progress |
52 | ESTALED | Stale NFS file handle |
53 | EUCLEAN | Structure needs cleaning |
54 | ENOTNA | Not a directory |
55 | ENOANO | Anomalous group file structure |
56 | EIOCBQUEUED | I/O control operation already queued |
57 | EBADSLT | Invalid slot |
58 | ENOSR | Out of streams resources |
59 | ELIBACC | Accessing a corrupted shared library |
60 | ELIBBAD | .lib section in a.out file has errors |
61 | ELIBSCN | .lib section in a.out corrupted |
62 | ELIBMAX | Attempting to link in too many libraries |
63 | ELIBEXEC | Unable to exec because an alternate version was linked |
64 | EILSEQ | Illegal byte sequence |
65 | ERESTART | Interrupted system call should be restarted |
66 | ESTRPIPE | Stream pipe error |
67 | EUSERSRC | Address not available |
68 | ENOTSOCK | Operation not permitted |
69 | EPROCUBLOCKED | No more processes |
70 | ENOSTR | Device not a stream |
71 | ENODATA | No data available (for no delay io) |
72 | ENOSR | Out of streams resources |
73 | ENONET | Machine is not on the network |
74 | ENOPKG | Error in package |
75 | EREMCHG | Address not available |
76 | EIDRM | ID removed |
77 | ENOMSG | No message of available |
78 | EOVERFLOW | Value too large for defined data type |
79 | ENOTUNIQ | Name not unique on the network |
80 | EBADFD | Faulty file descriptor |
81 | EREMCHG | Address not available |
82 | ELIBACC | Can't access shared library |
83 | EOWNERDEAD | Previous owner died |
84 | ELOOP | Too many symbolic links encountered, or loop |
85 | EOPNOTSUPP | Operation not supported |
... | ... | ... |
Linux错误号的分类与应用场景
h3. 文件相关错误
这类错误通常与文件操作有关,如打开、关闭、读写文件时出现的问题。
ENOENT: 表示没有找到指定的文件或目录。
EACCES: 表示权限不足,无法访问某个文件或目录。
EBUSY: 表示设备或资源正忙,无法立即完成请求的操作。
h3. 内存相关错误
这些错误涉及到内存分配失败或其他与内存相关的异常情况。
ENOMEM: 表示没有足够的内存空间来完成任务。
EFAULT: 表示访问了无效的地址空间。
EINVAL: 如果参数无效,则可能会返回此错误。
h3. 网络相关错误
网络通信中常见的错误包括连接超时、拒绝连接等。
ETIMEDOUT: 表示尝试建立连接时超时。
ECONNREFUSED: 表示远程主机拒绝了连接请求。
ENETUNREACH: 表示网络不可达。
h3. 进程控制相关错误
涉及进程创建、终止等方面的错误。
EPERM: 表示操作不被允许。
ESRCH: 表示未找到指定的进程。
ECHILD: 表示没有子进程。
Linux错误号处理的最佳实践
1、检查返回值:每次调用可能产生错误的函数后,都应检查其返回值是否为负数,以确定是否有错误发生。
2、使用errno
获取具体错误信息:当检测到错误时,可以通过全局变量errno
获取更详细的错误描述。
3、适当清理资源:即使遇到错误,也应确保释放已分配的资源,避免内存泄漏等问题。
4、记录日志:对于关键操作失败的情况,应该记录详细的日志信息,便于后续排查问题。
5、合理设计错误处理逻辑:根据不同的错误类型采取相应的恢复措施,比如重试操作、提示用户等。
FAQs
Q1:errno
是什么?
A:errno
是一个全局变量,用于存储最近一次系统调用失败时的错误代码,它可以帮助程序员了解具体的错误原因。
Q: 如果一个函数返回-1,我该如何判断具体的错误类型?
A: 你可以使用perror()
函数打印出详细的错误信息,或者直接查看errno
的值来确定具体的错误类型。
Q: 如何避免常见的文件操作错误?
A: 确保文件路径正确无误,检查文件权限设置是否合理,并在进行文件操作前验证文件是否存在,还应该考虑到并发访问的情况,使用适当的锁机制来防止竞态条件。
通过上述内容,相信你对Linux系统下的错误号有了更加清晰的认识,并能在实际工作中更好地应对各种可能出现的错误情况。
以上内容就是解答有关“linux错误号”的详细内容了,我相信这篇文章可以为您解决一些疑惑,有任何问题欢迎留言反馈,谢谢阅读。