Runaway SIGALRM signal handler


 
Thread Tools Search this Thread
Top Forums Programming Runaway SIGALRM signal handler
# 8  
Old 04-11-2008
Ramen Noodle,

You are correct. When I supply the return it terminates. But this is the real story. Below is the original program that had this problem. It uses the NCurses library. I simplified it for the post. This program puts the terminal into cbreak mode and does not require the newline character -- it is using non-blocking I/O. And you will discover that typign newline after the 'q' does not solve it.

But I would be grateful to know what does. Here it is:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curses.h>
#include <signal.h>
#include <sys/time.h>
#include <fcntl.h>
#include <time.h>
#include "timers.h"

/*****************************************************************************/
/* Global Constants */
/*****************************************************************************/

#define INITIAL_SPEED 30
#define RIGHT 1
#define LEFT -1
#define ROW 12
#define MESSAGE "ooooooo=>"
#define REVMSSGE "<=ooooooo"
#define BLANK " "

int row; // current row
int col; // current column
int dir; // Global variable to store direction of movement
int speed; // Current speed in chars/second
volatile sig_atomic_t finished;

/*****************************************************************************/
/* Signal Handler Prototypes */
/*****************************************************************************/
void on_alarm(int); /* handler for alarm */
void on_input(int); /* handler for keybd */

/*****************************************************************************/
/* Utility Function Prototype */
/*****************************************************************************/

void enable_kbd_signals();

/*****************************************************************************/
/* Main */
/*****************************************************************************/
int main( int argc, char * argv[])
{
struct sigaction newhandler; // for installing handlers
sigset_t blocked; // to set mask for handler


// Set up signal handling
newhandler.sa_handler = on_input; // name of handler
newhandler.sa_flags = SA_RESTART; // flag is just RESTART
sigemptyset(&blocked); // clear all bits of blocked set
//sigaddset(&blocked, SIGALRM);
newhandler.sa_mask = blocked; // set this empty set to be the mask
if ( sigaction(SIGIO, &newhandler, NULL) == -1 ) {
perror("sigaction");
return (1);
}

sigemptyset(&blocked); // clear all bits of blocked set
//sigaddset(&blocked, SIGIO);
newhandler.sa_mask = blocked; // set this empty set to be the mask
newhandler.sa_handler = on_alarm; // SIGALRM handler function
if ( sigaction(SIGALRM, &newhandler, NULL) == -1 ){ // try to install
perror("sigaction");
return (1);
}

// Prepare the terminal for the animation
initscr(); // initialize the library and screen
cbreak(); // put terminal into non-blocking input mode
noecho(); // turn off echo
clear(); // clear the screen
curs_set(0); // hide the cursor

// Initialize the parameters of the program
row = ROW;
col = 0;
dir = RIGHT;
finished = 0;
speed = INITIAL_SPEED ;

// Turn on keyboard signals
enable_kbd_signals();

// Start the real time interval timer with delay interval size
set_timer( ITIMER_REAL, 1000/speed, 1000/speed );


// mvaddstr(LINES-1, 0, "Current speed:");
//refresh();

// Put the message into the first position and start
mvaddstr(row, col, MESSAGE);


while( !finished ) {
pause();
}
endwin();
return 0;
}

/*****************************************************************************/

void on_input(int signum)
{
int c;

c = getch();

switch (c) {

case 'Q':

case 'q':

finished = 1;
}
}



/*****************************************************************************/

void on_alarm(int signum)
{

mvaddstr( row, col, BLANK ); // erase old string
col += dir; // advance one column
move( row, col ); // move to new locataion
if ( RIGHT == dir ) {
addstr( MESSAGE ); // add forward string
if ( col+strlen(MESSAGE) >= COLS-1 )
dir = LEFT; // reverse if hitting edge
}
else {
addstr( REVMSSGE ); // add reverse string
if ( col <= 0 )
dir = RIGHT; // reverse if hitting edge
}
refresh();
}

