Good evening! I have a problem that I can't seem to find a simple solution for.
I have a program that has a boolean isDebugOn. If this bool is TRUE, debug packets are printed.
I would like the ability to toggle this boolean to TRUE or FALSE at will (whatever method is viable). I have tried setting up environmental variables and changing it during program execution (... does not work b/c shells do not inherit the new env var).
I tried thinking of using a keyboard input... but I can't have a cin waiting for input. The program must be still running it's main loop and still be able to toggle this debug boolean.
But it is necessary sometimes e.g. for opting to print traces while a critical program is running which can not be stopped. Traces can not be kept on all the time because they may take up large space on the hosting machine.
Just to clarify--- this is a program that establishes a socket connection with another machine. The program receives packets from the other machine, and I need a mechanism to toggle printing packets on the fly.
mgupta: About your response..... pausing the program by catching some keystrokes sound like a viable solution. Can you explain more? I can only think that when we push a key, the program stops abruptly (though this momentary stop may cause us to lose some packets).
What do you mean by "printing packets"? Are you displaying their contents on the screen?
Perhaps you could find the size of the file attached to standard input. If there is a character in there, you read it. If it's, say, a 'p' character then you set the boolean to false.
@christname: when I say "printing packets," I mean printing packet contents (data inside the packet). I am displaying the contents on stdout.
@Moooce: I'm on a POSIX system.
@jsmith: Your idea sounds good. The program (luckily) already handles several signals. I'll give SIGHUP a look to see if I can do something with it. Any good tutorial sites for learning POSIX signals?
Whenever a signal is caught by a program, I read somewhere that program execution is halted. If for example I had some software listening in for packets ... and a signal is caught, then would we lose some packets in the process during that momentary system halt?
No. Your program isn't handling receiving the packets, your program basically just receiving from a buffer that the OS is keeping. Otherwise you might even lose packets while trying to receive a different one.
Yup! Thanks jsmith. That's exactly what I did! Thanks for the help. I got it working using SIGUSR1 and just performing a `kill -SIGUSR1 `pgrep 'fileName'`