• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

signal trapping in linux

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
sobhan102398
Asked:
sobhan102398
  • 3
1 Solution
 
sobhan102398Author Commented:
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
 
ozoCommented:
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
 
sobhan102398Author Commented:
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
 
rdelfinoCommented:

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
 
sobhan102398Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now