Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Creating a SIGSEGV for debug purposes

I am working on a lock-free shared variable class, and I want to be able to generate a SIGSEGV fault to see if my implementation works as I planned. I've tried creating a function that modifies a pointer and read it 100 times. I then call this function in both threads and have the threads run infinitely within my program. This doesn't generate the error I want. How should I go about doing this?

edit I don't handle segfaults at all, but they are generated in my program if I remove locks. I want to use a lock-less design, therefore i created a shared variable class that uses CAS to remain lockless. Is there are way that I can have a piece of code that will generate segfaults, so that i can use my class to test that it fixes the problem?

like image 581
RaptorIV Avatar asked Feb 26 '12 22:02

RaptorIV


People also ask

How do you make a SIGSEGV?

h> raise(SIGSEGV); Will cause an appropriate signal to be raised. @RaptorIV: Explain why you care what it is that generates the signal. Raising the signal like that will (with a few caveats) get treated exactly like a "real" segfault the signal will get delivered the exception handler will be invoked if one is present.

What generates a segfault?

A segfault will occur when a program attempts to operate on a memory location in a way that is not allowed (for example, attempts to write a read-only location would result in a segfault). Segfaults can also occur when your program runs out of stack space.

What is SIGSEGV?

SigSegV means a signal for memory access violation, trying to read or write from/to a memory area that your process does not have access to. These are not C or C++ exceptions and you can't catch signals.


1 Answers

#include <signal.h>

raise(SIGSEGV);

Will cause an appropriate signal to be raised.

like image 119
Flexo Avatar answered Sep 30 '22 01:09

Flexo