LinuxQuestions.org
Register a domain and help support LQ
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 09-19-2006, 05:52 AM   #1
Linuxprocess
LQ Newbie
 
Registered: Sep 2006
Posts: 5

Rep: Reputation: 0
Signal() sigaction() and IO interrupt


Hi,
I'm currently discovering a strange behavior with signal() and sigaction() on a RHEL 4.

During a syscall like read interrupted by an ALARM signal : in both cases the handler function is called, but with signal() : the read() continues indeed the read() call is stopped if I use sigaction.

The code :
--------------------------------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void callback(int param)
{
printf("signal !! \n");
}

int main(void)
{ int fd,ret;
char t;
struct sigaction sig;

sig.sa_handler=callback;
sigaction(SIGALRM,&sig,NULL);

fd=open("/dev/ttyS0",O_RDONLY);
printf("open\n");
if(fd<0)exit(1);
alarm(5);
printf("alarm\n");
ret=read(fd,&t,1);
printf("lu=%d (ret=%d)\n",t,ret);
close(fd);
exit(0);
}
--------------------------------------------------

When running, the read() call is interrupted :

> ./sigread
open
alarm
signal !!
lu=0 (ret=-1)
>

But, if I replace sigaction() with :
signal(SIGALRM,callback);
The read() call is not interrupted :
> ./sigread
open
alarm
signal !!
^C
>

I know that :
"The sigaction() function provides a more comprehensive and reliable mechanism for controlling signals; new applications should use sigaction() rather than signal()."
but IHMO it is not sufficient ...

Does anyone have any explanation of this difference ?

Thanks
 
Old 09-20-2006, 04:44 AM   #2
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
First, you're not using "struct sigact" right. Use bzero() to start with a clean structure. Most likely, the sigaction() example doesn't restart read() because the SA_RESTART bit in sa_flags is not set. On the other hand, signal() is usually implemented on top of sigaction() and some platforms set SA_RESTART explicitly for all signals and some make an exception of SIGALRM. I invite you to check the source code to glibc.

PS: You may check:
http://www.gnu.org/software/libc/man...rimitives.html

Last edited by primo; 09-20-2006 at 04:49 AM.
 
Old 09-20-2006, 10:38 AM   #3
Linuxprocess
LQ Newbie
 
Registered: Sep 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot for your explanation ...
I like understand such strange behavior.

Bye
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
RS232 programming (sigaction problem) lucky6969b Linux - Networking 2 04-05-2008 03:06 PM
sigaction() - how to create custom signals? rose_bud4201 Programming 15 05-24-2006 07:04 AM
control is not going to interrupt handler when interrupt comes in serial driver sateeshalla Linux - Kernel 1 05-04-2006 10:43 AM
send arguments to a sigaction handler erwinfletch Programming 1 05-01-2006 08:19 PM
Should I use signal() or sigaction()? .... grupoapunte Programming 2 06-05-2005 12:46 AM


All times are GMT -5. The time now is 08:40 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration