views:

34

answers:

1

Hey guys, I'm new to UNIX, trying to teach myself and came across this practice question in the book im using. My guess is that the program terminating simply calls the exit function if an invalid input is passed to it. Please let me know if I'm on the correct path! Thanks guys!

+1  A: 

That's one way for a program to terminate. If you call exit with a non-zero number, the program will have "exited normally with error status". There are other ways. If someone kills the process, it will die abnormally. If the program segfaults (accesses an address that isn't mapped to it), it'll be killed with SIGSEGV.

That answers the question of "what causes a program to terminate". That doesn't quite answer "what process is undertaken", which probably means "what does the kernel do when a program crashes?" I don't know all the gory details, but I know that when a program does something illegal, it doesn't (unless there's a bug in the kernel or something) corrupt memory of any other part of the system, it just stops the program from running and returns the resources it was using to the system.

Joey Adams
Thank you Joey for your reply! That does help a lot with my question and has pointed me in the right direction. One more question though - In the event that the process is killed, either by the user/OS or by SIGSEGV, is exit() called at all? Thanks.
Drizzy
@user354822: No, it is not called. Remember that `exit()` is a system call invoked by a userland program. If that program doesn't exist anymore, how can it call exit() ? Also, the man page for `atexit()` says that functions registered with atexit are not called if "a process terminates abnormally because of the delivery of a signal" (SIGSEGV (segmentation fault), SIGTERM (kill), SIGKILL (kill -9), etc.).
Joey Adams