The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
signal handler problems blowtorch High Level Programming 4 08-26-2007 09:14 AM
signal handler for SIGCHLD jens High Level Programming 3 07-02-2005 01:05 PM
Runaway processes killed (Really need help) Micz UNIX for Dummies Questions & Answers 2 10-28-2003 02:45 PM
shell script signal handler jalburger Shell Programming and Scripting 2 12-04-2002 05:10 PM
SIGALRM Help skannan High Level Programming 1 06-16-2002 11:17 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 04-09-2008
stewartw stewartw is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
Runaway SIGALRM signal handler

I have written a program to demonstrate a problem I have encountered when using BSD style asynchronous input using the O_ASYNC flag in conjunction with a real time interval timer sending regular SIGALRM signals to the program. The SIGIO handler obeys all safe practices, using only an atomic update to a volatile sig_atomic_t variable.

The program is below. The idea is that when the user enters a 'q', the program is supposed to stop. It does not. If you uncomment the printf statement in teh main program's loop you will see that the loop is exited, but the return statement is never reached because the SIGALRMS seem to keep the program alive. Be prepared to kill it with a Ctrl-C.

If I use the AIO style asynchronous I/O there is no problem. It is related to the O_ASYNC flag. (In fact if you simply call fcntl() and tell the kernel to send signals to the process, without setting the flag, the same problem will occur! )

Can anyone explain what is wrong?

Code:
#include    <stdio.h>
#include    <signal.h>
#include    <sys/time.h>
#include    <fcntl.h>


void    on_alarm(int);    /* handler for alarm    */
void    on_input(int);    /* handler for keybd    */
int     set_timer( int which, long initial, long repeat );

volatile sig_atomic_t   finished  = 0;

int main( int argc, char * argv[])
{
    int  fd_flags;
    int k = 0;
    struct sigaction newhandler;    
    sigset_t         blocked;       

    newhandler.sa_handler = on_input;  /* handler function    */
    newhandler.sa_flags   = 0;   

    sigemptyset(&blocked);             
    newhandler.sa_mask = blocked;      
    if ( sigaction(SIGIO, &newhandler, NULL) == -1 )
        perror("sigaction");

    newhandler.sa_handler = on_alarm;  /* handler function    */
    if ( sigaction(SIGALRM, &newhandler, NULL) == -1 )
        perror("sigaction");


    fcntl(0, F_SETOWN, getpid());
    fd_flags = fcntl(0, F_GETFL);
    fcntl(0, F_SETFL, (fd_flags|O_ASYNC));

    set_timer(ITIMER_REAL, 500, 500); 


    while( !finished  )    {         
       pause();
       printf("still in loop and finished = %d\n", finished);
    }
    return 0;
}
void on_input(int signum)
{
    int  c = getchar();          
    if ( c == 'q' )
        finished = 1;
}

void on_alarm(int signum)
{
    static int k = 0;
    printf("call number %d\n", k++);
}


int set_timer( int which, long initial, long repeat )
{
    struct itimerval itimer;
    long secs;

    // initialize initial delay
    secs = initial / 1000 ; 
    itimer.it_value.tv_sec     = secs;    
    itimer.it_value.tv_usec    = (initial - secs*1000 ) * 1000 ;   

    // initialize repeat inveral
    secs = repeat / 1000 ; 
    itimer.it_interval.tv_sec  = secs;     
    itimer.it_interval.tv_usec = (repeat - secs*1000 ) * 1000 ;
    
    return setitimer(which, &itimer, NULL);
}

Last edited by Perderabo; 04-10-2008 at 11:30 PM.. Reason: Adding code tags... it's just 13 little keystrokes to readability.
 

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0