Question about signals

Discussion in 'C Programming' started by root, Oct 9, 2009.

  1. root

    root Guest

    Hi

    I have a program where there's a main server thread that receives jobs
    and then farms them out to worker threads.

    Unfortunately there is either a bug or an inconsistency in the jobs data
    or something, because quite often the worker thread will try to
    dereference NULL. This brings down the whole program.

    So what I'd like to do is trap SIGSEGV in the worker threads only (not
    the main thread), maybe log the error, but basically have the relevant
    thread exit gracefully and not whack the whole process.

    Is signal() threadsafe? How can I only catch signals in one thread rather
    than for the process as a whole?

    Thanks for your help.
     
    root, Oct 9, 2009
    #1
    1. Advertising

  2. "root" <> wrote in message
    news:haoc2h$8n4$...
    > Hi
    >
    > I have a program where there's a main server thread that receives jobs
    > and then farms them out to worker threads.
    >
    > Unfortunately there is either a bug or an inconsistency in the jobs data
    > or something, because quite often the worker thread will try to
    > dereference NULL. This brings down the whole program.
    >


    errm...

    bad "data" should not be the cause of bugs.
    try instead fixing the code to not be succeptible to any such bad data.

    ideally, code should be written to resist crashing even if it is given
    largely random garbage.


    so, instead, maybe try to track down the bugs (in a debugger or similar), if
    possible, and then work out where the bugs originate, and either fix them,
    or at least insert basic sanity checks.


    also worth noting:
    are pointers being used correctly and in a thread-safe manner?
    is locking being used correctly?
    making use of 'volatile' in the right places?
    ....


    > So what I'd like to do is trap SIGSEGV in the worker threads only (not
    > the main thread), maybe log the error, but basically have the relevant
    > thread exit gracefully and not whack the whole process.
    >
    > Is signal() threadsafe? How can I only catch signals in one thread rather
    > than for the process as a whole?
    >


    personally, I think you are probably trying to solve the wrong problem
    here...

    more so, this question depends some on the specific OS, which is neither
    specified nor necessaily topical here.


    > Thanks for your help.
     
    BGB / cr88192, Oct 9, 2009
    #2
    1. Advertising

  3. root <> writes:
    > I have a program where there's a main server thread that receives jobs
    > and then farms them out to worker threads.
    >
    > Unfortunately there is either a bug or an inconsistency in the jobs data
    > or something, because quite often the worker thread will try to
    > dereference NULL. This brings down the whole program.
    >
    > So what I'd like to do is trap SIGSEGV in the worker threads only (not
    > the main thread), maybe log the error, but basically have the relevant
    > thread exit gracefully and not whack the whole process.
    >
    > Is signal() threadsafe? How can I only catch signals in one thread rather
    > than for the process as a whole?


    Standard C has no support for threads. Try comp.programming.threads,
    or perhaps a newsgroup that deals with your system
    (comp.unix.programmer if my guess is right).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 9, 2009
    #3
  4. root

    Eric Sosman Guest

    root wrote:
    > Hi
    >
    > I have a program where there's a main server thread that receives jobs
    > and then farms them out to worker threads.
    >
    > Unfortunately there is either a bug or an inconsistency in the jobs data
    > or something, because quite often the worker thread will try to
    > dereference NULL. This brings down the whole program.
    >
    > So what I'd like to do is trap SIGSEGV in the worker threads only (not
    > the main thread), maybe log the error, but basically have the relevant
    > thread exit gracefully and not whack the whole process.
    >
    > Is signal() threadsafe? How can I only catch signals in one thread rather
    > than for the process as a whole?


    1) You have asked a threading question, not a C question.
    Try a forum with "thread" in its name, not this one.

    2) <off-topic> What you would "like to do" is fundamentally
    a Bad Idea, Doomed To Failure, Something That Would Be Imagined
    Only By Someone Who Hasn't Thought It Through (tm). </off-topic>

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 10, 2009
    #4
  5. root wrote:
    > Unfortunately there is either a bug or an inconsistency in the jobs data
    > or something, because quite often the worker thread will try to
    > dereference NULL. This brings down the whole program.
    >
    > So what I'd like to do is trap SIGSEGV in the worker threads only (not
    > the main thread), maybe log the error, but basically have the relevant
    > thread exit gracefully and not whack the whole process.
    >
    > Is signal() threadsafe? How can I only catch signals in one thread rather
    > than for the process as a whole?


    That is the wrong solution.

    The correct solution is to verify each pointer isn't null before
    dereferencing it, avoiding the segfault entirely. Then put in the
    appropriate logging so that you can figure out why those null pointers
    are showing up where you're not expecting them.

    <OT>
    If you weren't using threads, I'd suggest using a debugger instead of
    logging, but I've found that using a debugger on multi-threaded programs
    often changes thread scheduling enough that the bug I'm looking for no
    longer appears.
    </OT>

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
     
    Stephen Sprunk, Oct 10, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    1,426
    Marcus Harnisch
    May 9, 2006
  2. Jackie

    <signal.h> - question about signals

    Jackie, Jul 28, 2004, in forum: C Programming
    Replies:
    11
    Views:
    591
    Villy Kruse
    Aug 2, 2004
  3. Ardni

    Question about signals

    Ardni, Oct 31, 2008, in forum: VHDL
    Replies:
    4
    Views:
    564
    jeppe
    Nov 5, 2008
  4. BLF
    Replies:
    6
    Views:
    1,590
  5. mark

    [Question] signals

    mark, Jul 22, 2009, in forum: C Programming
    Replies:
    12
    Views:
    671
    Nobody
    Jul 27, 2009
Loading...

Share This Page