Sponsored Content
Full Discussion: Signal Handler Hangs
Top Forums Programming Signal Handler Hangs Post 302404202 by sree_ec on Tuesday 16th of March 2010 12:38:37 AM
Old 03-16-2010
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 exit(0).. but i guess this is not the right way. What could be wrong?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

struct sigaction oldHandler;

void myHandler(int sig,  siginfo_t *siginfo, void *context) {
// if i have not written any code inside this function, the program will give a feel of hang( this functions is getting continuously called
        if(siginfo->si_code == SEGV_MAPERR)
        {
                write(1,"address not mapped to object",strlen("address not mapped to object"));
        }
        else if (siginfo->si_code == SEGV_ACCERR)
        {
                write(1,"invalid permissions for mapped object",strlen("invalid permissions for mapped object"));
        }

       write(1,"\n",1);

 //        exit(0); if this exit(0) is not present, program will get continuous calls to this signal handler
        return;
}

int main(int argc, char *argv[]) {

    /* Install mySignalHandler for SIGSEGV */
    struct sigaction sigAct;
    int              status = 0;
    char *addr = NULL;

    sigAct.sa_handler   = 0;
    sigAct.sa_sigaction = myHandler;
    sigfillset(&sigAct.sa_mask);
    sigAct.sa_flags = SA_SIGINFO;

    status = sigaction(SIGSEGV, &sigAct, &oldHandler);
    if (status != 0) {
        perror("Failed to install handler for signal SIGSEGV");
        exit(1);
    }
#if 1
    /* This will invoke the signal handler */
// addr = malloc(strlen("Hello"));
     strcpy(addr,"Hello");
    printf("%s\n",addr);

#endif
        printf("Returning from main\n");
    return 0;
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. Programming

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... (8 Replies)
Discussion started by: stewartw
8 Replies

5. 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

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. 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

8. 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

9. 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

10. 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
BSD_SIGNAL(3)						     Linux Programmer's Manual						     BSD_SIGNAL(3)

NAME
bsd_signal - signal handling with BSD semantics SYNOPSIS
#define _XOPEN_SOURCE #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t bsd_signal(int signum, sighandler_t handler); DESCRIPTION
The bsd_signal() function takes the same arguments, and performs the same task, as signal(2). The difference between the two is that bsd_signal() is guaranteed to provide reliable signal semantics, that is: a) the disposition of the signal is not reset to the default when the handler is invoked; b) delivery of further instances of the signal is blocked while the signal handler is executing; and c) if the handler interrupts a blocking system call, then the system call is automatically restarted. A portable application cannot rely on signal(2) to provide these guarantees. RETURN VALUE
The bsd_signal() function returns the previous value of the signal handler, or SIG_ERR on error. ERRORS
As for signal(2). CONFORMING TO
4.2BSD, POSIX.1-2001. POSIX.1-2008 removes the specification of bsd_signal(), recommending the use of sigaction(2) instead. NOTES
Use of bsd_signal() should be avoided; use sigaction(2) instead. On modern Linux systems, bsd_signal() and signal(2) are equivalent. But on older systems, signal(2) provided unreliable signal semantics; see signal(2) for details. The use of sighandler_t is a GNU extension; this type is only defined if the _GNU_SOURCE feature test macro is defined. SEE ALSO
sigaction(2), signal(2), sysv_signal(3), feature_test_macros(7), signal(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2009-03-15 BSD_SIGNAL(3)
All times are GMT -4. The time now is 11:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy