Reputation: 63
I have a project due soon about using signals and sighandlers and I'm essentially finished with it; however I am hung up on one part. Here is the part pasted from my teacher's prompt : "...When it receives sigusr2 it should toggle into or out off a debug mode, e.g. if the debug mode is currently "on" it should turn debugging off, if debug is "off" it should turn debugging on.
When debugging is "on", your program should report the total every x seconds and ignore sigusr1. When debugging is "off" the alarm should be turned off and your program should handle sigusr1 as before (i.e. report on the status of total) ".
What exactly does he mean by toggle into debug mode? The only debug mode I am familiar with is the eclipse version. Asking another classmate, I got the response that I was suppose to build my own debug mode, also I am not sure what this means. Can anyone shed some light and point my in the right direction? Thank you for reading and thank you for help in advance!
Upvotes: 1
Views: 1675
Reputation: 163287
Forget about "debug" for a moment and just consider that your program needs to have two modes. While in one mode, it should behave a certain way, and in the other mode, it behaves a different way.
What's an easy way of keeping track of two states? A variable with two (or more) possible values, such as bool
. So declare a global variable to keep track of whether you're in one mode or the other (but since using a global bool
variable from a signal handler isn't technically supported, use sig_atomic_t
instead):
sig_atomic_t in_debug_mode;
When you receive the signal that controls which mode your program should be in, change the value of the variable, and then make sure your program behaves in the way consistent with that mode. If you receive the signal and you're not in debug mode, then set in_debug_mode = true
, set up your SIGALRM handler, and disable your SIGUSR1 handler. If you receive the signal and you are in debug mode, then set in_debug_mode = false
, clear the SIGALRM handler, and set the SIGUSR1 handler.
The name of the mode is irrelevant. For the purposes of the assignment, it's called "debug mode," but it has nothing to do with the debugging features of any other program you might be familiar with.
Upvotes: 2
Reputation: 477040
It simply means that your program keeps some state information about the debug level, like so:
int debug_level = 0;
void log(const char * message)
{
if (debug_level > 0)
{
printf("Debug message: %s\n", message);
}
}
Then you can say log("starting loop");
or something like that in your program, and it will only be printed if the debug level is sufficiently high.
When you register the signal handler, you simply let it modify the global debug_level
variable. (Make sure to use type sigatomic_t
for the variable though to ensure that you can safely modify it from within the signal handler!)
Upvotes: 3