Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
We need context. Are we talking about programming concepts (how to make a program exit), or system administrations concepts (how programs do exit). –  Patrick Jan 6 '12 at 0:25
    
Given that three people have answered this one three different ways reveals more about the "quality" of this examination than about "terminating normally". The examination question has no merit. Ignore it. –  Bruce Ediger Jan 6 '12 at 3:05
    
@Patrick the course is "Programming in Unix with C". I guess it is both.. –  Pithikos Jan 6 '12 at 13:45
2  
@Pithikos And that's an actual question? I recommend you look for a better course, preferably one that's accredited by something that didn't come out of a crackerjack box. –  Shadur Jan 6 '12 at 14:30
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
The problem is that 'exit normally' is open to interpretation. Some applications have signal handlers for things like ctrl+c (sigint) and will exit gracefully when they receive the signal. While such an application doesnt exit of its own accord, it does exit normally. –  Patrick Jan 6 '12 at 0:22
add comment

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.

share|improve this answer
add comment

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.

Note, two signals can not be caught by process, SIGKILL and SIGSTOP. Termination by SIGKILL is consider not normal termination.

Normal termination
killall -SIGHUP firefox
killall -SIGINT firefox

Non gracefull termination
killall -SIGKILL firefox

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.