signal trapping in linux

Posted on 1998-11-21
Last Modified: 2010-04-15
Consider the following code
void alter();
int i;
void alter()
When I try to beak using Ctrl+C it does not go into the alter sub function.
However if use a sleep function within the for loop it connects to the alter sub function.
How can I make the SIGINT signal to be trapped in the first case.
Question by:sobhan102398
  • 3

Author Comment

ID: 1254535
I tried it again today.
I observed it closely and this is what i got,
When I press Ctrl + C the signal is being generated but very late and printf("Interrupted) print's so fast that is difficult to track. I used the following
void alter()
The sleep doesn't work and printf statement is executed like as I have stated above.

Also when I try out this for the signal signal(SIGPWR,alter1);
void alter()
I switch off the computer and when I on it again the file is not created i.e SIGPWR is not trapped.Why is it so.

One last question regd signal trapping
can i trap the signal for the following code
I used sleep(1) like this
Now I was able to trap it.
How can I trap the signal in the first case without using sleep.
Is read and write non blocking.
LVL 84

Expert Comment

ID: 1254536
What are you trying to do?
I'd expect
for(i=0;i<1000;i++) printf("Uninterrupted\n");
to run so fast that the ouput buffer is full before you can type a Ctrl+C
and even if you can type Ctrl+C fast enough, the buffer won't be flushed before the next "\n" is printed.

Author Comment

ID: 1254537
I am trying to copy one file to another. When I interrupt the process, I should save till that point by calling alter where I write some code for that. However I am unable to trap the signal SIGINT within this loop
I have to use sleep inorder to capture the signal.I want my program to trap the signal without using sleep

Accepted Solution

rdelfino earned 60 total points
ID: 1254538

The code you showed probably is running so fast, it ends processing before the
signal is handled to your process. Try using longer processing loops, so may be you will
be able to catch all the signals you want (except those you cannot catch, as SIGKILL)

I hope it helps


Author Comment

ID: 1254539
Also we connect from dos to linux using a ncsa rlogin program. Whenever we                     press Alt+a we get a new session . Or we press Alt+x we close the session.
                    i.e The program is written such that when a person presses Alt+a he goes to a particular function. So they are trapping keys at any point.
                    How can I simulate this.
I need to go to a particular function from whatever state I am when I press a selected  hot key. Using this I can bypass the above problem(printf pob) by using another set of keys
other than Ctlr+C.
How do I do this

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 146
why "." vs "->" 23 120
C language IDE – Compilers installation 14 71
Global Keyboard Hooks Blocked 4 75
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now