如何参考错误代码阅读 POSIX 的手册页?

How to read the manual page for POSIX refering to error codes?

我目前正在使用 pthread 并从这里阅读文档:Pthread Manual Pthread Join

但是,当我阅读页面时,我看到了错误,但没有看到相应的 return 值,这些值将从 pthread_join 得到 returned。所以我的问题是,错误是否按升序排列(因为它可能是一个枚举器)?

诸如EDEADLK之类的错误值是通过包含<errno.h>头文件定义的宏。

示例:

#include <pthread.h>
#include <errno.h>

...

int retval = pthread_join( threadID, NULL );
if ( retval == EDEADLK )
{
    // error-handling code for deadlock
}
else if ( retval == EINVAL )
{
    // error-handling code for invalid thread id
}
else if ( retval == ESRCH  )
{
    // error-handling code for no such thread id
}

请注意,以上代码仅对 Linux 正确,因为唯一的错误编号 specified by POSIX for pthread_join()EDEADLCK

根据 the POSIX standard for error numbers,(与您的问题特别相关的粗体部分):

2.3 Error Numbers

Most functions can provide an error number. The means by which each function provides its error numbers is specified in its description.

Some functions provide the error number in a variable accessed through the symbol errno, defined by including the <errno.h> header. The value of errno should only be examined when it is indicated to be valid by a function's return value. No function in this volume of POSIX.1-2008 shall set errno to zero. For each thread of a process, the value of errno shall not be affected by function calls or assignments to errno by other threads.

Some functions return an error number directly as the function value. These functions return a value of zero to indicate success.

If more than one error occurs in processing a function call, any one of the possible errors may be returned, as the order of detection is undefined.

Implementations may support additional errors not included in this list, may generate errors included in this list under circumstances other than those described here, or may contain extensions or limitations that prevent some errors from occurring.

The ERRORS section on each reference page specifies which error conditions shall be detected by all implementations ("shall fail") and which may be optionally detected by an implementation ("may fail"). If no error condition is detected, the action requested shall be successful. If an error condition is detected, the action requested may have been partially performed, unless otherwise stated.

Implementations may generate error numbers listed here under circumstances other than those described, if and only if all those error conditions can always be treated identically to the error conditions as described in this volume of POSIX.1-2008. Implementations shall not generate a different error number from one required by this volume of POSIX.1-2008 for an error condition described in this volume of POSIX.1-2008, but may generate additional errors unless explicitly disallowed for a particular function.

Each implementation shall document, in the conformance document, situations in which each of the optional conditions defined in POSIX.1-2008 is detected. The conformance document may also contain statements that one or more of the optional error conditions are not detected.

Certain threads-related functions are not allowed to return an error code of [EINTR]. Where this applies it is stated in the ERRORS section on the individual function pages.

The following macro names identify the possible error numbers, in the context of the functions specifically defined in this volume of POSIX.1-2008; these general descriptions are more precisely defined in the ERRORS sections of the functions that return them. Only these macro names should be used in programs, since the actual value of the error number is unspecified. All values listed in this section shall be unique, except as noted below. The values for all these macros shall be found in the <errno.h> header defined in the Base Definitions volume of POSIX.1-2008. The actual values are unspecified by this volume of POSIX.1-2008.

[E2BIG]
    Argument list too long. The sum of the number of bytes
    used by the new process image's argument list and environment
    list is greater than the system-imposed limit of {ARG_MAX} bytes.

    or:

    Lack of space in an output buffer.

    or:

    Argument is greater than the system-imposed maximum.
[EACCES]
    Permission denied. An attempt was made to access a file
    in a way forbidden by its file access permissions.
[EADDRINUSE]
    Address in use. The specified address is in use.
[EADDRNOTAVAIL]
    Address not available. The specified address is not
    available from the local system.
[EAFNOSUPPORT]
    Address family not supported. The implementation does
    not support the specified address family, or the specified
    address is not a valid address for the address family
    of the specified socket.
[EAGAIN]
    Resource temporarily unavailable. This is a temporary
    condition and later calls to the same routine may complete
    normally.
[EALREADY]
    Connection already in progress. A connection request is already
    in progress for the specified socket.
    .
    .
    .