What happens when the main () function does not return zero

Pay attention to the following code:

#include <stdio.h> int main() { int x; printf ("\nEnter x: "); scanf ("%d", &x); return x; } Output: $Enter x: -2 $echo $? 254 

My question is:

The OS knows that the return value of main () by executing the P process (the code above) is NON-ZERO.

Does the OS support any operations in such cases?

+4
source share
3 answers

It should be clear here what you mean by “operating system”.

If you look from the point of view of the shell (which in any case is just another program for user space, and not part of the kernel itself), the value of the process output is used to evaluate the value of the expression and is important when performing shell programming, since this is the most natural check that after completion of the process.

From the point of view of the kernel, the value of the process output deviates significantly. The kernel should clean up after the process, regardless of the exit code returned.

+4
source

The return value actually indicates the program termination status. A value other than zero indicates that the program was aborted abnormally. Status can be used when using other programs, so it is always recommended to return the actual status.

+4
source

Usually, when the program executes "as expected", we return 0. If it does something, it should not, we return a nonzero value depending on the type of error (for this you can refer to the errno manual). Now this return value is received by the "shell" under which the program was launched. This return value can be obtained using "$?" in BASH. Thus, it is purely for the user accessibility for the program (that it works correctly, and if not, then what error). This return value can then be used in shell scripts, etc., to make decisions about whether to repeat or what to do, what the administrator wants to do. The kernel simply clears the user land program.

+1
source

Source: https://habr.com/ru/post/1444271/


All Articles