execute some code just before stopping program using CTRL-C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

execute some code just before stopping program using CTRL-C

 
0
  #1
Mar 19th, 2009
hi everyone.

my program uses an infinite loop & runs forever.
i want to run some codes when i decide to stop the program using CTRL-C.

Is this possible ? Please advise me.

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: execute some code just before stopping program using CTRL-C

 
0
  #2
Mar 19th, 2009
There isn't a standard way to do it -- depends on the operating system. You might try calling onexit(), but it may not get called when Ctrl-C is hit.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 10
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: execute some code just before stopping program using CTRL-C

 
0
  #3
Mar 19th, 2009
I'm not exactly sure what you mean, but as CTRL-C is used to terminate your program I doubt any code will be allowed to run.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: execute some code just before stopping program using CTRL-C

 
0
  #4
Mar 19th, 2009
You could try signal():
  1. #include <stdio.h>
  2. #include <signal.h>
  3.  
  4. void cleanup(int unused) {
  5. printf("Cleaning up\n");
  6. exit(-1);
  7. }
  8.  
  9. int main() {
  10. signal(SIGINT, cleanup);
  11. for (;;) /* Ctrl-C to exit */
  12. ;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: execute some code just before stopping program using CTRL-C

 
0
  #5
Mar 20th, 2009
As nucleon illustrated above it will depend on the operating system. His code is *nix only and possily MAC. For MS-Windows, see this article
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: execute some code just before stopping program using CTRL-C

 
0
  #6
Mar 23rd, 2009
thanks nucleon & Ancient Dragon.

I modified the way the program will be running.It will run in the background so no output will be visible to the console.

I want to use the kill command & achieve same result.
Plz advise me what i need to modify.

thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: execute some code just before stopping program using CTRL-C

 
0
  #7
Mar 23rd, 2009
you must be running *nix because MS-Windows doesn't have such a command.
kill -9 123 where 123 is the pid of the running process.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: execute some code just before stopping program using CTRL-C

 
0
  #8
Mar 24th, 2009
Thanks for advising Ancient Dragon.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 550 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC