Causing and trapping SIGSEGV
An assignment I was given in my Operating Systems class was to determine how much memory is associated with a program via probing all memory locations and seeing which give you a segmentation fault.
So basically, start at 0 and increment by 4 (32 bit only) for all the memory and keep a counter going every time it doesn't fire the signal off (and SIGBUS I think, don't have a copy of the assignment on me at the time).
My problem is since after the trapping function it just returns to the same line, it's constantly looping after the first segfault. I tried screwing around with global variables and changing the pointer location within the trap statement, but it's kinda hard to tell if that works any better.
For example, having a pointer assigned to a global variable pointer that is incremented by 4 every time the trap gets the signal. Rough sketch, since I'm not on the right OS at the moment to see the file:
int *a, *b;
int count = 0;
In main somewhere
if (signal == SIGSEGV)
*b += 4;
That wouldn't exactly ever exit either. I tried some weird thing called a longjmp, which I was told was like a goto that could jump through functions, but it didn't really work out to well.
Seems like I'm jumping through hoops, because the professor (who refuses to tell us how to do it in the slightest) says it should not be much longer than 20 lines.
Is there some cheap trick I'm missing, because most places say you shouldn't be able to recover from a sigsegv in the first place, and if you do it's likely the rest of the behaviour is undefined.