continuous stream of sigsegv's ? - Unix

This is a discussion on continuous stream of sigsegv's ? - Unix ; hi, why would this code snippet print continuous "caught signal 9" messages on the terminal when the offending code (writing to NULL location) is executed only once: ------------------------ #include #include void segv(int signo) { printf("caught signal %d\n", signo); } int ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: continuous stream of sigsegv's ?

  1. continuous stream of sigsegv's ?

    hi,

    why would this code snippet print continuous "caught signal 9"
    messages on the terminal when the offending code (writing to NULL
    location) is executed only once:

    ------------------------
    #include
    #include

    void
    segv(int signo)
    {
    printf("caught signal %d\n", signo);
    }

    int
    main(void)
    {
    signal(SIGSEGV, segv);
    *((int *)0) = 1;
    return 0;
    }
    -------------------------

    I thought the NULL dereference should cause ONE sigsegv (or maybe 4
    for 4 byte write). but when i run my program i see a continuous stream
    of never ending "caught signal 9" messages. I am missing something
    here?

    I ran this on a 64-bit/Fedora-9 box.

    Thanks in advance
    S.

  2. Re: continuous stream of sigsegv's ?

    slashdev@gmail.com writes:

    > hi,
    >
    > why would this code snippet print continuous "caught signal 9"
    > messages on the terminal when the offending code (writing to NULL
    > location) is executed only once:
    >
    > ------------------------
    > #include
    > #include
    >
    > void
    > segv(int signo)
    > {
    > printf("caught signal %d\n", signo);
    > }
    >
    > int
    > main(void)
    > {
    > signal(SIGSEGV, segv);
    > *((int *)0) = 1;
    > return 0;
    > }
    > -------------------------
    >
    > I thought the NULL dereference should cause ONE sigsegv (or maybe 4
    > for 4 byte write). but when i run my program i see a continuous stream
    > of never ending "caught signal 9" messages. I am missing something
    > here?
    >
    > I ran this on a 64-bit/Fedora-9 box.


    When the signal handler returns, the instruction that generated the
    fault is restarted. This is a feature; in some applications, the signal
    handler can "correct" the condition that caused the fault and return,
    and the main program continues without being aware of the interruption.
    For instance, you can implement user-space virtual memory this way.

    (Maybe you were expecting the system to skip the faulting instruction and
    go on to the next one. If you think about it, you'll realize this
    wouldn't be useful behavior; there is no way in general a program is
    going to run correctly if some of its instructions get skipped without
    its knowledge.)

    So in this case, the signal handler does nothing to correct the problem,
    so when the faulting instruction is restarted, it just faults again, and
    SIGSEGV is raised again.

    Now on modern Linux systems with glibc, a signal handler installed with
    signal() remains installed after the signal is raised (so-called "BSD
    signal semantics"). So your handler just gets called again, and the
    cycle repeats.

    If you want the program to die, you could:

    - call _exit() from the signal handler.

    - call signal(SIGSEGV, SIG_DFL) from the signal handler, and then
    return. Then the next occurence of the signal would kill the process
    and perhaps dump core, as usual.

    - Use sigaction() to install the handler instead of signal(), and use
    the SA_ONESHOT flag, which causes the signal to automatically be set
    back to SIG_DFL after the first time it is delivered. On some systems
    this is the default behavior of signal() (so-called "SYSV signal
    semantics"), and the difficulty of knowing which is which is a major
    reason to use sigaction() all the time, so that you can explicitly
    select which behavior you want.

  3. Re: continuous stream of sigsegv's ?

    On Nov 11, 5:03*pm, slash...@gmail.com wrote:
    > hi,
    >
    > why would this code snippet print continuous "caught signal 9"
    > messages on the terminal when the offending code (writing to NULL
    > location) is executed only once:
    >
    > ------------------------
    > #include
    > #include
    >
    > void
    > segv(int signo)
    > {
    > * * * * printf("caught signal %d\n", signo);
    >
    > }
    >
    > int
    > main(void)
    > {
    > * * * * signal(SIGSEGV, segv);
    > * * * * *((int *)0) = 1;
    > * * * * return 0;}


    > I thought the NULL dereference should cause ONE sigsegv (or maybe 4
    > for 4 byte write). but when i run my program i see a continuous stream
    > of never ending "caught signal 9" messages. I am missing something
    > here?


    Do you expect your code to somehow magically skip a line? How would it
    know what to skip or where to stop skipping?

    DS

+ Reply to Thread