Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


signal trapping in linux

Posted on 1998-11-21
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 120 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

609 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