如何从系统调用中提取错误?

时间:2013-02-01 03:20:02

标签: c unix system-calls

例如在手册页中它说:

The mmap() function shall fail if:

EACCES
The fildes argument is not open for read, regardless of the protection specified,        
fildes is not open for write and PROT_WRITE was specified for a MAP_SHARED type 
mapping. 

还有很多其他案件。我假设有一种方法来检查发生了哪个错误但是一小时的搜索没有产生任何结果。如何检查是否发生了特定错误?

2 个答案:

答案 0 :(得分:1)

该错误值将保存在全局变量errno中。您可以从perror(3)获得一个人类可读的字符串。

答案 1 :(得分:0)

如果您使用C ++

,也可以这样做
std::cerr << strerror(errno) << std::endl;

更多详细信息可在此处找到:

C++ alternative to perror()