Re: keypresses and signal handling
- From: Rich <rtillmore@xxxxxxxxx>
- Date: Sun, 26 Sep 2010 11:30:41 -0700 (PDT)
On Sep 25, 3:39 pm, Eric Sosman wrote:
On 9/25/2010 4:06 PM, Rich wrote:
Hi,
In my program I want it so when the users presses the space bar (or
any other key if it is easier) they get a printf("hello, I am still
processing your request please wait") while the program is doing its
work. I added a signal handler to catch Ctrl-C and that works great
(i.e the program gracefully exits instead of crashes). Is there some
other signal I can use to do what I want?
C itself has no such capability, although the systems C runs on
may offer something. Check your system's documentation.
By the way, you almost certainly do *not* want to call printf()
in a signal handler, nor any other library function that your system's
documentation does not specifically describe as "async signal safe" or
something along those lines. Trouble can occur (usually, does occur)
if the signal arrives while the program is executing printf(), or some
other function (malloc(), maybe?) that printf() calls internally. If
you're half-way through a realloc() call, go into the signal handler
while realloc() has flour scattered all over the kitchen and the fridge
door hanging open, and then try to call malloc() before anybody's
had a chance to tidy up -- well, the outcome is seldom a happy one.
An alternative you might consider is to display some kind of
animated progress indicator: A number that starts at 417 and counts
down to zero, or a little twirling baton, or something like that. See
Question 19.3 on the comp.lang.c Frequently Asked Questions (FAQ) page
at <http://www.c-faq.com/>.
My interrupt handler sets a flag variable to 1 and the variable is
tested at key points in the program where the program can flush the
buffers and close all of the open files. As I understand signal
handling (which I admit is limited) if I am in a function that is
doing a malloc (I don't have realloc in my program) and I press Ctrl-C
during the malloc then my program jumps to my signal handler. My
signal handler sets my flag to 1 (flag is a global variable
initialized to 0, and the programs continues where it left off. So in
my case I should be safe. Do I understand this properly?
19.3 is a good start. I was hoping to avoid having something
(progress bar or baton) constantly showing while the program was
running as that would consume processor cycles making the program
slower. Slower by how much I don't know until I do some testing.
I am trying to keep my program OS independent so I really don't want
to use ncurses or some other OS specific library. I realize my
program is almost always used on Linux so POSIX should be available.
SIGQUIT looks promising but I don't know if it works the same the way
on windows 7 or XP.
I gave the FAQ a look over but I didn't read section 19. Displaying a
progress bar or baton doesn't seem like a system dependency to me. It
seems for like a stdio or Miscellaneous to me.
Thank you everyone for your input. I have quite a bit of research and
coding to do to see what will work best for me.
Thanks,
.
- Follow-Ups:
- Re: keypresses and signal handling
- From: Eric Sosman
- Re: keypresses and signal handling
- References:
- keypresses and signal handling
- From: Rich
- Re: keypresses and signal handling
- From: Eric Sosman
- keypresses and signal handling
- Prev by Date: Re: [OT] Finding a word inside a string
- Next by Date: Re: why these codes will cause Segmentation fault in linux?
- Previous by thread: Re: keypresses and signal handling
- Next by thread: Re: keypresses and signal handling
- Index(es):
Relevant Pages
|