Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Catching Ctrl-C termination in C programs?

Catching Ctrl-C termination in C programs?
Thread Tools
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Sep 10, 2004, 09:16 PM
 
Is there any way that I can execute some code before the a C application quits when sending it Ctrl-C?
     
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Sep 10, 2004, 09:39 PM
 
should be a sigalam handler right?
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Sep 10, 2004, 10:07 PM
 
Originally posted by BLAZE_MkIV:
should be a sigalam handler right?
Ah, don't know what a sigalam handler is, but that pointed me in the right direction. I'm no C programmer (java here), but need to know this, so apparently it's called "signal handling". I found myself a nice tutorial thanks.
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Sep 10, 2004, 11:58 PM
 
I believe you'll find it's SIGINT not SIGALRM.

- proton
     
Forum Regular
Join Date: Jan 2001
Status: Offline
Reply With Quote
Sep 11, 2004, 06:57 AM
 
I got a small exemple that catch a Ctrl C signal. It small and should be easy to understand.

you can d/l it from:

http://www.cs.umu.se/~dv02mgn/sigtest.c
http://www.cs.umu.se/~dv02mgn/sig.c
http://www.cs.umu.se/~dv02mgn/sig.h

conmpiles whit:

gcc -Wall -pedantic -o sigtest sigtest.c sig.c

execute with

./sigtest

it sets upp the signal handler and loops forever, press ctrl c to exit. If you look in the sig.c file, the ctrlC function is executed when a SIGINT (ctrl c) signal is sent.

hope it helps.
     
Dedicated MacNNer
Join Date: Mar 2003
Location: UK
Status: Offline
Reply With Quote
Sep 11, 2004, 07:14 AM
 
Code:
// Signal handler function. void handleSignal(int signum) { switch(signum) { case SIGHUP: printf("Caught signal HUP, quitting..."); break; case SIGQUIT: printf("Quitting..."); break; case SIGINT: printf("Interrupted, quitting..."); break; default: printf("Catching signal %i, quitting...", signum); } // Shutdown gracefully. exit(0); } // Register signal handlers for common signals. void registerSignalHandlers() { signal(SIGHUP, handleSignal); signal(SIGQUIT, handleSignal); signal(SIGINT, handleSignal); signal(SIGABRT, handleSignal); }
Just call registerSignalHandlers() somewhere at the start of your program, and fill in what you want to do where I've put the printf statements.
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Sep 11, 2004, 08:26 AM
 
Note that there are very few functions that are safe to call from signal handlers.
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Sep 11, 2004, 11:59 AM
 
Thanks everyone for the great help!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:14 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2