/*****************************************************************************/
/*
* install a handler, tell kernel who to notify on input, enable signals
*/
void enable_kbd_signals()
{
int fd_flags;

fcntl(0, F_SETOWN, getpid());
fd_flags = fcntl(0, F_GETFL);
fcntl(0, F_SETFL, (fd_flags|O_ASYNC));
}
# 9  
Old 04-11-2008
My ncurses is rusty. Like 2003 rusty Smilie
Don't think you want me for this. Sounds debuggable and you seem proficient.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Serial port signal(IRQ) handler (using C)

Hello, I'm writing some serial(UART) handler but have stuck on few issues, maybe anyone can help to show me what I'm doing wrong. Basically I'm intending to write serial RX signal handler. Application receives defined packages of data over serial which contains header and payload. Handler... (3 Replies)
Discussion started by: Lauris_k
3 Replies

2. Programming

problem in doing coding of signal handler

i m unble to execute code of signal handler using a) Wait b) Waitpid (1 Reply)
Discussion started by: madhura
1 Replies

3. Shell Programming and Scripting

Perl Signal Handler

I was working on some Perl code that does signal handling and I came across this one liner and wasn't sure what it was doing. local $SIG{__DIE__} = sub {$! = 2; die $_;}; I think the first part of the anonymous subroutine is setting $! to 2, but I am not sure what the second part is doing. ... (1 Reply)
Discussion started by: SFNYC
1 Replies

4. Programming

Signal Handler Hangs

Hi, I have a problem with signal handler algorithm in linux. My code is hanging ( It is continuously looping inside the signal handler) . I am pasting my code here... Please provide me some help regarding this. I googled many places and wrote this code.. but doesnt seem to be working without... (6 Replies)
Discussion started by: sree_ec
6 Replies

5. Shell Programming and Scripting

Perl - Problems with Signal Handler

I have a problem with signal handlers not working. I have a long 1000 line code and somehow this code for signal handling is not working: $SIG{INT} = \&interrupt; sub interrupt { print STDERR "Caught a control c!\n"; exit; # or just about anything else you'd want to do } Any... (2 Replies)
Discussion started by: som.nitk
2 Replies

6. Programming

Problem with signal handler and interrupted system call

Hi, I have a daq program that runs in an infinite loop until it receives SIGINT. A handler catches the signal and sets a flag to stop the while loop. After the loop some things have to be cleaned up. The problem is that I want my main while loop to wait until the next full second begins, to... (2 Replies)
Discussion started by: soeckel
2 Replies

7. Programming

Usage of exit() inside a signal handler

Is it ok to use exit() inside a signal handler? I catch SIGUSR1 in a signal handler and I try to close a file and then exit. The result is inconsistent. Sometimes the process exit and sometimes it returns to the original state before the signal handler was invoked. Perhaps exit is not legal in... (8 Replies)
Discussion started by: Tuvia
8 Replies

8. Programming

signal handler problems

Hey guys, I am trying to write a little shell, and was writing a signal handler to handle SIGINT (I am using 'stty intr ^C' and using ctrl-C to give SIGINT). I wrote this signal handler: void handle_sigint() { write(2,"handling sigint\n",16); write(1,"\nshell% ",8); } ... (4 Replies)
Discussion started by: blowtorch
4 Replies

9. Programming

signal handler for SIGCHLD

Hi, I have an c++ application which uses the function fork and execvp(). The parent does not wait until the child ends. The parents just creates children and let them do their stuff. You can see the parent program as a batch-manager. I have added a SIGCHLD handler to the program: void... (3 Replies)
Discussion started by: jens
3 Replies

10. Shell Programming and Scripting

shell script signal handler

AIX 4.3.3 I am trying to write a signal handler into a ksh shell script. I would like to capture the SIGTERM, SIGINT, and the SIGTSTP signals, print out a message to the terminal, and continue executing the script. I have found a way to block the signals: #! /bin/ksh SIGTERM=15 SIGINT=2... (2 Replies)
Discussion started by: jalburger
2 Replies
Login or Register to Ask a Question