Terminating the program gracefully in C

Sarp Kaya picture Sarp Kaya · Sep 9, 2012 · Viewed 18.2k times · Source

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?

Answer

Deepanjan Mazumdar picture Deepanjan Mazumdar · Sep 9, 2012

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.