- When you're using shell script to automate processing by using several programs to perform "sub-tasks". In this case, the shell script--very possibly--need to make logical decision based on your program exit status.
- When your C/C++ program is part of a multiprocess program in which your C/C++ program is called/executed (a.k.a fork-ed and exec-ed) by the parent process. In many cases, the parent process need to know whether your program executes successfully or not.
Now, let's be more concrete. Let's say you anticipated that your C/C++ program will be invoked by bash-compatible shell. In that case, your code's exit status must make sense to bash. Therefore, you should:
- Use return meaningful exit status in Bash from main(). See: http://www.tldp.org/LDP/abs/html/exit-status.html
- Avoid using reserved Bash exit status. See: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF for reserved exit status.
- Use values from sysexits.h for exit status in your C/C++ program.
Following the rules above doesn't necessarily mean your C/C++ program will be bug free because some of the exit status are ambiguous. Nevertheless, it should make it more palatable to be combined into larger system and ease debugging.
As closing, let's look at a very simple code that uses sysexits.h below.
#include <stdio.h> #include <sysexits.h> /** * Usage: * test_code -x param1 -z param2 */ int main(int argc, char *argv[]) { if (argc != 5) { printf("Usage: %s -x param1 -z param2\n", argv[0]); return EX_USAGE; } //... irrelevant code omitted return EX_OK; }
Post a Comment
No comments:
Post a Comment