Operating System - HP-UX
1758583 Members
1991 Online
108872 Solutions
New Discussion юеВ

C breaking infinite loop by <ESC> or <F10>

 
Michel PIGUEL
Occasional Advisor

C breaking infinite loop by <ESC> or <F10>

Hello,
Is someone had an C program example to break an inifinite loop by pressing ESCAPE or F10 keyboard key ?
M.Piguel
9 REPLIES 9
Olav Baadsvik
Esteemed Contributor

Re: C breaking infinite loop by <ESC> or <F10>


Hi,Hi,

I am not quite sure what you are after, but

I assume the following:

. You have a program that is designed to run in
an infinite loop.

. For some reason you want to have the possibility
to exit this loop but not exit the program.

I can think of at least two ways to do this:

1, Use a select call in the loop to notice when some
input is given, test what input was given and act
accordingly.

2, Set up a signal-handler to cacth a signal SIGUSR2
which you may send to the prosess using the kill command
or a small program.

Regards
Olav


Ceesjan van Hattum
Esteemed Contributor

Re: C breaking infinite loop by <ESC> or <F10>

A Ctrl-C is normally a SIGINT signal. Search the web for examples.
Try to understand the 'catch_sigint()' funtion as mentioned in http://www.arcterex.net/c_notes.html

Succes,
Ceesjan
Michel PIGUEL
Occasional Advisor

Re: C breaking infinite loop by <ESC> or <F10>

Hi,
Thanks for these first elements of answer, but I had already found these examples. My problems are:
1. which code is sent when I press or ?
2. all examples speak about SIGINT, SIGABRT and so on but never about others signals.
3. how may I use SIGUSR1 (or SIGUSR2) or write a program which interact when I press a key of my keyboard ?
Regards
Michel
Olav Baadsvik
Esteemed Contributor

Re: C breaking infinite loop by <ESC> or <F10>

Hi,

I think that what code is sent when you press or is
the smallest problem.

What you must consider is how your program will notice that you
press anything at all. Unless you want to use signals you will
have do a read inside the loop. The problem then is that if you
do a read when there is no data entered your read will hang there
and block the execution of the loop.

That is why I mentioned the select() call that may be used to
test if there is something there to read.
Se the man page for select for examples on how this function
may be called from a program.

man select

Regards
Olav
Wodisch_1
Honored Contributor

Re: C breaking infinite loop by <ESC> or <F10>

Hi Michel,

in addition to what the others wrote, you will have to use "curses", to enable your program to understand the encoding of the function keys, in dependence of the environment variable "$TERM"...
The ESCAPE key is easy (code number decimal 27), but the function keys can send from one to many characters PER KEY PRESSED, so that part is a little bit more difficult.
Reading about the library "curses" might help you there.

HTH,
Wodisch
A. Clay Stephenson
Acclaimed Contributor

Re: C breaking infinite loop by <ESC> or <F10>

Normally, waiting for a regular keypress (i.e. a non SIGNAL generating keypress) is a dumb way to do things on a UNIX box; it eats up a great deal of processor time for essentially nothing. It's especially bad if you make a termio call to set MIN to 0 and TIME to 0. It's at least tolerable if you set MIN to 0 and TIME to 1 (but that slows down your loop).

The better way to do this is to let your loop run as fast as possible and to set up a signal handler. You could change the interrupt, kill, or quit keys to whatever you like and then build a signal handler to deal with SIGINT, SIGKILL, or SIGQUIT. If you want to do it at the really low-level use signal,setjmp, and longjmp. Man signal, setjmp, and longjmp for details. By the way, using ESC is generally a bad idea because so many keys generate ESCAPES as part od their sequence.
If it ain't broke, I can fix that.
Michel PIGUEL
Occasional Advisor

Re: C breaking infinite loop by <ESC> or <F10>

Hi, Hi
I've understood it's really better to use interrupt signal like SIGINT etc...
I've made a test and C is correctly trapped but not Y or D.
Where can I find some examples to catch these signals ?
Regards
Michel
A. Clay Stephenson
Acclaimed Contributor

Re: C breaking infinite loop by <ESC> or <F10>

Okay, here is about a 60-second C example of breaking out of a loop with a signal handler. When you press Ctrl-C or whatever your intr key is defined to be, it breaks out of a loop and prints the last statement.

Note that when you use a signal handling function do as little as possible and in most cases you need to reset the handler to itself.

This should get you started but if you need a more complex example using setjmp and longjmp to do a non-local goto then that's about a three-minute bit of coding.
If it ain't broke, I can fix that.
Shannon Petry
Honored Contributor

Re: C breaking infinite loop by <ESC> or <F10>

In addition to what Clay gave, there are some basics you are missing on the terminal.
By default C is mapped to be SIGKILL. You can make it anything you want to with the "stty" command. So asking for D to work, should not be asked, but ask which signals you want your program to die on, and know what signals are mapped to which keys.

This is not as easy as it sounds, because most terminals do not map all signals. So, dont confuse SIGKILL to be the same as C as it's just a keyboard shortcut for the terminal to send the signal.

Regards,
Shannon
Microsoft. When do you want a virus today?