C 中的所有系统调用都将 -1 作为其错误代码吗?
Do all systems calls in C associate -1 as its error code?
例如,int fork
returns -1
出错。 int pipe
也 returns -1
出错。 int socket
也 returns -1
出错。
我想知道,在出错时执行 all
系统调用 return -1
。我见过带有错误检查的代码,它们完全不同。有些做 return value < 0
,有些做 return value == -1
,有些做 return value < 1
,所以,我能做些什么来检查错误 any系统调用?
我必须检查什么?或者它实际上取决于系统调用?总不能做< 0
吧?
what can I do to error check any system call?
您必须阅读该特定系统调用的文档。没有统一的约定。
如果有效 ID 被定义为始终为非负数,通常使用 -1
表示 return 某种 ID(进程 ID、文件描述符等)。
I want to know, do all system calls return -1 on error.
没有。在不提供任何实际示例的情况下,我引用 POSIX 1003.1-2008, 2016 Edition:
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
[...].
Some functions return an error number directly as the function value.
These functions return a value of zero to indicate success.
通过 errno
提供错误详细信息的函数也通过它们的 return 值表示发生了(未指定的)错误。大多数此类信号采用 return 值 -1 的形式。
您继续查询:
I've seen code
with error checking, and it's all different. Some of them do return
value < 0
, some of them do return value == -1
, some of them do return
value < 1
, and so, what can I do to error check any system call?
许多通过 returning -1 发出错误信号的函数不会记录任何其他负 return 值。对于此类函数,测试 return 值是否恰好为 -1 和测试它是否为负值具有相同的目的。根据 1 测试 return 值并不等价,但它可能对某些特定上下文有效,例如当它用于检测函数失败和函数成功时,仍然指示应用程序级错误,例如错误的用户输入。
如引用文本所示,每个函数都记录了它发出错误信号的机制,以及如何获取相应的错误编号(如果有)。您可以通过选择与其文档一致的任何方法来检查特定函数的错误。 POSIX 在这方面包含两个主要的 类 函数——通过 returning -1 发出错误信号的函数和通过 returning 任何一个函数发出错误信号的函数非零值——但有时您还需要处理其他函数。如果您不确定,最好查阅您正在使用的函数的文档。
例如,int fork
returns -1
出错。 int pipe
也 returns -1
出错。 int socket
也 returns -1
出错。
我想知道,在出错时执行 all
系统调用 return -1
。我见过带有错误检查的代码,它们完全不同。有些做 return value < 0
,有些做 return value == -1
,有些做 return value < 1
,所以,我能做些什么来检查错误 any系统调用?
我必须检查什么?或者它实际上取决于系统调用?总不能做< 0
吧?
what can I do to error check any system call?
您必须阅读该特定系统调用的文档。没有统一的约定。
如果有效 ID 被定义为始终为非负数,通常使用 -1
表示 return 某种 ID(进程 ID、文件描述符等)。
I want to know, do all system calls return -1 on error.
没有。在不提供任何实际示例的情况下,我引用 POSIX 1003.1-2008, 2016 Edition:
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
[...].Some functions return an error number directly as the function value. These functions return a value of zero to indicate success.
通过 errno
提供错误详细信息的函数也通过它们的 return 值表示发生了(未指定的)错误。大多数此类信号采用 return 值 -1 的形式。
您继续查询:
I've seen code with error checking, and it's all different. Some of them do
return value < 0
, some of them doreturn value == -1
, some of them doreturn value < 1
, and so, what can I do to error check any system call?
许多通过 returning -1 发出错误信号的函数不会记录任何其他负 return 值。对于此类函数,测试 return 值是否恰好为 -1 和测试它是否为负值具有相同的目的。根据 1 测试 return 值并不等价,但它可能对某些特定上下文有效,例如当它用于检测函数失败和函数成功时,仍然指示应用程序级错误,例如错误的用户输入。
如引用文本所示,每个函数都记录了它发出错误信号的机制,以及如何获取相应的错误编号(如果有)。您可以通过选择与其文档一致的任何方法来检查特定函数的错误。 POSIX 在这方面包含两个主要的 类 函数——通过 returning -1 发出错误信号的函数和通过 returning 任何一个函数发出错误信号的函数非零值——但有时您还需要处理其他函数。如果您不确定,最好查阅您正在使用的函数的文档。