Question about signals

R

root

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

BGB / cr88192

root said:
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.
 
K

Keith Thompson

root said:
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).
 
E

Eric Sosman

root said:
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>
 
S

Stephen Sprunk

root said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top