LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2011, 06:11 AM   #1
fneves
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Rep: Reputation: 4
Interrupt handler with an argument


Hello I need help on a program I'm building. What had initially was a loop that opened files, and write them back close. Now what I want to do is to generate an interrupt when I leave the program with CTRL + C and the files were closed, so it is necessary to open them only once.

Code:
#include <signal.h>
 void sigint_handler()
 {      
	//fclose(logfile);

        printf("\nYou have pressed CTRL+C. The files have now been closed and saved!\n");
	exit(0);
 }
  
int main(int argc, char *argv[])
{
	signal(SIGINT, sigint_handler);
        ...

        //openfile
        //while loop 
        //return0
}
What I wonder is whether it was possible to make a break and go into this program and save a file. The problem is that I'm not getting through the files as argument in the interrupt SIGINT. How do I do it?
Thank you very much

Last edited by fneves; 01-15-2011 at 06:13 AM.
 
Old 01-15-2011, 07:02 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
I am sorry if I have mis interpreted you but did you mean that when someone pressed ctrl + c, an interrupt should be triggered?
 
Old 01-16-2011, 07:17 AM   #3
fneves
LQ Newbie
 
Registered: Nov 2010
Posts: 8

Original Poster
Rep: Reputation: 4
What I want to do is when I press CTRL+C, before the programs close, i want to save some open files. Its possible to configure different taps for interruption? It means, can I configure ant interrupt with CTRL+A for example, instead CTRL+C?
Thanks
 
1 members found this post helpful.
Old 01-16-2011, 07:56 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thats an interesting question and searching Google with
Quote:
ctrl c signal
will get interesting results!

Last edited by Aquarius_Girl; 01-16-2011 at 08:29 AM.
 
Old 01-16-2011, 08:55 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by fneves View Post
What I wonder is whether it was possible to make a break and go into this program and save a file. The problem is that I'm not getting through the files as argument in the interrupt SIGINT. How do I do it?
You're close, but slightly off-track.

First, there are functions you shouldn't use in a signal handler. See man 7 signal and specifically the section on Async-signal-safe functions. Signal handlers are not intended to do any significant work. You shouldn't attempt to do any cleanup there; it won't work the way you intend to.

Years ago there was an FTP file server that did exactly what you try to do here. Because of certain interactions between signal handling and operations done in the normal course of the FTP server program, all you needed to do was to hit Ctrl+C at the exact right moment, and you'd get superuser access rights. It stumped a lot of people at first, but eventually the exact sequence was tracked down. It was an important lesson and a reminder for many.

Think about the word break. It's much safer if your signal handler just puts up a flag indicating the user wants to make a break. Sure, you need to think about how to break out of each task your program does, but that's life: there is no magic bullet. You'll most likely notice that the mindset helps you write better code, though. The first thing to typically emerge as a side effect is much more robust error handling; with just a few lines of code more you can handle all sorts of unexpected errors gracefully. I find it much better than just crapping out and leaving the user to wonder what the hell just happened.

There is one common pitfall, though. There is a special integer type, sig_atomic_t, which is guaranteed to work as a signal flag. It has a limited range, only 0 to 127, if you write portable code; its size and range varies from platform to platform (but all include at least the range 0 to 127).
If you define your flag as a global variable of type volatile sig_atomic_t, you can safely access it whenever and wherever you want, and your compiler understands its value may change unexpectedly. All other types are not guaranteed to be atomic; if your main program happens to be reading it while your signal handler modifies them, the main program may read garbage instead. And if both modify them at the same time, the value stored may be garbage. (Although character types char and unsigned char are normally atomic, there may be unexpected interactions on certain platforms if your main program is modifying them or something within the same native word while the signal handler is trying to modify the value.)
And the compiler is allowed to remember and even postpone modifying the value of any non-volatile variable until the next write access -- for example, to just after the heavy, long-running work loop you might have.

In a nutshell: sig_atomic_t is the only type of variable your signal handler may safely read and modify; but your main program is not guaranteed to notice any changes in them. For the main program to be guaranteed to notice, you need to use volatile sig_atomic_t type.
On x86 and other platforms the actual rules are much more relaxed, but I wouldn't rely on them, especially if you want the code to work on all Linux variants.

Hope this helps you,
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 02:53 AM.
 
1 members found this post helpful.
Old 01-16-2011, 09:12 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
It means, can I configure ant interrupt with CTRL+A for example, instead CTRL+C?
No, not really. (You can remap keys, but that's not what you mean, is it?)

You can, however, change the terminal mode, and get every key and key combination as soon as the user presses the keys (if the input is directed at your program). You can even arrange for an IO signal (SIGIO) to be sent whenever that happens, and read the keypresses directly in your signal handler if you are very, very careful (and use unistd.h, and not stdio.h). After that you can flag your main program the way I outlined in my previous post in this thread.
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 02:53 AM.
 
  


Reply



Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel panic: killing interrupt handler! In interrupt handler - not syncing. divyashree Programming 26 05-15-2010 01:27 PM
control is not going to interrupt handler when interrupt remyasj LinuxQuestions.org Member Intro 1 11-12-2009 12:56 AM
control is not going to interrupt handler when interrupt comes in serial driver sateeshalla Linux - Kernel 1 05-04-2006 09:43 AM
<0>Kernel panic: Aiee, killing interrupt handler! In interrupt handler - not syncing mrb Linux - Newbie 2 01-09-2005 09:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration