c exit status

32 篇文章 0 订阅
25.6.2 Exit Status

When a program exits, it can return to the parent process a smallamount of information about the cause of termination, using theexit status. This is a value between 0 and 255 that the exitingprocess passes as an argument to exit.

Normally you should use the exit status to report very broad informationabout success or failure. You can't provide a lot of detail about thereasons for the failure, and most parent processes would not want muchdetail anyway.

There are conventions for what sorts of status values certain programsshould return. The most common convention is simply 0 for success and 1for failure. Programs that perform comparison use a differentconvention: they use status 1 to indicate a mismatch, and status 2 toindicate an inability to compare. Your program should follow anexisting convention if an existing convention makes sense for it.

A general convention reserves status values 128 and up for specialpurposes. In particular, the value 128 is used to indicate failure toexecute another program in a subprocess. This convention is notuniversally obeyed, but it is a good idea to follow it in your programs.

Warning: Don't try to use the number of errors as the exitstatus. This is actually not very useful; a parent process wouldgenerally not care how many errors occurred. Worse than that, it doesnot work, because the status value is truncated to eight bits. Thus, if the program tried to report 256 errors, the parent wouldreceive a report of 0 errors—that is, success.

For the same reason, it does not work to use the value of errnoas the exit status—these can exceed 255.

Portability note: Some non-POSIX systems use differentconventions for exit status values. For greater portability, you canuse the macros EXIT_SUCCESS and EXIT_FAILURE for theconventional status value for success and failure, respectively. Theyare declared in the file stdlib.h.

— Macro: int EXIT_SUCCESS

This macro can be used with the exit function to indicatesuccessful program completion.

On POSIX systems, the value of this macro is 0. On othersystems, the value might be some other (possibly non-constant) integerexpression.

— Macro: int EXIT_FAILURE

This macro can be used with the exit function to indicateunsuccessful program completion in a general sense.

On POSIX systems, the value of this macro is 1. On othersystems, the value might be some other (possibly non-constant) integerexpression. Other nonzero status values also indicate failures. Certainprograms use different nonzero status values to indicate particularkinds of "non-success". For example, diff uses status value1 to mean that the files are different, and 2 or more tomean that there was difficulty in opening the files.

Don't confuse a program's exit status with a process' termination status. There are lots of ways a process can terminate besides having it's programfinish. In the event that the process termination is caused by programtermination (i.e., exit), though, the program's exit status becomespart of the process' termination status.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值