I have sort of a homework and it asks me to end the program gracefully without explicit termination such as calling exit() or killing the threads.
However I cannot think of any other methods than return 0
, so what are the methods to end a program gracefully?

- 372,613
- 87
- 782
- 758

- 3,686
- 21
- 64
- 103
-
`main` will by default return *0* in the lack of a `return 0;` statement. – obataku Sep 09 '12 at 15:31
-
4Gracefully ? Perhaps they meant printing "Goodbye" ? – cnicutar Sep 09 '12 at 15:31
-
2"Thou shalt permit the programme to dye..." – Sep 09 '12 at 15:31
-
@Cnicutar, I don't think so as it does not allow us to use exit() – Sarp Kaya Sep 09 '12 at 15:32
-
1Can you post the text of the homework ? Maybe you're not reading it completely right :-) – cnicutar Sep 09 '12 at 15:33
-
@cnicutar "The input 'q' indicates that the simulation has terminated. Note that the simulation must end gracefully without explicit termination such as calling exit() or killing the threads." – Sarp Kaya Sep 09 '12 at 15:35
-
2I believe what your homework says is that you should not have more than 1 exit point. Be it a `return 0`, or an `exit(0)`, it should not make any difference. – jweyrich Sep 09 '12 at 15:36
-
How about if I use return EXIT_SUCCESS; ? – Sarp Kaya Sep 09 '12 at 15:44
6 Answers
Killing the threads is absolutely not a graceful way to terminate a program. I think what your instructor means is that all your parent
threads should wait on their child
threads before terminating themselves.
Ideally, an explicit call pthread_exit
from the main thread would ensure that all it's children continue running even after it exits. Refer to this link. But, the safest way to wait on your child threads before exiting is to use pthread_join
.
Nevertheless, exit(0)
is the graceful return for a process as such.

- 1
- 1

- 1,447
- 3
- 13
- 20
-
This is correct answer, when I asked to my teacher, what needs to be done was actually `pthread_join` waits the thread to return NULL with `pthread(threadname, NULL);` so in my threads there were loops inside of them, so the solution was to finish these loops with a variable so that all threads could return NULL – Sarp Kaya Sep 14 '12 at 09:23
I think you are missing to tell us that you have a multi-threaded program. I suppose that the the idea of gracefully terminating the program is meant to terminate all your threads by setting a flag or something like that. And then only to terminate your main
after all your threads have provably ended. The way you actually then terminate your main
is of lesser importance.

- 76,821
- 6
- 102
- 177
-
+1. It's not about language syntax nor methods of terminating a process. It's about properly ending threads, closing and deallocating resources, etc. That's what I understand from *gracefully*. – jweyrich Sep 09 '12 at 15:51
exit(0)
generally indicates that process (your program) terminated gracefully. In case of error it would exit(-1)
or some other error code.

- 52,392
- 12
- 90
- 87
No idea what they could mean with gracefully but my first idea was just a return 0.
Or exit(0)

- 426
- 1
- 8
- 18
See my comment.
main
will by default return 0 in the lack of areturn 0;
statement.
See §5.1.2.2.3¶1 of the C99 standard.
If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument; reaching the}
that terminates themain
function returns a value of0
. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.
So, the following terminates gracefully although implicitly, distinguished from an explicit exit
or return
in main
.
main() { }
They may be referring to how you handle errors in lower-level routines. Rather than doing something like
printf("ERROR: couldn't initialize the claveman\n");
exit(1);
You would return from that routine (possibly printing the error message at that level or waiting to do it at a higher level like main()).
return CLAVEMAN_INITIALIZE_ERROR;
All your routines would return zero for success or non-zero error codes, up until the code in main was able to return either EXIT_SUCCESS or an error code indicating the failure.

- 6,802
- 8
- 41
- 64