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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.
I'm not sure what schools teach these days, but I would guess that terminate normally means "exit with 0 (zero) status".
I can think of 3 ways to do this (at least in a C program):
return 0; /* 1 */ exit(0); /* 2 */ _exit(0); /* 3 */
I think (1) and (2) are just about equivalent, but (3) causes a process to exit without flushing its output buffers. You'd use this in the case of a fork/exec situation to avoid double-output of any bytes buffered up (by kernel or stdio) before the child process exits.
In order to terminate a process, a signal is sent to it. For most of these signals, the process can catch such signal, do some stuff ( release network resources, print good bye) and (most of time) terminate.
What I have described is a way for a process to be killed gracefully, or normally. The process can also terminate if it has finished its job, or using a "x" button on GUI program. But since this is unix related site, I'm guessing otherwise.
killall -SIGHUP firefox
killall -SIGINT firefox
Non gracefull termination
killall -SIGKILL firefox