Solved

signal trapping in linux

Posted on 1998-11-21
5
341 Views
Last Modified: 2010-04-15
Consider the following code
void alter();
main()
{
int i;
signal(SIGINT,alter);
for(i=0;i<1000;i++)
printf("Uninterrupted\n");
}
void alter()
{
printf("Interrupted");
sleep(2);
}
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.
0
Comment
Question by:sobhan102398
[X]
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
5 Comments
 

Author Comment

by:sobhan102398
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()
{
printf("Interrupted\n");
sleep(2);
}
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()
{
create("file",0700);
exit(1);
}
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
while((no=read(f,buff,1024))>0)
write(f1,buff,no);
I used sleep(1) like this
while((no=read(f,buff,1024))>0)
{
write(f1,buff,no);
sleep(1);
}
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.
0
 
LVL 84

Expert Comment

by:ozo
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.
0
 

Author Comment

by:sobhan102398
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
while((no=read(f,buff,1024))>0)
      write(f1,buff,no);
I have to use sleep inorder to capture the signal.I want my program to trap the signal without using sleep
0
 
LVL 2

Accepted Solution

by:
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

Reginaldo
 
0
 

Author Comment

by:sobhan102398
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
0

Featured Post

Technology Partners: 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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 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