943,865 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 756
  • C RSS
Mar 19th, 2009
0

execute some code just before stopping program using CTRL-C

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Mar 19th, 2009
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 19th, 2009
0

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

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster
kenji is offline Offline
145 posts
since May 2008
Mar 19th, 2009
0

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

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. }
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 20th, 2009
0

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

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
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 23rd, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Mar 23rd, 2009
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Mar 24th, 2009
0

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

Thanks for advising Ancient Dragon.
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: student grading system
Next Thread in C Forum Timeline: anyone..plz help me how to do this assingment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC