失败时返回代码.正面还是负面? [英] Return Code on failure. Positive or negative?

查看:83
本文介绍了失败时返回代码.正面还是负面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特殊情况下,Linux中的C程序可能无法执行. 示例:您分配了一些空间,但操作系统拒绝了它.

a C-programm can fail to execute under special circumstances in Linux. Example: You allocate some space and the OS denies it.

char *buffer = (char *) malloc(1024);
if (buffer == NULL)
    return ENOMEM;

此失败由交付给操作系统的返回码标记.

This failure is marked by the return code which is delivered to the OS.

  • 返回码0(EXIT_SUCCESS)被标记为成功执行.
  • 不为0的返回码被标记为失败.

所以我的问题是,程序检测到错误时的约定是什么? 它应该返回正数还是负数?

So my question is, what is the convention, when a program detects an error. Should it return a positive or a negative return code ?

我的教授一直告诉我在UNIX/Linux中返回负错误代码.但是errno码都是正整数.同样,EXIT_FAILURE中的define-Statement为1,为正整数. 所以我的代码应该像这样:

My professor told me in UNIX/Linux allways to return a negative error code. But the errno-Codes are all positive integers. Also the define-Statement of EXIT_FAILURE in is 1, a positive integer. So should my code look like this:

char *buffer = (char *) malloc(1024);
if (buffer == NULL)
    return -ENOMEM;

还是喜欢上面的代码?我从Linux内核模块知道,在大多数情况下,它们会在失败时返回负错误代码.

or like the code above? I know from Linux-Kernel-Modules that in most cases they return a negative error code on failure.

感谢您的帮助.

推荐答案

所以我的问题是,当程序检测到 错误.它应该返回正数还是负数?

So my question is, what is the convention, when a program detects an error. Should it return a positive or a negative return code ?

您没有做出选择的奢侈(或责任).从C的角度来看,您的选择分为三类:

You do not have the luxury (or responsibility) of making that choice. From the point of view of C in general, your choices fall into three categories:

  • 您指定的退出状态为0EXIT_SUCCESS(不一定是不同的).这表示成功完成.

  • The exit status you specify is either 0 or EXIT_SUCCESS (which are not necessarily distinct). This indicates successful completion.

您指定的退出状态为EXIT_FAILURE.这表示未成功完成.

The exit status you specify is EXIT_FAILURE. This indicates unsuccessful completion.

您指定的退出状态是其他任何内容.

The exit status you specify is anything else.

在每种情况下,发送给主机环境的结果都是实现定义的,尽管在​​第一种情况下,它传达了成功,而在第二种情况下,则传达了失败.

The result signaled to the host environment is implementation-defined in every case, though in the first it is one that communicates success, and in the second it is one that communicates failure.

现在,在Linux和其他实现POSIX exit()语义的操作系统上,实现定义是标准化的:

Now, on Linux and other operating systems implementing POSIX exit() semantics, the implementation-definedness is standardized:

状态值可以是0EXIT_SUCCESSEXIT_FAILURE或任何其他值,尽管只能从status & 0377) >和waitpid() ;

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available from wait() and waitpid();

( IEEE Std 1003.1-2008,2016版;已添加重点).这些是传统的UNIX语义.您指定的退出状态只有最低有效的8位重要,因此签名丢失了.但是,当前版本的POSIX继续说:

(IEEE Std 1003.1-2008, 2016 Edition; emphasis added). Those are the traditional UNIX semantics; only the least-significant 8 bits of the exit status you specify matter, so the signedness is lost. However, the current version of POSIX goes on to say:

完整值应可从waitid()获得,并在siginfo_t中传递给SIGCHLD的信号处理程序.

the full value shall be available from waitid() and in the siginfo_t passed to a signal handler for SIGCHLD.

因此,您可以将负数传达给父进程(如果它知道要查找一个).但是总的来说,它不会.特别是,如果父进程是shell,那么它将不会.

Thus, you can communicate a negative number to the parent process if it knows to look for one. In general, however, it won't do. In particular, if the parent process is the shell then it won't do.

我的教授总是告诉我在UNIX/Linux中返回负错误代码.

My professor told me in UNIX/Linux allways to return a negative error code.

这听起来更像是关于除main() 之外的函数的函数返回码的指令,但是我不明白为什么您认为我们关于教授的意思的第二种猜测会比直接请您的教授澄清.

That sounds more like an instruction about function return codes for functions other than main(), but I don't see why you think our second guessing about what your professor meant would be more reliable than asking your professor for clarification directly.

但是errno码都是正整数.

But the errno-Codes are all positive integers.

好吧,与您的教授要澄清的另一件事是,他是否在向您提供有关他如何期望您为自己的班级编写函数的特定说明(更有可能),或者他是否对通用C或POSIX约定进行断言. (不太可能,因为有很多POSIX函数不是真的)或其他.

Well, another thing to clarify with your professor would be whether he was giving you specific instructions about how he expects you to write functions for his class (more likely), or whether he was making an assertion about general C or POSIX convention (unlikely, because there are many POSIX functions of which it is not true), or something else.

请注意,C标准库函数不会返回errno代码.当他们返回指示失败的代码(通常但不总是为负数)时,出于详细原因,程序员必须查阅errno变量-函数返回代码通常不携带该信息.某些由POSIX标准化但不是C do 的函数直接返回errno代码.显然,这些都不符合您教授的处方.

Note well that C standard library functions do not return errno codes. When they return a code indicating failure (often, but not always, a negative number), the programmer is obliged to consult the errno variable for a detailed reason -- the function return code generally does not carry that information. Some functions standardized by POSIX but not C do return errno codes directly; obviously, these do not follow your professor's prescription.

还请注意,errno值用于在同一程序调用的函数之间进行通信,而不用于与程序环境进行通信.它们不打算用作程序退出状态,尤其不能保证它们的值适合8位(见上文).

Note also that errno values are for communicating between functions called by the same program, not for communicating with the program's environment. They are not intended for use as program exit statuses, and in particular, there is no guarantee that their values fit in eight bits (see above).

EXIT_FAILURE中的define-Statement也为1,为正整数.所以我的代码应该像这样:

Also the define-Statement of EXIT_FAILURE in is 1, a positive integer. So should my code look like this:

EXIT_FAILURE的值取决于实现.因此,您只能从中收集特定于实现的见解.但是,如您所见,表明您的实现中程序失败的通用退出状态为1.

The value of EXIT_FAILURE is implementation-dependent. You can therefore glean only implementation-specific insight from it. As you can see, however, the general-purpose exit status indicating program failure in your implementation is 1.

在Linux和其他POSIX(-ish)系统上,您可以通过在1到125之间选择故障状态来实现与Shell的最佳集成,因为Shell为状态126-255指定了特殊的意义,并且当然会解释状态0.作为成功.

On Linux and other POSIX(-ish) systems, you achieve best integration with the shell by choosing failure statuses between 1 and 125, as the shell designates special significance to statuses 126-255, and, of course, interprets status 0 as success.

char *buffer = (char *) malloc(1024);
if (buffer == NULL)
    return -ENOMEM;

还是喜欢上面的代码?我从Linux内核模块知道 在失败的情况下返回负错误代码.

or like the code above? I know from Linux-Kernel-Modules that in most cases they return a negative error code on failure.

再次,不. Errno代码不是要用作程序退出代码,只有在相当特殊的情况下,您才应尝试以否定状态退出.常见的约定是向stderr写一个有用的错误消息(perror()正是用于此目的),然后以状态1或(更好,IMO)EXIT_FAILURE退出.

Again, no. Errno codes are not meant for use as program exit codes, and only in rather special circumstances should you try to exit with a negative status. A common convention is to write an informative error message to stderr (perror() serves exactly this purpose) and then exit with status 1 or (better, IMO) EXIT_FAILURE.

如果要提供描述故障性质的退出代码,则取决于每个程序来定义其值和重要性,请记住,为了获得最佳兼容性,您只能使用1-125

If you want to provide exit codes that describe the nature of the failure then it is up to you to define their values and significance on a per-program basis, remembering that for best compatibility you have only 1 - 125 to work with.

这篇关于失败时返回代码.正面还是负面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