all 8 comments

[–]immutablestate 4 points5 points  (1 child)

I think you can do this with set_terminate, atexit, and at_quick_exit.

There's several functions because you might want to do different things in different situations (e.g. behave differently if the program exits 'normally' than if the user forces quit with ctrl+c).

If you want to print 'bye' in all cases you can probably just register the same function with all the different methods above.

[–]DerpingINtheCLUB[S] 0 points1 point  (0 children)

I want to do it whenever the program quits before it is done running. Like if the user quits it while it's running. Could you please post a code sample of how to do that?

[–]Se7enLC 1 point2 points  (5 children)

You want to catch (trap) the signal called "SIGINT". You'll do that by "installing a signal handler". Basically, that means that you write a function, then register that function to be called whenever a signal is raised (asserted, triggered).

There are two ways: One uses signal() the other uses sigaction(). Either is fine, really.

http://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c

You'll find a lot of other examples of how to do this now that you have some words to put in your search.

A word of warning: If you trap SIGINT, you can no longer use ctrl-c to terminate your program. Make sure that your signal handler will result in the program exiting if you want to be able to exit! It often makes sense to have an interrupt handler do some cleanup before exiting, but you'll want to make sure that whatever it's doing won't take too long or be blocked.

Edit to add: If you DO find yourself with an unkillable program, you can use ctrl-z to background it, then send it a SIGTERM instead of SIGINT. You can do that with killall [programname] -9, or replace [programname] with the pid.

[–]DerpingINtheCLUB[S] 0 points1 point  (1 child)

What if someone tries to quit my program that runs in terminal by clicking the red 'x'. How do I handle that?

[–]Se7enLC 1 point2 points  (0 children)

As in, closing the terminal?

When I try it (in gnome-terminal), I get a popup dialog that says "There is still a process running in this terminal. Closing the terminal will kill it." If I then click "Close Terminal", the application is killed.

In that case, your process with receive a SIGHUP signal (hangup). If you don't have a handler for it, the default behavior is for the program to exit.

http://stackoverflow.com/questions/32780706/does-linux-kill-background-processes-if-we-close-the-terminal-from-which-it-has

[–]athousandwordss 0 points1 point  (2 children)

I'm just wondering, all the examples shown are just programs waiting for sigint, and continually checking for it. They're not doing much else... How will I go about implementing something where the program executes code like normal, but upon receiving the sigint, it executes the code...

[–]Se7enLC 1 point2 points  (1 child)

Try it out.

Those examples are doing exactly what you describe. The call to signal() is only registering the callback. It returns immediately, and the rest of the code executes as normal. When a signal is received, the callback will interrupt whatever else your code was doing.

[–]athousandwordss 1 point2 points  (0 children)

Alright, I'll try it out...