Re: Cntrl-C Exit and Error Exits Question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11 July 2017 at 20:35, Thomas Dineen wrote:
> Gentle People:
>
>    Some questions on program exits and exit error handlers.
>
>    I am developing a multi-platform application using Gnu/Gcc tools.

Your question has nothing to do with GCC, it's about general C
programming in a UNIX environment. There are hundreds of resources for
that on the web.

>    When you Cntrl-C a program is an exit handler of function called?
> Is it possible to attach a user supplied Exit Handler to do some
> cleanup like close sockets?

A SIGINT signal will be raised by Ctrl-C, so you can set a signal
handler for that signal, which can set a flag to say the program has
been interrupted. On return from the signal handler the main program
can check for that flag (e.g. in your main event loop) and exit
cleanly.

>     When I Cntrl-C my program it appears the a socket may be left open
> causing errors when I restart the program. So I am thinking about exit
> handlers to close sockets, close files, and release memory.

The sockets are not still held open by the process, because the
process no longer exists. But they will be in a TIME_WAIT state if it
wasn't cleanly shutdown, and so won't be reusable until that times
out. Read about the SO_REUSEADDR socket option, and maybe SO_LINGER.

You don't need to close files or release memory when a process exits,
that happens automatically. A process that doesn't exist can't still
be using memory.

>      If a program crashes on a common error like divide by zero, or bad
> pointer is it possible to add an error handler to catch and process these
> errors?

You generally don't want to.

Again, none of this is about GCC, so this is not the right list to ask.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux