Re: keypresses and signal handling



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,
.



Relevant Pages

  • Re: continuous stream of sigsegvs ?
    ... So in this case, the signal handler does nothing to correct the problem, ...   and perhaps dump core, ... Use sigactionto install the handler instead of signal, ...
    (comp.unix.programmer)
  • Re: Run a particular number of concurrent jobs
    ... the number of concurrent simulations by editing the number in a ... You can have a signal handler which does the reaping, ...   processes send a kill to the parent when they're done. ... what the child processes think happened. ...
    (comp.unix.shell)
  • Re: longjmp from a signal handler ... kosher?
    ...   int op_dvd ... signals suck. ... say you cannot longjmp from a signal handler and some say you can. ...
    (comp.lang.c)
  • Re: Handling two signal handler
    ... I want to define signal handler in each module which will do specific ... application.pl   define a a signal handler &globalhandler ... raising the &globalhandler should be called means the global handler ... the value of $SIGbefore you install foo_cleaneras ...
    (comp.lang.perl.misc)
  • Re: system() using vfork() or posix_spawn() and libthr
    ... An interpretation was issued suggesting to remove fork() from the list ... deadlocked when calling forkfrom a signal handler. ... for example, set a flag that the thread is executing signal handler, ... stack pointer when a first signal was delivered to the thread, ...
    (freebsd-hackers)