Meant with terminating a process normally

exitkillprocess

In an examination it is stated:

Give 2 different ways that a process can terminate normally. Describe
the differences.

What is meant with terminate normally?

Best Answer

The term 'normally' used in this context typically means that an executing process exits when having completed all its instructions successfully, without e.g. being killed by signals or crashing unexpectedly. For example: Ctrl+C is hit while the command find is running and the find process recevives the SIGINT signal. Handling this signal is implementation specific, but usually this means killing the process normally. A normal termination of a C program happens upon successfully returning from the function main being the last executed statement in the program.

This contrasts a process that for instance does not complete all the instructions and exits in non-deterministic manner. This can be caused by programmatic errors or it can be caused by signals issued by other processes to kill it. An abnormal termination is often determined in the logic implemented in the program e.g. a child process aborts/exits to the parent process with some return value x, indicating some state or event in execution that is not 'normal'. For example: a process recevives the SIGKILL signal. This signal can not be handled and kills the process.

Here the process abstraction is similar to a programmatic function in that they both share the ability to return a value to the calling process e.g. a shell. The semantics of these return values defines what qualifies a "successful" or a "unsuccessful" call. So in this case it's the programmer who is left with the responsibility for defining or using these semantics.

To make a concrete example: a command executed from bash will eventually return a value to it's caller upon termination. Here, any value other than 0 indicates abnormal termination of the command. In bash you can inspect the variable $? to see the return value of the last executed statement. Using this return value can be useful when controlling the flow of execution in a script e.g. to prevent reading from a non-existing file or such.

There are also other things to consider when talking terminating processes, such as freeing allocated address space with associated resources in memory, plus cpu scheduling of jobs, but that's another chapter.