当linux中的C api函数发生异常时,一般会将errno变量(需includeerrno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因,在实际编程中用这一招解决了不少原本看来莫名其妙的问题。但是errno是一个数字,代表的具体含义还要到errno.h中去阅读宏定义,而每次查阅是一件很繁琐的事情。有下面几种方法可以方便的得到错误信息
  

(1)void perror(const char *s)
函数说明
perror ( )用来将上一个函数发生错误的原因输出到标准错误(stderr),参数s 所指的字符串会先打印出,后面再加上错误原因 字符串。此错误原因依照全局变量 errno 的值来决定要输出的字符串。
  

(2) char *strerror(int errno)
将错误代码转换为字符串错误信息,可以将该字符串和其它的信息组合输出到用户界面例如
fprintf(stderr,"error in CreateProcess %s,Process ID %d ",strerror(errno),processID)
注:假设processID是一个已经获取了的整形ID
 

 (3)printf("%m", errno);
另外不是所有的地方发生错误的时候都可以通过error获取错误代码


errno.h中定义的错误代码值如下:
linux2.4.20-18的内核代码中的/usr/include/asm/errno.h

#ifndef _I386_ERRNO_H
#define _I386_ERRNO_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 /* Arg 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 */
#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 NFS 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 */
#endif

补充说明:

同时也可以使用strerror()来自己翻译

下面给出两个例子:
例一:

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
int   fd;
extern int errno;
if((fd = open("/dev/dsp",O_WRONLY)) < 0)
{
printf("errno=%d\n",errno);
char * mesg = strerror(errno);
printf("Mesg:%s\n",mesg);
}exit(0);
}

dsp设备忙的话将输出如下:
errno=16
Mesg:Device or resource busy

例二:

#include <string.h>
#include <errno.h>  /* for strerror */
#include <stdio.h>int main(int argc, char ** argv)
{int i = 0;for(i = 0; i < 256; i++)printf("errno.%02d is: %s/n", i, strerror(i));return 0;
}

Linux C编程--进程介绍4--errno相关推荐

  1. Linux C编程--进程介绍1--进程的创建

    这篇文章介绍的内容包括 1.Linux进程--进程标识号 2.进程控制--进程创建 1.Linux进程--进程标识号 进程(Process)是一个程序在其自身的虚拟地址空间中的一次执行活动.多个程序并 ...

  2. Linux C编程--进程介绍3--进程终止和等待

    进程结束 1.在Linux中任何让一个进程结束 进程退出表示进程即将结束.在Linux中进程退出分为了正常退出和异常退出两种. 1>正常退出 a. 在main()函数中执行return . b. ...

  3. Linux C编程--进程介绍6--进程的各种标识

    本文将介绍进程的: 1.进程的用户标识号 2.进程标识号 进程的用户ID     Linux/Unix进程涉及到三类用户ID:       1.实际用户ID(real user id,RUID):为该 ...

  4. Linux C编程--进程介绍7--综合应用实例

    第一个实例重点说明fork和exec系统函数 该实例是一个交互式命令处理程序,它能完成Linux系统标准Shell的小部分功能,具体功能如下所述: 1.提交命令的参数最多为8个 2.可前,后台执行 3 ...

  5. Linux C编程--进程介绍5--system函数

    表头文件 #i nclude<stdlib.h> 定义函数 int system(const char * string); 这个函数是用fork,exec,waitpid这三个系统函数实 ...

  6. Linux C编程--进程介绍2--exec函数

    exec函数族 fork()函数是用于创建一个子进程,该子进程几乎拷贝了父进程的全部内容,但是,这个新创建的进程如何执行呢?这个exec函数族就提供了一个在进程中启动另一个程序执行的方法.  exec ...

  7. Linux 系统编程 -进程概念篇

    Linux系统编程-进程篇 冯诺依曼体系结构 冯诺依曼的两个重要思想 当代计算机的三级缓存 操作系统 操作系统的概念 操作系统的组成 操作系统作用 Linux下的操作系统体系 进程 进程概念 进程特性 ...

  8. Linux系统编程——进程基础知识

    Linux系统编程--进程基础知识 1.程序和进程 程序,是指编译好的二进制文件,在磁盘上,不占用系统资源(cpu.内存.打开的文件.设备.锁-) 进程,是一个抽象的概念,与操作系统原理联系紧密.进程 ...

  9. Linux系统编程 进程控制

    文章目录 01. 学习目标 02. 进程和程序 (理解) 03. 单道.多道程序设计(了解) 3.1 单道程序设计 3.2 多道程序设计 04. 并行和并发(理解) 05. MMU(了解) 06. 进 ...

最新文章

  1. python自动测试p-Python自动化测试
  2. HTML、JSP、Servlet中的相对路径和绝对路径 页面跳转问题
  3. 计算机控制基础知识,最新 分析计算机控制系统及其运算基础知识-精品
  4. 清除nginx服务器网站缓存数据
  5. Rtworld目录网全解开源2.0-功能齐全
  6. c++对象回收问题_从垃圾回收解开Golang内存管理的面纱之三垃圾回收
  7. 一台比亚迪秦EV充电时自燃 相关人员:无人员伤亡、起火未爆炸
  8. wordpress 调用css,WordPress调用CSS最常用的方法有哪些?
  9. java 抽象类与接口的区别及其在jdk中的应用
  10. 【转贴CSDN】关于DataGrid的精华
  11. css定位中position:absolute与float的区别
  12. JAVA项目之图书管理系统
  13. Win7以上系统通过Dbgview打印驱动日志
  14. java 获取中文拼音首字母(缩写) 含pinyin4j maven包
  15. svg html g标签id,svg是什么?svg常用的方法(附代码)
  16. 上不了网,代理服务器出现问题
  17. java swing实训项目(图书管理系统)
  18. 纯干货!Java后端开发十二条经验分享!
  19. 深入理解流,什么是流?
  20. 网络防御 --- 恶意软件与反病毒详解

热门文章

  1. 构建局域网的病毒防护体系
  2. hbase 数据的导入导出
  3. C语言自学《八》---- C语言知识总结
  4. Asp.Net获取客户端信息-学习中
  5. hadoop生态搭建(3节点)
  6. R语言学习笔记:向量
  7. git中找回丢失的对象
  8. lintcode: 爬楼梯
  9. 迷茫与飞跃:9月开始,明确了研究方向,功力提升明显,成绩比较显著
  10. C++ operator两种用法【转】